19 using System.Collections.Generic;
22 using Lucene.Net.Analysis;
23 using Lucene.Net.Documents;
24 using Lucene.Net.Search;
25 using Lucene.Net.Index;
26 using Lucene.Net.QueryParsers;
27 using Lucene.Net.Store;
30 namespace Lucene.Net.Search.Vectorhighlight
39 private String fieldName;
40 public LinkedList<TermInfo> termList =
new LinkedList<TermInfo>();
42 public static void Main(String[] args)
69 #if LUCENENET_350 //Lucene.Net specific code. See https://issues.apache.org/jira/browse/LUCENENET-350
72 this.fieldName = fieldName;
74 List<string> termSet = fieldQuery.
getTermSet(fieldName);
77 if (termSet == null)
return;
80 VectorHighlightMapper tfv =
new VectorHighlightMapper(termSet);
83 if (tfv.Size==0)
return;
86 foreach (String term
in terms)
88 if (!StringUtils.AnyTermMatch(termSet, term))
continue;
89 int index = tfv.IndexOf(term);
91 if (tvois == null)
return;
92 int[] poss = tfv.GetTermPositions(index);
93 if (poss == null)
return;
94 for (
int i = 0; i < tvois.Length; i++)
95 termList.AddLast(
new TermInfo(term, tvois[i].StartOffset, tvois[i].EndOffset, poss[i]));
101 #else //Original Port
104 this.fieldName = fieldName;
107 if (tfv == null)
return;
113 catch (InvalidCastException e)
118 List<String> termSet = fieldQuery.
getTermSet(fieldName);
120 if (termSet == null)
return;
122 foreach (String term
in tpv.GetTerms())
124 if (!termSet.Contains(term))
continue;
125 int index = tpv.IndexOf(term);
127 if (tvois == null)
return;
128 int[] poss = tpv.GetTermPositions(index);
129 if (poss == null)
return;
130 for (
int i = 0; i < tvois.Length; i++)
131 termList.AddLast(
new TermInfo(term, tvois[i].GetStartOffset(), tvois[i].GetEndOffset(), poss[i]));
140 void Sort(LinkedList<TermInfo> linkList)
143 linkList.CopyTo(arr, 0);
144 Array.Sort(arr,
new Comparison<TermInfo>(PosComparer));
147 foreach (
TermInfo t
in arr) linkList.AddLast(t);
152 return t1.Position - t2.Position;
159 public string FieldName
161 get {
return fieldName; }
170 if (termList.Count == 0)
return null;
172 LinkedListNode<TermInfo> top = termList.First;
173 termList.RemoveFirst();
184 termList.AddFirst(termInfo);
191 public bool IsEmpty()
193 return termList == null || termList.Count == 0;
204 public TermInfo(String text,
int startOffset,
int endOffset,
int position)
207 this.startOffset = startOffset;
208 this.endOffset = endOffset;
209 this.position = position;
217 public int StartOffset
219 get {
return startOffset; }
224 get {
return endOffset; }
229 get {
return position; }
232 public override string ToString()
234 StringBuilder sb =
new StringBuilder();
235 sb.Append(text).Append(
'(').Append(startOffset).Append(
',').Append(endOffset).Append(
',').Append(position).Append(
')');
236 return sb.ToString();
241 return (this.position - o.position);