【问题标题】:Exception while using apache lucene for stop words removal使用 apache lucene 去除停用词时出现异常
【发布时间】:2017-08-22 05:05:57
【问题描述】:

我正在使用以下代码从输入文本中删除停用词。 tokenStream.incrementToken() 运行时出现以下异常。

java.lang.IllegalStateException: TokenStream contract violation: reset()/close() call missing, reset() called multiple times, or subclass does not call super.reset(). Please see Javadocs of TokenStream class for more information about the correct consuming workflow.

代码:

public static String removeStopWords(String textFile) throws Exception {
        CharArraySet stopWords = EnglishAnalyzer.getDefaultStopSet();
        TokenStream tokenStream = new StandardTokenizer();
        tokenStream = new StopFilter(tokenStream, stopWords);
        StringBuilder sb = new StringBuilder();
        CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
        tokenStream.reset();
        while (tokenStream.incrementToken()) {
            String term = charTermAttribute.toString();
            sb.append(term + " ");
        }
        return sb.toString();
    }

【问题讨论】:

    标签: lucene stop-words


    【解决方案1】:

    如下实例化您的 TokenStream -

    TokenStream tokenStream = new StandardAnalyzer().tokenStream("field",new StringReader(textFile));
    

    【讨论】:

    • "field" 是创建的 TokenStream 用于的字段 (IndexableField) 的名称。如果您的 tokenStream 不特定于某个字段,则可以改为传递 null。此外,由于您的输入是字符串,您可以使用 - tokenStream(null, textFile);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 2014-05-20
    • 2019-01-25
    • 2015-09-05
    • 2017-01-21
    相关资源
    最近更新 更多