How to see if word exists in Pocketsphinx dictionary?

前端 未结 3 1895
醉酒成梦
醉酒成梦 2021-01-22 18:40

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 recogn

相关标签:
3条回答
  • 2021-01-22 19:04

    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))

    0 讨论(0)
  • 2021-01-22 19:11

    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
    }
    
    0 讨论(0)
  • 2021-01-22 19:12

    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.

    0 讨论(0)
提交回复
热议问题