Share via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

Use a SemanticResultKey to Extract a SemanticResultValue (Microsoft.Speech)

After creating a SemanticResultValue instance and a SemanticResultKey instance to provide access to the semantic value, the semantic key is frequently used to obtain the semantic value in a handler for the SpeechRecognized event on the SpeechRecognitionEngine class.

The e parameter of the SpeechRecognized event handler is of type SpeechRecognizedEventArgs. This class provides access to the Result property, which in turn provides access to the Semantics collection (a collection of key and value pairs) and other useful properties. Given a key (the SemanticResultKey instance), you can determine the associated value (a SemanticResultValue instance) by evaluating the following expression:

e.Result.Semantics["key"].Value.

The example in the topic Add Semantics to a GrammarBuilder Grammar (Microsoft.Speech) creates a GrammarBuilder grammar from a collection of words for the English (U.S.) numbers "zero" through "nine" defined by a Choices object. The following excerpt shows how the example creates a SemanticResultValue value for each of the possible recognized words, pairing each word with its semantic (and in this case, numeric) value.

Choices digits = new Choices();
GrammarBuilder digitValues = new GrammarBuilder();

SemanticResultValue temp = new SemanticResultValue("zero", 0);
digits.Add(temp);
digitValues.Append(temp);

temp = new SemanticResultValue("one", 1);
digits.Add(temp);
digitValues.Append(temp);
temp = new SemanticResultValue("two", 2);
digits.Add(temp);
digitValues.Append(temp);
.
.
.

After creating the GrammarBuilder instance (digitValues), the example creates a new GrammarBuilder instance named gb to associate a SemanticResultKey key with a recognized value from the digitsChoices instance. The event handler for the SpeechRecognized event can use this association to determine the semantic value of a word spoken in an input phrase. The following example reproduces the portion of code in Add Semantics to a GrammarBuilder Grammar (Microsoft.Speech).

GrammarBuilder gb = new GrammarBuilder();
gb.Append(new SemanticResultKey("first_value", digits));
gb.Append("plus");
gb.Append(new SemanticResultKey("second_value", digits));
gb.Append("equals");Grammar g = new Grammar(gb);
sre.LoadGrammar(g);

With the SemanticResultValue and SemanticResultKey infrastructure in place, the event handler for the SpeechRecognized event is able to determine semantic values from an input phrase. When a user speaks a phrase such as "five plus seven equals," the event handler in the following example gets the semantic values for "five" and "seven" (namely, 5 and 7), and adds them to produce their sum. When the user finishes speaking the input phrase, the recognizer raises the SpeechRecognized event, which causes the event handler shown in the following example to be called.

Note

In the following example, the application is assumed to have registered to be notified of the SpeechRecognized event and has supplied a handler for the event.

The e.Result.Semantics expression represents the dictionary of semantic key-value pairs. The e.Result.Semantics["first_value"].Value expression represents the semantic value of the first number word in the input phrase, and the e.Result.Semantics["second_value"].Value expression represents the semantic value of the second number word in the input phrase.

void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
  int a = 0;
  int b = 0;
  bool errorCondition = false;

  if (e.Result != null)
  {
    if (e.Result.Semantics != null)
    {
      a = (int)e.Result.Semantics["first_value"].Value;
      b = (int)e.Result.Semantics["second_value"].Value;
    }

    else
    // Error: e.Result.Semantics == null
    {
      errorCondition = true;
    }
  }
  
  else
  // Error: e.Result == null
  {
    errorCondition = true;
  }

  if (!errorCondition)
  {
    string str = a.ToString();
    str += " + ";
    str += b.ToString();
    str += " = ";
    int result = a + b;
    str += result.ToString();
    MessageBox.Show(str);
  }
  else
  {
    MessageBox.Show("Error occurred");
  }
}

See Also

Concepts

Create Grammars Using GrammarBuilder (Microsoft.Speech)