2016-05-17 31 views
1

我需要做一個簡單的搜索引擎,它可以識別和干擾羅馬尼亞語詞彙,包括帶有變音符的詞語。我使用了羅馬尼亞語分析器,但是當涉及到使用和不使用變音符號的同一個詞時,它並沒有做正確的詞法分析。在java中創建一個lucene羅馬語詞幹程序netbeans

你能幫我一個添加/修改現有羅馬尼亞詞幹程序的代碼嗎? PS:我編輯了這個問題,要更清楚一點。

回答

2

您可以複製RomanianAnalyzer源創建自定義的分析和過濾器在createComponents方法添加到分析鏈。 ASCIIFoldingFilter可能會是你要找的。我會將其添加到最後,以確保在刪除變音符號時不會搞亂提示詞。

public final class RomanianASCIIAnalyzer extends StopwordAnalyzerBase { 
    private final CharArraySet stemExclusionSet; 

    public final static String DEFAULT_STOPWORD_FILE = "stopwords.txt"; 
    private static final String STOPWORDS_COMMENT = "#"; 

    public static CharArraySet getDefaultStopSet(){ 
    return DefaultSetHolder.DEFAULT_STOP_SET; 
    } 

    private static class DefaultSetHolder { 
    static final CharArraySet DEFAULT_STOP_SET; 

    static { 
     try { 
     DEFAULT_STOP_SET = loadStopwordSet(false, RomanianAnalyzer.class, 
      DEFAULT_STOPWORD_FILE, STOPWORDS_COMMENT); 
     } catch (IOException ex) { 
     throw new RuntimeException("Unable to load default stopword set"); 
     } 
    } 
    } 

    public RomanianASCIIAnalyzer() { 
    this(DefaultSetHolder.DEFAULT_STOP_SET); 
    } 

    public RomanianASCIIAnalyzer(CharArraySet stopwords) { 
    this(stopwords, CharArraySet.EMPTY_SET); 
    } 

    public RomanianASCIIAnalyzer(CharArraySet stopwords, CharArraySet stemExclusionSet) { 
    super(stopwords); 
    this.stemExclusionSet = CharArraySet.unmodifiableSet(CharArraySet.copy(stemExclusionSet)); 
    } 

    @Override 
    protected TokenStreamComponents createComponents(String fieldName) { 
    final Tokenizer source = new StandardTokenizer(); 
    TokenStream result = new StandardFilter(source); 
    result = new LowerCaseFilter(result); 
    result = new StopFilter(result, stopwords); 
    if(!stemExclusionSet.isEmpty()) 
     result = new SetKeywordMarkerFilter(result, stemExclusionSet); 
    result = new SnowballFilter(result, new RomanianStemmer()); 
//This following line is the addition made to the RomanianAnalyzer source. 
    result = new ASCIIFoldingFilter(result); 
    return new TokenStreamComponents(source, result); 
    } 
} 
+0

是的,這有幫助。不完美,但比羅馬尼亞安妮澤()更好。謝謝! –

相關問題