Using the Interpretation Object
#include <LV_SRE_Semantic.h>
When the speech port executes your
semantic interpretation
tags, the output is an ECMAScript (JavaScript) object. LumenVox provides a C and C++ API for examining this
object. When the speech port has finished its decode, and processed the resulting parse tree and tags, you
may request an interpretation object. The interpretation object contains information about the decode --
confidence score, matching grammar, etc. -- plus a single semantic data object.
C API
// the name of the active grammar that matched this interpretation
// the Speech Engine's confidence in this interpretation
// the sentence that the Speech Engine decoded
// the object returned by the semantic interpretation process
C++ API
int confidence
= interpretation.
Score( );
LVSemanticData result_data
= interpretation.
ResultData ( );
Semantic Data Examples
In the following examples, the grammar will be:
#ABNF 1.0;
language en-US;
mode voice;
tag-format <lumenvox/1.0>;
root $small_number_and_text;
$base = (one:"1"|two:"2"|three:"3"|four:"4"|five:"5"|six:"6"|seven:"7"|
eight:"8"|nine:"9"){ $ = parseInt($) };
$teen = ten:"10"|eleven:"11"|twelve:"12"|thirteen:"13"|fourteen:"14"|fifteen:"15" |
sixteen:"16"|seventeen:"17"|eighteen:"18"|nineteen:"19"
{ $ = parseInt($) };
$twenty_to_ninetynine = (twenty:"20"|thirty:"30"|forty:"40"|fifty:"50"|sixty:"60"|
seventy:"70"|eighty:"80"|ninety:"90"){ $ = parseInt($) }
[$base { $ += $base }];
$tens = ($base|$teen|$twenty_to_ninetynine) { $ = $$ };
$hundred = ([a] hundred {$ = 100} | $base hundred {$ = 100 * $base});
$small_number = $hundred {$ = $$} [[and] $tens {$ += $$}] | $tens { $ = $$ };
$small_number_and_text = $small_number { $.number = $$; $.text = $$$.text };
And the input sentence will be "four hundred and six." If you do not understand how
SRGS grammars are written, or how the semantic
interpretation process works, please read the SRGS Grammar and/or
Semantic Interpretation
tutorials now.
The result of the semantic interpretation process on the input sentence is an ECMAScript object that looks
like this:
small_number_and_text :
{
number: 406,
text: "four hundred and six"
}
Example 1: Access Data Directly
If we knew that our application would always be receiving an object containing an integer property named
"number", and a string property named "text", we could write code to retrieve the
data as follows:
C Code
int number = LVSemanticData_GetInt(number_container);
H_SI_DATA text_container = LVSemanticObject_GetPropertyValue(result,"text");
C++ Code
int number
= result_obj
["number"].
GetInt( );
const char* text
= result_obj
["text"].
GetString( );
Example 2: Traverse a Semantic Data Structure
You can get the interpretation data as an XML string using GetInterpretationString.:
C Code
if (LV_SRE_GetNumberOfInterpretations(port_handle, voice_channel) > 0)
{
printf("%s\n",LV_SRE_GetInterpretationString(hport, voice_channel, index));
}
C+ Code
if (port.GetNumberOfInterpretations(voicechannel) > 0)
{
printf("%s\n",port.GetInterpretationString(voicechannel, index)
}
Note that you will need to parse this string in your client application.
The following code prints a generic interpretation object as an XML fragment:
C Code
void PrintXML(H_SI hsi)
{
printf("<%s>\n",result_name);
printf("</%s>\n",result_name);
}
void PrintDataXML(H_SI_DATA hsi)
{
int i;
int n;
const char* property_name;
H_SI_DATA data;
{
case SI_TYPE_BOOL:
break;
case SI_TYPE_INT:
break;
case SI_TYPE_DOUBLE:
break;
case SI_TYPE_STRING:
break;
case SI_TYPE_OBJECT:
for (i = 0; i < n; i++)
{
printf("<%s>\n", property_name);
PrintDataXML(data);
printf("</%s>\n", property_name);
}
break;
case SI_TYPE_ARRAY:
for (i = 0; i < n; i++)
{
printf("<item>\n");
PrintDataXML(data);
printf("</item>\n");
}
break;
}
}
Result
<small_number_and_text>
<number>406</number>
<text>four hundred and six</text>
</small_number_and_text>
Once you have examined the results and no longer need to perform decodes on a port, you should unload all your
local grammars and close that speech port.
See Also