Semantic interpretation¶
This chapter describes the semantic interpretation tags supported by the ASR library, and provides relevant deployment details, as well as presenting test tools that can be used for developing and debugging grammars with semantic interpretation.
The ASR library allows semantic interpretation tags in SISR (Semantic Interpretation for Speech Recognition) format to be added to SRGS grammars. This section presumes that you are already familiar with the SISR standard. If you are not, it is important for you to check the specifications for details on the adopted syntax and its corresponding semantics.
The SISR standard defines two semantic tags: the literal format and the scripts format. The ASR library supports both formats, as presented in the following sections.
Literal interpretation¶
The literal format is a very simple way to associate a chain of characters to each sentence recognized by the grammar. Due to this simplicity, the format presents very little flexibility, and is only adequate for very simple grammars, such as lists of commands or confirmation grammars.
To use tags in ABNF grammars in the literal format, the tag-format declaration must be added to the grammar file header, informing that the adopted format is semantics/1.0-literals.
tag-format <semantics/1.0-literals>;
In the XML format, the grammar element’s tag-format feature is used.
<grammar tag-format="semantics/1.0-literals" ...>
If a grammar has interpretation tags but their format is not declared in the header, it is assumed that they are in the literal format. The following example illustrates a grammar with tags in the literal format, stored in the sim_nao.gram file.
#ABNF 1.0 UTF-8;
tag-format <semantics/1.0-literals>;
root $root;
$root = $sim {SIM} | $nao {NAO};
$sim = sim | confirmo | [é] isso [mesmo] | correto;
$nao = não [confirmo] | [está] (errado | incorreto);
The grammar can be tested with a command line, based on a text entry, using the grammar-parse
tool. The tool receives an entry in the form of a grammar file and a sequence of words, and outputs the semantic result (if any) in JSON format. Please check the following examples of how the tools is used to test the sim_nao.gram file.
$ grammar-parse sim_nao.gram sim
"SIM"
$ grammar-parse sim_nao.gram é isso mesmo
"SIM"
$ grammar-parse sim_nao.gram não
"NAO"
$ grammar-parse sim_nao.gram está incorreto
"NAO"
The result is displayed in the default output in quotation marks, since it is in the JSON format.
Interpretation with scripts¶
The ASR library also supports the script format defined by the SISR specification. To do so, the semantics/1.0 tag format must be used in the grammars.
tag-format <semantics/1.0>;
<grammar tag-format="semantics/1.0" ...>
The following example illustrates a number grammar, using tags in script format. The grammar, called numeros.gram, recognizes written-out numbers between zero and ninety nine, returning the matching numeric value.
#ABNF 1.0 UTF-8;
tag-format <semantics/1.0>;
root $numero;
$numero = $digito_0_9 | $dezena_10_19 | $dezena_20_90;
$digito_0_9 = zero {out=0} | $digito_1_9;
$digito_1_9 = um {out=1}
| dois {out=2}
| três {out=3}
| quatro {out=4}
| cinco {out=5}
| seis {out=6}
| sete {out=7}
| oito {out=8}
| nove {out=9};
$dezena_10_19 = dez {out=10}
| onze {out=11}
| doze {out=12}
| treze {out=13}
| catorze {out=14}
| quinze {out=15}
| dezesseis {out=16}
| dezessete {out=17}
| dezoito {out=18}
| dezenove {out=19};
$dezena_20_90 = vinte {out=20}
| vinte e $digito_1_9 {out = 20 + rules.latest()}
| trinta {out=30}
| trinta e $digito_1_9 {out = 30 + rules.latest()}
| quarenta {out=40}
| quarenta e $digito_1_9 {out = 40 + rules.latest()}
| cinquenta {out=50}
| cinquenta e $digito_1_9 {out = 50 + rules.latest()}
| sessenta {out=60}
| sessenta e $digito_1_9 {out = 60 + rules.latest()}
| setenta {out=70}
| setenta e $digito_1_9 {out = 70 + rules.latest()}
| oitenta {out=80}
| oitenta e $digito_1_9 {out = 80 + rules.latest()}
| noventa {out=90}
| noventa e $digito_1_9 {out = 90 + rules.latest()}
;
Grammars in script format can also be tested using the grammar-parse
tool.
$ grammar-parse numeros.gram vinte e cinco
25
$ grammar-parse numeros.gram quarenta e dois
42
If you wish to print values on the screen while running semantic interpretation, you can use the debug
function, as explained below in pizza.gram grammar.
#ABNF 1.0 UTF-8;
tag-format <semantics/1.0>;
root $root;
$root = [$quero] $quant (pizza | pizzas) [de] $sabor
{debug("Texto:", meta.quero.text);
out.quant = rules.quant;
out.sabor = rules.sabor}
;
$quero = [eu] (quero | queria | gostaria de) [uma];
$quant = uma {out=1}
| duas {out=2}
| três {out=3}
| quatro {out=4}
| cinco {out=5}
| seis {out=6}
| sete {out=7}
| oito {out=8}
| nove {out=9};
$sabor = escarola {out="ESCAROLA"}
| marguerita {out="MARGUERITA"}
| calabresa {out="CALABRESA"};
Now check these examples of using the grammar-parse tool on the pizza.gram grammar.
$ grammar-parse pizza.gram quero uma pizza de calabresa
Texto: quero
{"quant":1,"sabor":"CALABRESA"}
$ grammar-parse pizza.gram gostaria de duas pizzas de escarola
Texto: gostaria de
{"quant":2,"sabor":"ESCAROLA"}
It is possible, in compliance with the SISR specification, to reference external grammars in the body of script grammars, and vice-versa.