How do I load a text grammar in a SAPI 5.4 C# program?

杀马特。学长 韩版系。学妹 提交于 2019-12-24 00:37:49

问题


I've been using SRGS grammars with SAPI 5.4 to define command and control grammars. Now I'd like to switch to text grammars so they'd be compatible with SAPI 5.1 (and possibly even SAPI 4?).

I found an example of a SAPI 5.4 compatible text grammar here

I'd like to load that grammar from it's XML file at runtime as opposed to using the grammar compiler tool and then including the binary version of the grammar as a resource. Ideally I'd like to be able to create an XDocument in memory and then have the Speech.Recognizer listen for those commands.

Does anyone know how to do this? Thanks!


回答1:


To use the text grammars you have to directly interact with the SAPI COM (by including the Microsoft Speech Object Library) - instead of just using the higher level System.Speech.

So if you copied the text grammar linked to above and saved it as sol.xml (which it is in the SAPI SDK samples) you could load it using the following code:

SpSharedRecoContext reco = new SpSharedRecoContext();
ISpeechRecoGrammar grammar;
grammar = reco.CreateGrammar();
grammar.CmdLoadFromFile("sol.xml");
grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive);

I tested that code with XP (5.1), Visa (5.3), and 7 (5.4).



来源:https://stackoverflow.com/questions/3588739/how-do-i-load-a-text-grammar-in-a-sapi-5-4-c-sharp-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!