如何使用分析器“分析”字符串,并返回分析的字符串?
我正在尝试在此网站上找到以下代码,但它正在抛出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
进行测试
答案 0 :(得分:3)
尝试使用tokenStreamResult.GetAttribute&lt; ITermAttribute &gt;()