mal wieder ine Problem: Weiß jemand zufällig wie ich in SAPI5.1 ein Grammar aus einer xml Datei laden kann? Bei mir sieht das bis jetzt in etwa so aus: (für das laden ist die Funktion loadGrammarFile zuständig, file wird aus aufrufender Klasse übergeben)
Code: Alles auswählen
from win32com.client import constants
from thread import *
import win32com.client
import pythoncom
import thread
import time
connect = win32com.client.Dispatch("Python.GetSRServer")
class pySapiIn:
""" Initialize the speech recognition with the passed in list of words """
def __init__(self):
# controls the thread (while 1 => running)
self.wordsToAdd = [ "One", "Two", "Three", "Four", "Reduce", "Stop" ]
self.running = 1
self.active = 1
self.file = "tv.txt"
# 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()
# set Dictation State
# at first use
# Do not allow free word recognition - only command and control
# recognizing the words in the grammar only
self.DictationState(0)
# Load Grammar
self.LoadGrammar(self.wordsToAdd)
# And add an event handler that's called back when recognition occurs
# start this event handler in new thread
start_new_thread(self.ConnectEvent,())
# wait_for_events = self.ConnectEvent()
def ConnectEvent(self):
self.eventHandler = ContextEvents(self.context)
while self.running == 1:
time.sleep(1)
while self.active == 1:
pythoncom.PumpWaitingMessages()
def LoadGrammar(self, wordsToAdd):
# 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 self.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()
def DictationState(self,state):
self.grammar.DictationSetState(state)
def ShutItAllDown(self):
self.active = 0
self.running = 0
del self.listener
def deactivate(self):
self.active = 0
def activate(self):
self.active = 1
start_new_thread(self.ConnectEvent,())
def StartServerConnection(self):
self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
return None
def shutdown(self):
self.active = 0
self.running = 0
return None
def restart(self):
self.active = 1
self.running = 1
start_new_thread(self.ConnectEvent,())
return None
def loadGrammarFile(self, file):
self.file = file
self.grammar = self.context.CreateGrammar()
self.grammar.CmdLoadFromFile(file, constants.SLODynamic)
self.grammar.CmdSetRuleIdState(0, constants.SGDSActive)
self.grammar.CmdSetRuleState("fileRule", 1)
self.DictationState(0)
print "Grammar Loaded"
return None
"""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)
erg = connect.OnWordRecognition(newResult.PhraseInfo.GetText())