2013-06-02 20 views
0

雖然瀏覽我想出了一個lucene的拼寫檢查程序。我有興趣從tangentum添加phonetix附加組件(特別是metaphone)。有沒有一種方法可以將metaphone整合到我的程序中?如何整合它?如何將Metaphone整合到java-lucene中的拼寫檢查程序中?

package com.lucene.spellcheck; 
import java.io.File; 
import java.io.IOException; 
import org.apache.lucene.document.Document; 
import org.apache.lucene.document.Field; 
import org.apache.lucene.index.Term; 
import org.apache.lucene.search.IndexSearcher; 
import org.apache.lucene.search.spell.Dictionary; 
import org.apache.lucene.search.spell.PlainTextDictionary; 
import org.apache.lucene.search.spell.SpellChecker; 
import org.apache.lucene.store.Directory; 
import org.apache.lucene.store.FSDirectory; 
public class SimpleSuggestionService { 
private static final String F_WORD = null; 
public static void main(String[] args) throws Exception { 
File dir = new File("e:/spellchecker/"); 
Directory directory = FSDirectory.open(dir); 
SpellChecker spellChecker1 = new SpellChecker(directory); 
spellChecker1.indexDictionary(
new PlainTextDictionary(new File("c:/fulldictionary00.txt"))); 
String wordForSuggestions = "noveil"; 
int suggestionsNumber = 5; 
String[] suggestions = spellChecker1. 
suggestSimilar(wordForSuggestions, suggestionsNumber); 
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); 
} 
} 
}  

回答

0

您可以在自定義StringDistance實現利用所需語音算法,或結合它是某種方式與其它相似的算法(如標準LevensteinDistance通過。你只需要實現getDistance的(字符串, String)方法在你的StringDistance實現中,可能類似於:

public MetaphoneDistance() { 
    Metaphone metaphone = new Metaphone(); 
} 

//I'm not really familiar with the library you mentioned, but I assume generateKeys performs a double metaphone? 
public float getDistance(String str1, ,String str2) { 
    String[] keys1 = metaphone.getKeys(str1); 
    String[] keys2 = metaphone.getKeys(str2); 
    float result = 0; 
    if (key1[0] == key2[0] || key1[0] == key2[1]) result += .5 
    if (key1[1] == key2[0] || key1[1] == key2[1]) result += .5 
    return result; 
}