#Test Script for computer virtual personality. #Credits to Inigo Surguy (inigosurguy@hotmail.com) 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)
import pyTTS tts = pyTTS.Create() help(tts.Speak)
#Test Script for computer virtual personality. #Credits to Inigo Surguy (inigosurguy@hotmail.com) 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 time """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", "Good", "Great", "Wonderful", "How are you?", "Not well", "Awful", "Bad", "Not good", "Not too well", "Thank you", "Thanks", "Time" ] speechReco = SpeechRecognition(wordsToAdd) while 1: pythoncom.PumpWaitingMessages() if RecognitionType == ("Hello Aelita"): self.say("Hello, Loren. How are you today?") if RecognitionType == ("Fine") or ("Great") or ("Wonderful") or ("Good"): say("Great") say("Is there anything I can do for you?") if RecognitionType == ("Not well") or ("Awful") or ("Bad") or ("Not Good") or ("Not too well"): say("That's too bad") say("If there's anything or need, just let me know.") if RecognitionType == ("Time"): say("The time is " + time.asctime(),)
if RecognitionType == ("Hello Aelita"): self.say("Hello, Loren. How are you today?") if RecognitionType == ("Fine") or ("Great") or ("Wonderful") or ("Good"): say("Great") say("Is there anything I can do for you?") if RecognitionType == ("Not well") or ("Awful") or ("Bad") or ("Not Good") or ("Not too well"): say("That's too bad") say("If there's anything or need, just let me know.") if RecognitionType == ("Time"): say("The time is " + time.asctime(),)
Help on method Speak in module pyTTS.sapi: Speak(self, text, *flags) method of pyTTS.sapi.SynthAndOutput instance Speaks text with the optional flag modifiers. Text can include XML commands.
Traceback (most recent call last): File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 305, in RunScript debugger.run(codeObject, __main__.__dict__, start_stepping=1) File "C:\Python25\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run _GetCurrentDebugger().run(cmd, globals,locals, start_stepping) File "C:\Python25\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 631, in run exec cmd in globals, locals File "C:\Users\Owner\Desktop\Nina Interaction.py", line 109, in <module> "What time is it" : "speaker.Speak('The time is ' + time.asctime)"} NameError: name 'self' is not defined
# A code to Interact with an autonomous robot called Nina. # Modified from Inigo Surguy's sample code for speech recognition # and the timer sample scripts by Christian Wyglendowski (http://mail.python.org/pipermail/tutor/2004-November/033333.html) # Further commentary by Surguy. # Sample code for speech recognition using the MS Speech API # Inigo Surguy (inigosurguy@hotmail.com) import time import threading class Timer(threading.Thread): def __init__(self, seconds): self.runTime = seconds threading.Thread.__init__(self) def run(self): time.sleep(self.runTime) class GreetingTimer(Timer): def run(self): counter = self.runTime for sec in range(self.runTime): time.sleep(1.0) counter -= 1 Greeting = 0 GT = GreetingTimer(10) Greeting = 0 from win32com.client import constants import win32com.client import pythoncom """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) def SetItems(self): self.items = pickle.load(open(self.SAVE_FILENAME)) while Greeting == 1: self.items = {"Fine" : "self.say('That's good to hear!'); Greeting = 0", "Great" : "self.say('Fantasic!'); Greeting = 0", "Wonderful" : "self.say('That's spectacular'); Greeting = 0", "Not Good" : "self.say('That's too bad'); Greeting = 0", "Terrible" : "speaker.Speak('I'm sorry'); Greeting = 0", "Awful" : "speaker.Speak('Oh I'm very sorry'); Greeting = 0"} if __name__=='__main__': wordsToAdd = [ "Hello Nina", "Fine", "Great", "Wonderful", "Not Good", "Terrible", "Awful", "What time is it"] speechReco = SpeechRecognition(wordsToAdd) while 1: pythoncom.PumpWaitingMessages() self.items = {"Hello Nina" : "Greeting = 1; GT.start", "What time is it" : "speaker.Speak('The time is ' + time.asctime)"}
| DaniWeb Message | |
| Cancel Changes | |