2017-06-12 3 views
0

私はいくつかのフレーズを含むファイルを持っています。 luceneによるjarowinklerを使用して、私はそのファイルからの私の入力の最も類似したフレーズを取得するはずです。luceneのJarowinklerDistanceが奇妙な結果を返す

これは私の問題の例です。

我々が含まれているファイルがあります:私の入力は、これは良いあるであれば、類似性スコアがここにあることから、最初のファイルから「これは良いですが」私を得ることになっている

//phrases.txt 
this is goodd 
this is good 
this is god 

を最大(1)。しかし何らかの理由で、それは「これは良い」と「これは神だ」と返すだけです!

try { 
    SpellChecker spellChecker = new SpellChecker(new RAMDirectory(), new JaroWinklerDistance()); 
    Dictionary dictionary = new PlainTextDictionary(new File("src/main/resources/words.txt").toPath()); 
    IndexWriterConfig iwc=new IndexWriterConfig(new ShingleAnalyzerWrapper()); 
    spellChecker.indexDictionary(dictionary,iwc,false); 

    String wordForSuggestions = "this is good"; 

    int suggestionsNumber = 5; 

    String[] suggestions = spellChecker.suggestSimilar(wordForSuggestions, suggestionsNumber,0.8f); 
    if (suggestions!=null && suggestions.length>0) { 
     for (String word : suggestions) { 
      System.out.println("Did you mean:" + word); 
     } 
    } 
    else { 
     System.out.println("No suggestions found for word:"+wordForSuggestions); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

答えて

1

suggestSimilarは、入力と同じです提案を提供しません。

は、ここに私のコードです。ソースコードを引用すると:あなたはwordForSuggestionsが辞書にあるかどうかを知りたい場合は

//が自身のために言葉を示唆していない、それは愚かな

だろうことは、existメソッドを使用します。

if (spellChecker.exist(wordForSuggestions)) { 
    //do what you want for an, apparently, correctly spelled word 
}