19 using System.Collections.Generic;
22 using Lucene.Net.Documents;
23 using Lucene.Net.Search;
24 using Lucene.Net.Index;
26 using WeightedFragInfo = Lucene.Net.Search.Vectorhighlight.FieldFragList.WeightedFragInfo;
27 using SubInfo = Lucene.Net.Search.Vectorhighlight.FieldFragList.WeightedFragInfo.SubInfo;
28 using Toffs = Lucene.Net.Search.Vectorhighlight.FieldPhraseList.WeightedPhraseInfo.Toffs;
30 namespace Lucene.Net.Search.Vectorhighlight
35 public static String[] COLORED_PRE_TAGS = {
36 "<b style=\"background:yellow\">",
"<b style=\"background:lawngreen\">",
"<b style=\"background:aquamarine\">",
37 "<b style=\"background:magenta\">",
"<b style=\"background:palegreen\">",
"<b style=\"background:coral\">",
38 "<b style=\"background:wheat\">",
"<b style=\"background:khaki\">",
"<b style=\"background:lime\">",
39 "<b style=\"background:deepskyblue\">",
"<b style=\"background:deeppink\">",
"<b style=\"background:salmon\">",
40 "<b style=\"background:peachpuff\">",
"<b style=\"background:violet\">",
"<b style=\"background:mediumpurple\">",
41 "<b style=\"background:palegoldenrod\">",
"<b style=\"background:darkkhaki\">",
"<b style=\"background:springgreen\">",
42 "<b style=\"background:turquoise\">",
"<b style=\"background:powderblue\">"
45 public static String[] COLORED_POST_TAGS = {
"</b>" };
48 : this(new String[] {
"<b>" },
new String[] {
"</b>" })
55 this.preTags = preTags;
56 this.postTags = postTags;
59 static Object CheckTagsArgument(Object tags)
61 if (tags is String)
return tags;
62 else if (tags is String[])
return tags;
63 throw new ArgumentException(
"type of preTags/postTags must be a String or String[]");
66 public abstract List<WeightedFragInfo> GetWeightedFragInfoList(List<WeightedFragInfo> src);
70 String[] fragments = CreateFragments(reader, docId, fieldName, fieldFragList, 1);
71 if (fragments == null || fragments.Length == 0)
return null;
75 public virtual String[] CreateFragments(
IndexReader reader,
int docId, String fieldName,
FieldFragList fieldFragList,
int maxNumFragments)
77 if (maxNumFragments < 0)
78 throw new ArgumentException(
"maxNumFragments(" + maxNumFragments +
") must be positive number.");
80 List<WeightedFragInfo> fragInfos = GetWeightedFragInfoList(fieldFragList.
fragInfos);
82 List<String> fragments =
new List<String>(maxNumFragments);
83 Field[] values = GetFields(reader, docId, fieldName);
84 if (values.Length == 0)
return null;
85 StringBuilder buffer =
new StringBuilder();
86 int[] nextValueIndex = { 0 };
87 for (
int n = 0; n < maxNumFragments && n < fragInfos.Count; n++)
90 fragments.Add(MakeFragment(buffer, nextValueIndex, values, fragInfo));
92 return fragments.ToArray();
96 protected virtual String[] GetFieldValues(
IndexReader reader,
int docId, String fieldName)
110 protected virtual String MakeFragment(StringBuilder buffer,
int[] index, String[] values,
WeightedFragInfo fragInfo)
112 int s = fragInfo.startOffset;
113 return MakeFragment(fragInfo, GetFragmentSource(buffer, index, values, s, fragInfo.endOffset), s);
118 int s = fragInfo.startOffset;
119 return MakeFragment(fragInfo, GetFragmentSource(buffer, index, values, s, fragInfo.endOffset), s);
124 StringBuilder fragment =
new StringBuilder();
126 foreach (
SubInfo subInfo
in fragInfo.subInfos)
128 foreach (
Toffs to
in subInfo.termsOffsets)
130 fragment.Append(src.Substring(srcIndex, to.startOffset - s - srcIndex)).Append(GetPreTag(subInfo.seqnum))
131 .Append(src.Substring(to.startOffset - s, to.endOffset - s - (to.startOffset - s))).Append(GetPostTag(subInfo.seqnum));
132 srcIndex = to.endOffset - s;
135 fragment.Append(src.Substring(srcIndex));
136 return fragment.ToString();
163 protected virtual String GetFragmentSource(StringBuilder buffer,
int[] index, String[] values,
int startOffset,
int endOffset)
165 while (buffer.Length < endOffset && index[0] < values.Length)
167 buffer.Append(values[index[0]]);
168 if (values[index[0]].Length > 0 && index[0] + 1 < values.Length)
172 int eo = buffer.Length < endOffset ? buffer.Length : endOffset;
173 return buffer.ToString().Substring(startOffset, eo - startOffset);
176 protected virtual String GetFragmentSource(StringBuilder buffer,
int[] index,
Field[] values,
int startOffset,
int endOffset)
178 while (buffer.Length < endOffset && index[0] < values.Length)
180 buffer.Append(values[index[0]].StringValue);
181 if (values[index[0]].IsTokenized && values[index[0]].StringValue.Length > 0 && index[0] + 1 < values.Length)
185 int eo = buffer.Length < endOffset ? buffer.Length : endOffset;
186 return buffer.ToString().Substring(startOffset, eo - startOffset);
189 protected virtual String GetPreTag(
int num)
191 int n = num % preTags.Length;
195 protected virtual String GetPostTag(
int num)
197 int n = num % postTags.Length;