在java netbeans中创建一个lucene罗马尼亚词干分析器

时间:2016-05-17 08:38:19

标签: java lucene

我需要做一个简单的搜索引擎,它可以识别和阻止罗马尼亚语单词,包括那些带有变音符号的单词。我使用的是RomanianAnalyzer,但是当涉及到有和没有变音符号的同一个单词时,它没有做正确的词干。

您能帮我一个添加/修改现有罗马尼亚词干分析器的代码吗?

PS:我编辑了这个问题,更清楚了。

1 个答案:

答案 0 :(得分: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);
  }
}