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方法就行了,源代码如下

    public void ReusableTokenStream2()
            {
                string testwords = "web开发网";
                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属性接口来遍历。源代码如下

    复制代码
      public void ReusableTokenStream3()
            {
                Lucene.Net.Analysis.Standard.StandardAnalyzer a = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
                string s = "web开发网";
                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();
            }
    复制代码
  • 相关阅读:
    node.js中用户密码的加密
    linux下高可用LVS搭建及配置方法
    在Centos 5.x或6.x上安装RHEL EPEL Repo
    shell脚本 expect 实现自动登陆
    platform 收集linux/windows操作系统信息
    市面上比较流行的图表展示
    django作业2
    django实现分片上传文件
    linux下大于2T硬盘格式化方法
    django之第二天
  • 原文地址:https://www.cnblogs.com/xbzhu/p/11826618.html
Copyright © 2011-2022 走看看