19 using System.Collections.Generic;
22 using Lucene.Net.Analysis;
23 using Lucene.Net.Analysis.Tokenattributes;
24 using Lucene.Net.Index;
25 using Lucene.Net.Search;
26 using Lucene.Net.Store;
28 namespace WorldNet.Net
33 static List<String> already;
37 public static void Main(System.String[] args)
41 System.Console.Out.WriteLine(typeof(
SynLookup) +
" <index path> <word>");
50 String word = args[1];
52 var countingCollector =
new CountingCollector();
53 searcher.Search(query, countingCollector);
55 if (countingCollector.numHits == 0)
57 Console.Out.WriteLine(
"No synonyms found for " + word);
61 Console.Out.WriteLine(
"Synonyms found for \"" + word +
"\":");
64 var hits = searcher.Search(query, countingCollector.numHits).ScoreDocs;
67 hits.Select(t => searcher.Doc(t.Doc)).Select(doc => doc.GetValues(
Syns2Index.
F_SYN)).SelectMany(values => values))
69 Console.Out.WriteLine(v);
84 public static Query Expand(String query,
90 already =
new List<String>();
91 var top =
new List<String>();
93 var ts = a.
TokenStream(field,
new StringReader(query));
96 while (ts.IncrementToken())
98 var word = termAtt.
Term;
100 if (!already.Contains(word))
110 System.Collections.IEnumerator it = top.GetEnumerator();
111 while (it.MoveNext())
114 var word = (String)it.Current;
116 tmp.Add(tq,
Occur.SHOULD);
118 var c =
new CollectorImpl(field, boost);
125 internal sealed
class CountingCollector :
Collector
129 public override void SetScorer(
Scorer scorer)
132 public override void Collect(
int doc)
137 public override void SetNextReader(
IndexReader reader,
int docBase)
140 public override bool AcceptsDocsOutOfOrder
149 internal sealed
class CollectorImpl :
Collector
152 private readonly
string field;
153 private readonly
float boost;
155 public CollectorImpl(
string field,
float boost)
161 public override void SetScorer(
Scorer scorer)
166 public override void Collect(
int doc)
169 var values = d.GetValues(Syns2Index.F_SYN);
170 foreach (var syn
in values.Where(syn => !already.Contains(syn)))
178 tmp.Add(tq,
Occur.SHOULD);
182 public override void SetNextReader(
IndexReader reader,
int docBase)
184 this.reader = reader;
187 public override bool AcceptsDocsOutOfOrder
200 public const String F_SYN =
"syn";
203 public const String F_WORD =
"word";