Note
Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.
Create and Access Semantic Content (Microsoft.Speech)
While performing recognition, a speech recognition engine returns recognition results to the speech application that include semantic information about the recognized speech input, as well as the text of recognized words and phrases. The semantic information contained in a recognition result is often more meaningful to an application than the recognized text. You can author semantic content and the code that retrieves semantics from recognition results to provide actionable information to your application.
Create Semantic Content
Although the speech recognition engine generates some semantic content by default, you can create the semantic structure for recognized speech by assigning the semantic values and semantic keys that the speech recognition engine returns in the recognition result. The mechanism for authoring semantic content depends on the process you use to author a grammar.
Create Dynamic Grammars Programmatically
For grammars created programmatically using the GrammarBuilder and Choices classes, you can assign semantic values using the Append(SemanticResultValue) method and assign semantic keys using the Append(SemanticResultKey) method. You can assign semantic value to items in a list within a Choices object by passing a SemanticResultValue object to the Add([]) method. For more information, see Add Semantics to a GrammarBuilder Grammar (Microsoft.Speech).
For grammars created programmatically using classes of the Microsoft.Speech.Recognition.SrgsGrammar namespace, you can assign semantic content using the constructors on the SrgsSemanticInterpretationTag class.
Create Static Grammars as Files
- For grammars created in XML-format that comply with Speech Recognition Grammar Specification (SRGS) Version 1.0, you enter semantic content in tag elements (tag Element (Microsoft.Speech)) that are a children of either an item Element (Microsoft.Speech) or a rule Element (Microsoft.Speech). For more information, see Using the tag Element (Microsoft.Speech) and Support for Semantic Markup (Microsoft.Speech).
Access the Recognition Result
An application registers to be notified of events that the speech recognition engine raises when it is processing speech input. A SpeechRecognitionEngine instance raises the SpeechHypothesized, SpeechRecognitionRejected or SpeechRecognized, and RecognizeCompleted events to provide information about a recognition operation. Event arguments for these events are provided by, respectively, the SpeechHypothesizedEventArgs, SpeechRecognitionRejectedEventArgs, SpeechRecognizedEventArgs, and RecognizeCompletedEventArgs classes. All of these classes inherit from the RecognitionEventArgs class. Importantly, all these classes inherit the Result property on RecognitionEventArgs.
You can access the Result property using the second parameter, which is usually named e, on the event handler. The following example shows an empty handler for the SpeechRecognized event. Event handlers for the SpeechHypothesized, SpeechRecognitionRejected, and RecognizeCompleted events are similar.
void sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
// Handle SpeechRecognized event.
}
The Result property is a reference to a RecognitionResult instance, which has properties that contain information about alternate recognition results, the audio associated with the recognized phrase, the degree of certainty for the recognized phrase, and a number of other items of interest about the recognized phrase.
Access the Semantics in the Recognition Result
Semantics is an important property of RecognitionResult that contains a reference to a SemanticValue instance, which can be thought of as a collection of key/value pairs that represents the semantic organization of a recognized phrase. You can access the keys and values for the semantics of a recognition result using the IDictionary<String, SemanticValue>.Keys and IDictionary<String, SemanticValue>.Values properties, respectively.
The following illustration shows the properties on RecognitionResult and SemanticValue classes, which you can access using the Result property.
See Also
Reference
Concepts
Add Semantics to a GrammarBuilder Grammar (Microsoft.Speech)
Use a SemanticResultKey to Extract a SemanticResultValue (Microsoft.Speech)