2013-02-02 67 views
3

如何使用分析器來「分析」字符串並返回分析的字符串?使用Lucene分析器解析字符串

我想下面的代碼中發現過這個網站,但它拋出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

回答

3

>()代替

測試嘗試tokenStreamResult.GetAttribute < ITermAttribute