zoukankan      html  css  js  c++  java
  • Lucene.Net 3.0.3如何从TokenStream中获取token对象

    Lucene.Net最高版本为3.0.3,并且apache已经不再提供Lucene.Net的更新,没仔细研究过Lucene.Net的所有版本,Lucene.Net3.0.3遍历TokenStream获取Token对象,已经和以前的版本有了很大的区别,很多方法都已经删除了或者过时。

    以前版本的Lucene.Net从TokenStream中获取Token时调用Next方法就行了,源代码如下

    -收缩C#代码

    public void ReusableTokenStream2()
            {
    string testwords = "编程设计网www.coding123.net";
                SimpleAnalyzer simple = new SimpleAnalyzer();
                TokenStream ts = simple.ReusableTokenStream("", new StringReader(testwords));
                Token token;
    while ((token = ts.Next()) != null)
                {
                    Console.WriteLine(token.TermText());
                }
                ts.Close();
            }

    但是在Lucene.Net3.0.3中TokenStream已经不存在Next方法,而是需要ITermAttribute属性接口来遍历。源代码如下

    -收缩C#代码

    public void ReusableTokenStream3()
            {
                Lucene.Net.Analysis.Standard.StandardAnalyzer a = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
    string s = "编程设计网www.coding123.net";
                System.IO.StringReader reader = new System.IO.StringReader(s);
                Lucene.Net.Analysis.TokenStream ts = a.TokenStream(s, reader);
    bool hasnext = ts.IncrementToken();
                Lucene.Net.Analysis.Tokenattributes.ITermAttribute ita;
    while (hasnext)
                {
                    ita = ts.GetAttribute<Lucene.Net.Analysis.Tokenattributes.ITermAttribute>();
                    Console.WriteLine(ita.Term);
                    hasnext = ts.IncrementToken();
                }
                ts.CloneAttributes();
                reader.Close();
                a.Close();
                Console.ReadKey();
            }

    网上找到的lucene.net的资料都是老版本的,新版本的lucene的资料都是基于java的,lucene.net的很少,只要看下java版本的代码,然后对比lucene.net对应类的源代码了摸索,那个郁闷。。

  • 相关阅读:
    R语言 ggplot2包
    C++实现景区信息管理系统
    linux系统目录介绍
    Python中的赋值、深拷贝与浅拷贝(内存地址)
    三大相关系数: pearson, spearman, kendall(python示例实现)
    Xshell删除键不好使:删除显示退格^H
    Spark SQL中出现 CROSS JOIN 问题解决
    Python apply函数
    Python Dataframe 分组排序和 Modin
    Python 中的时间处理包datetime和arrow
  • 原文地址:https://www.cnblogs.com/adodo1/p/4328144.html
Copyright © 2011-2022 走看看