You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucenenet.apache.org by Кузнецов Владимир Николаевич <ku...@skbkontur.ru> on 2011/06/30 12:59:15 UTC

[Lucene.Net] How to do that the results are displayed in the first original tokens and them with synonyms?

How to do that the results are displayed in the first original tokens and them with synonyms?

 My Analyzer(part) :

public override TokenStream TokenStream(string fieldName, TextReader reader)
 { TokenStream result = new StandardTokenizer(reader); result = new LowerCaseFilter(result); result = new StopFilter(result, stoptable); result = new SynonymFilter(result, synonymEngine); result = new ExtendedRussianStemFilter(result, charset); return result; }
 My SynonymFilter :

internal class SynonymFilter : TokenFilter
 {
 private readonly ISynonymEngine engine;

 private readonly Queue<Token> synonymTokenQueue
 = new Queue<Token>();

 public SynonymFilter(TokenStream tokenStream, ISynonymEngine engine) : base(tokenStream)
 { this.engine = engine; } 
public override Token Next()
 {
 if (synonymTokenQueue.Count > 0)
 { return synonymTokenQueue.Dequeue(); } 
Token t = input.Next();

 if (t == null)
 return null;

 if (t.Type() == "<SYNONYM>")
 return t;

 IEnumerable<string> synonyms = engine.GetSynonyms(t.TermText());

 if (synonyms == null)
 { return t; } 
foreach (string syn in synonyms)
 {
 if (!t.TermText().Equals(syn))
 { var synToken = new Token(syn, t.StartOffset(), t.EndOffset(), "<SYNONYM>"); synToken.SetPositionIncrement(0); synonymTokenQueue.Enqueue(synToken); }
 }

 return t;
 }
 }

Thanks!