Hello Everyone. I'm new to python and so far, I simply love it! I'm still just starting to get the knack though and there's a lot more I want to know. I've decided to use it mainly for my robots' programming scripts. Right now I'm currently stuck on a simple script on getting my windows vista machine to recognize phrases I say, and reply to them using TTS. It's a learning process in itself.
Bare with me, most of the time I feel like I haven't got a clue when I'm writing a python script. It's the first programming language I'm learning.
Here's the script I'm using on my computer. I'll implement it into my robot later, once I get it to work. I modified it from two python sample scripts by Inigo Surguy and Peter Parente, so a lot of credit goes to them, at least until I become savy enough to write my original speech Recognition and TTS scripts... What I want it to do is wait for me to say "Hello, Aelita" which is what I call my laptop, and respond by addressing my name and asking how I am doing. I'm also trying to get it to tell me the time, whenever I say "Time." It is as follows:
#Test Script for computer virtual personality. #Credits to Inigo Surguy (firstname.lastname@example.org) and Peter Parente for original #Speech Recognition and TTS scripts. #Further commentary by Surguy from win32com.client import constants import win32com.client import pythoncom import pyTTS impor time tts = pyTTS.Create() tts.Rate = 1 tts.Volume = 90 tts.GetVoiceNames() tts.SetVoiceByName('MS-Anna-1033-20-DSK') """Sample code for using the Microsoft Speech SDK 5.1 via COM in Python. Requires that the SDK be installed (it's a free download from http://www.microsoft.com/speech and that MakePy has been used on it (in PythonWin, select Tools | COM MakePy Utility | Microsoft Speech Object Library 5.1). After running this, then saying "One", "Two", "Three" or "Four" should display "You said One" etc on the console. The recognition can be a bit shaky at first until you've trained it (via the Speech entry in the Windows Control Panel.""" class SpeechRecognition: """ Initialize the speech recognition with the passed in list of words """ def __init__(self, wordsToAdd): # For text-to-speech self.speaker = win32com.client.Dispatch("SAPI.SpVoice") # For speech recognition - first create a listener self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer") # Then a recognition context self.context = self.listener.CreateRecoContext() # which has an associated grammar self.grammar = self.context.CreateGrammar() # Do not allow free word recognition - only command and control # recognizing the words in the grammar only self.grammar.DictationSetState(0) # Create a new rule for the grammar, that is top level (so it begins # a recognition) and dynamic (ie we can change it at runtime) self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0) # Clear the rule (not necessary first time, but if we're changing it # dynamically then it's useful) self.wordsRule.Clear() # And go through the list of words, adding each to the rule [ self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd ] # Set the wordsRule to be active self.grammar.Rules.Commit() self.grammar.CmdSetRuleState("wordsRule", 1) # Commit the changes to the grammar self.grammar.Rules.Commit() # And add an event handler that's called back when recognition occurs self.eventHandler = ContextEvents(self.context) # Announce we've started self.say("Started successfully") """Speak a word or phrase""" def say(self, phrase): self.speaker.Speak(phrase) """The callback class that handles the events raised by the speech object. See "Automation | SpSharedRecoContext (Events)" in the MS Speech SDK online help for documentation of the other events supported. """ class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")): """Called when a word/phrase is successfully recognized - ie it is found in a currently open grammar with a sufficiently high confidence""" def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result): newResult = win32com.client.Dispatch(Result) if __name__=='__main__': wordsToAdd = [ "Hello Aelita", "Fine", "How are you?", "Not well", "Awful", "Bad", "Not good", "Not too well", "Thank you", "Thanks", "Time" ] speechReco = SpeechRecognition(wordsToAdd) while 1: pythoncom.PumpWaitingMessages() if speechReco("Hello Aelita"): greeting1 = "Hello, Lore-enn. How are you today?" tts.Speak(greeting1) if speechReco("Fine"): great = "Great!" tts.Speak(great) offr1 = "Is there anything I can do for you?" tts.Speak(offr1) if speechReco("Not well") or ("Awful") or ("Bad") or ("Not Good") or ("Not too well"): sorry1 = "That's too bad" sorry2 = "I'm so sorry." tts.Speak(sorry1) or (sorry2) onDuty1 = "If there's anything or need, just let me know." tts.Speak(onDuty1) if speechReco("Thanks") or ("Thank you"): hum1 = "Don't mention it" hum2 = "No problem" hum3 = "Your welcome" tts.Speak(hum1) or (hum2) or (hum3) if speechReco("Time"): timeStr1 = "The time is " + time.asctime(), timeStr2 = "It's " + time.asctime() timeStr3 = "Right now, it's " + time.asctime() tts.Speak(timeStr1) or (timeStr2) or (timeStr3)
It's working mostly, in that it opens up Microsoft Speech Recognition and recognizes the words I say, and as far as I know the script is free from syntax errors and exceptions. The problem I'm having now is getting the computer's voice to say something back. As of now, the phrase just shows up in the MSR window but doesn't say anything back. I think it has to do with the "speechReco" I write for when the computer's supposed to catch a certain phrase. I don't think its the right term. So, the MSR is recognizing my words, but the python script doesn't, hence no TTS response. I'd appreciate it if maybe someone could help me out with this script, and/or tell me if there's some sort of python syntax dictionary or glossary out there, that tells all the different words you can right in a python script, when to use them, and what they do?
Once again, bare with me. I'm trying to learn as much as I can about this stuff. Thanks.