Skip to content

Adding grammars and dictionaries

remuladgryta edited this page Apr 10, 2013 · 3 revisions

Adding grammars and word dictionaries is crucial to make any use of the speech recognition API, grammars use a format similar to the Java Speech Grammar Format (JSGF) and word dictionaries use the Sphinx-3 dictionary format (explanation)

Grammars

To add valid grammars for speech recognition, you need to call addGrammarEntry(name, rule, public) on the API instance (API.get()).

The rule parameter needs to be a valid Java Speech Grammar Format rule expansion (right hand side of assignment). For example, if we have the rule <greeting> = hello | good (morning | day | evening) the name parameter would be greeting and the rule parameter would be hello | good (morning | day | evening). The public parameter determines if the rule is a top-level rule (available for detection) or only makes up part of a top level rule.

To demonstrate this, say we have the rules:

public <phrase> = <greeting> <person>;
<greeting> = hello | good morning;
<person> = searge | lex | cpw;

The API calls you'd need to do is:

API api = API.get();
api.addGrammarEntry("person", "direwolf | pahimar", false);
api.addGrammarEntry("greeting", "hello | good morning", false);
api.addGrammarEntry("phrase", "<greeting> <person>", true);

Dictionaries

By default, the SpeechAPI includees the cmudict0.6d dictionary, which contains most common english words (and a lot of uncommon ones too). As you may have noticed in the previous example however, neither direwolf or pahimar are english words, so in order to detect them, they need to be added to the dictionary. This can be done by calling the addDictionaryEntry(URL) method on the API instance. The URL parameter must point to a valid Sphinx-3 formatted dictionary file, and it will be treated as an addendum to the existing dictionary.

In our example, we'd need a file with the following contents:

DIREWOLF D AY ER W UH L F
PAHIMAR P AA HH IH M AH R

Assume you package this in the root of your .jar, as modname.dict, you'd need to call

API.get().addDictionaryEntry(this.class.getResource("/modname.dict"));
Clone this wiki locally