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对应类的源代码了摸索,那个郁闷。。

  • 相关阅读:
    关于Thread ThreadPool Parallel 的一些小测试demo
    VS附加到进程调试
    netcore 实现一个简单的Grpc 服务端和客户端
    CodeSmith 找不到请求的 .Net Framework Data Provider
    ocelot集成consul服务发现
    使用ocelot作为api网关
    关于add migration 报错的问题解决方案
    关于多线程efcore dbcontext 的解决方案。
    docker mysql 容器报too many connections 引发的liunx磁盘扩容操作
    关于liunx 机器脱机环境(netcore)Nuget包迁移的问题
  • 原文地址:https://www.cnblogs.com/adodo1/p/4328144.html
Copyright © 2011-2022 走看看