2016-05-17 61 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

是的,这有帮助。不完美,但比罗马尼亚安妮泽()更好。谢谢! –