How to see if word exists in Pocketsphinx dictionary?

核能气质少年 提交于 2019-12-02 02:15:29

问题


I simply want to see if a string exists in a dictionary file. (Dictionary file at bottom of question)

I want to check if the voice recognizer can recognize a word or not. For example, the recognizer will not be able to recognize a string of ahdfojakdlfafiop, because that is not defined in the dictionary. So, can I check if a word is in the dictionary of pocktsphinx?

Something like:

    if(myString.existsInDictionary){
startListeningBecauseExists();
    }else(
//Doesn't exist in dictionary!!!
       }

I just want a way to be able to tell if the recognizer can listen for what I want it to listen to.

here is the dictionary file:

https://raw.githubusercontent.com/cmusphinx/pocketsphinx-android-demo/master/app/src/main/assets/sync/cmudict-en-us.dict

Thanks,

Ruchir


回答1:


In C there is ps_lookup_word function which allows you to lookup for the word:

if (ps_lookup_word(ps, "abc") == NULL) {
    // do something
}

In Java wrapper it's a method Decoder.lookupWord:

if(decoder.lookupWord("abc") == null) {
    // do something
}

In Android, you can access decoder from Recognizer:

if(recognizer.getDecoder().lookupWord("abc") == null) {
    // do something
}



回答2:


Read the file using BufferedReader and store all the words in ArrayList

ArrayList<String> dictionary = new ArrayList<>();
String line;
BufferedReader reader = new BufferedReader(new FileReader(dictionaryFile));
while((line = reader.readLine()) != null) {
    if(line.trim().length() <= 0 ) {
        continue;
    }
    String word = line.split(" ")[0].trim();
    word = word.replaceAll("[^a-zA-Z]", "");
    dictionary.add(word);
}

then check if word present in dictionary using

dictionary.contains(yourString);

Hope it'll help.




回答3:


You could load the dictionary to a arraylist by reading it line by line and to get only the words do

arraylist.add(line.split("\\s+")[0]);

And then check if it exist by

if(arraylist.contains(word))



来源:https://stackoverflow.com/questions/35737607/how-to-see-if-word-exists-in-pocketsphinx-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!