Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create an application that uses speech recognition using System.Speech. I want to load a grammar that has been stored in XML format. I can't figure out how to do it. Some things I have found say that xml grammars can not be used with System.Speech but can be used with SAPI. I can not find anything that shows how to do a c# SAPI application.

I need to be able to implement speech recognition using xml grammars. I would very much appreciate anyone that can help me achieve that goal.
Posted
Updated 22-Dec-18 3:24am

1 solution

If the XML file follows the SRGS specification[^], you can:
C#
Grammar gr = new Grammar(@"path\to\grammar.xml");
 
Share this answer
 
Comments
jmallory0001 12-May-15 14:02pm    
I have looked through the link you gave but don't see a complete example. Is the following compliant with the SRGS:

<grammar xml:lang="en-US" xmlns="http://www.w3.org/2001/06/grammar" root="topLevel" version="1.0">
<rule id="topLevel">
<ruleref uri="#playCommands"></ruleref>
</rule>
<rule id="playCommands">
<ruleref uri="#playAction"></ruleref>
<item> the </item>
<ruleref uri="#fileWords"></ruleref>
</rule>
<rule id="playAction">
<one-of>
<item> play </item>
<item> start </item>
<item> begin </item>
</one-of>
</rule>
<rule id="fileWords">
<one-of>
<item> song </item>
<item> tune </item>
<item> track </item>
<item> item </item>
</one-of>
</rule>
</grammar>
Thomas Daniels 12-May-15 14:32pm    
Yes, I believe that's valid SRGS.

(Also, I edited your comment to fix the XML tags; they didn't show up because they were regarded as HTML tags)
jmallory0001 12-May-15 14:48pm    
Thank you very much for your help. It seems to be loading now but I'm still not getting the SpeechRecognized event. It hits the Hypothesized event but no further. Do you have any guidance to resolve this?
Thomas Daniels 12-May-15 14:52pm    
Have you loaded the Grammar into the speech recognizer, using the LoadGrammar method?
jmallory0001 12-May-15 14:55pm    
Yes. My code is below:
RecognitionEngine = new SpeechRecognitionEngine();
RecognitionEngine.SetInputToDefaultAudioDevice();
RecognitionEngine.SpeechRecognized += SpeechRecognized;
RecognitionEngine.SpeechDetected += SpeechDetected;
RecognitionEngine.SpeechHypothesized += SpeechHypothesized;

SrgsDocument srgsdoc = new SrgsDocument("..\\Grammars\\RootGrammar.xml");
Grammar grammar = new Grammar(srgsdoc);
RecognitionEngine.LoadGrammar(grammar);

RecognitionEngine.RecognizeAsync(RecognizeMode.Multiple);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900