3

如何使用分析器“分析”字符串,并返回分析后的字符串?

我正在尝试在此站点上找到的以下代码,但它抛出了一个ArgumentException- “此 AttributeSource 没有属性 Lucene.Net.Analysis.Tokenattributes.TermAttribute”

     public static string AnalyseString(Analyzer analyser, string stringToAnalyse)
    {

        MemoryStream ms = new MemoryStream();
        StreamWriter sw = new StreamWriter(ms);
        sw.Write(stringToAnalyse);
        sw.Flush();
        ms.Seek(0, SeekOrigin.Begin);
        StreamReader sr = new StreamReader(ms);

        TokenStream tokenStreamResult = analyser.TokenStream(null,sr);
        StringBuilder sb = new StringBuilder();
        //Lucene.Net.Analysis.Token t = new Lucene.Net.Analysis.Token();

        while (tokenStreamResult.IncrementToken())
        {
            var attrib = tokenStreamResult.GetAttribute<TermAttribute>();

            string t2 = tokenStreamResult.GetAttribute<TermAttribute>().Term;

            sb.Append(t2 + " ");


        }
        return sb.ToString();
    }

我正在使用最新的 Lucene.Net 版本(3.0.3.0),并且正在使用SimpleAnalyzer

4

1 回答 1

3

尝试 tokenStreamResult.GetAttribute< ITermAttribute >() 代替

于 2013-04-09T05:40:59.900 回答