3

我根据“Lucene in action”一书编写了简单的代码,它应该向我展示不同的分析器是如何工作的(使用 Lucene 4.3.0)。但是当我尝试增加 tokenStream 时,我会收到 NullPointerException。你有什么建议吗?下面我附上代码:

public static void main(String[] args) throws IOException {
    System.out.println("\n----");
    System.out.println("StandardAnalyzer");
    displayTokensWithFullDetails(new StandardAnalyzer(Version.LUCENE_43),
        "I'll email you at xyz@example.com");
}

public static void displayTokensWithFullDetails(Analyzer analyzer,
                                                  String text) throws IOException {

    TokenStream stream = analyzer.tokenStream("contents",
                                              new StringReader(text));

    //defining attributes

    while(stream.incrementToken()) {

      //print details of tokens

    }
    System.out.println();
  }

我无法开始循环。你知道我做错了什么吗?

在这里我添加堆栈跟踪:

----
StandardAnalyzer
Exception in thread "main" java.lang.NullPointerException
at           org.apache.lucene.analysis.standard.StandardTokenizerImpl.zzRefill(StandardTokenizerImpl.java:923)
at org.apache.lucene.analysis.standard.StandardTokenizerImpl.getNextToken(StandardTokenizerImpl.java:1133)
at org.apache.lucene.analysis.standard.StandardTokenizer.incrementToken(StandardTokenizer.java:171)
at org.apache.lucene.analysis.standard.StandardFilter.incrementToken(StandardFilter.java:49)
at org.apache.lucene.analysis.core.LowerCaseFilter.incrementToken(LowerCaseFilter.java:54)
at org.apache.lucene.analysis.util.FilteringTokenFilter.incrementToken(FilteringTokenFilter.java:50)
at AnalyzerUtils.displayTokensWithFullDetails(AnalyzerUtils.java:115)
at AnalyzerUtils.main(AnalyzerUtils.java:169)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
4

1 回答 1

2

好的。我找到了解决方案。启动 TokenStream 后,StandardTokenizationImpl 不接收 Reader 对象而是接收 null。当我们在流 StandardTokenizationImpl 上键入 reset() 时,最终会收到我们的 Stream。所以我的代码应该如下所示:

public static void main(String[] args) throws IOException {
    System.out.println("\n----");
    System.out.println("StandardAnalyzer");
    displayTokensWithFullDetails(new StandardAnalyzer(Version.LUCENE_43),
        "I'll email you at xyz@example.com");
}

public static void displayTokensWithFullDetails(Analyzer analyzer,
                                                  String text) throws IOException {

    TokenStream stream = analyzer.tokenStream("contents",
                                              new StringReader(text));

    stream.reset(); //Add this line removes NullPointerException

    //defining attributes

    while(stream.incrementToken()) {

      //print details of tokens

    }
    System.out.println();
  }
于 2013-05-26T12:58:38.037 回答