繁体   English   中英

使用Lucene从字符串数组中获取所有单词作为标记

[英]Get all words from a string array as tokens using Lucene

我有一个字符串数组

String []str={"This is a demo","only test","nothing more"}

当我使用Lucene标记这些数组时,我得到的只是这是一个演示,我将在下面附加我的Java代码:

try {
                for(String str2:str ){
                TokenStream stream = analyzer.tokenStream("field", new StringReader(str2));                               
                 CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class);
                    stream.reset(); 
                       while (stream.incrementToken()) {
                           System.out.println(termAtt.toString());
                              }             
                             stream.end(); 
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }

我需要数组中的每个单词作为标记。

我运行了您的代码,并发现有关TokenStream未关闭的异常。 修复起来很简单:

public static void main(String[] args) throws IOException {
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
    String []str={"This is a demo","only test","nothing more"};
    for (String str2 : str) {
        TokenStream stream = analyzer.tokenStream("field", new StringReader(str2));
        stream.reset();
        CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class);
        while (stream.incrementToken()) {
            System.out.println(termAtt.toString());
        }
        stream.end();
        stream.close();
    }
}

以上印刷品

演示
只要
测试
没有
更多

可以预期,因为其他单词都是停用词。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM