How can you use an Analyser to 'analyse' a string, and return the analysed string?
I am trying the below code found off this site, but it is throwing an ArgumentException - "This AttributeSource does not have the attribute 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();
}
I am using the latest Lucene.Net version (3.0.3.0), and am testing with a SimpleAnalyzer
Try tokenStreamResult.GetAttribute<ITermAttribute>() instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With