forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradientFormatter.cs
More file actions
246 lines (227 loc) · 9.96 KB
/
Copy pathGradientFormatter.cs
File metadata and controls
246 lines (227 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Text;
using Integer = J2N.Numerics.Int32;
namespace Lucene.Net.Search.Highlight
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// Formats text with different color intensity depending on the score of the term.
/// </summary>
public class GradientFormatter : IFormatter
{
private readonly float maxScore; // LUCENENET: marked readonly
protected int m_fgRMin, m_fgGMin, m_fgBMin;
protected int m_fgRMax, m_fgGMax, m_fgBMax;
protected bool m_highlightForeground;
protected int m_bgRMin, m_bgGMin, m_bgBMin;
protected int m_bgRMax, m_bgGMax, m_bgBMax;
protected bool m_highlightBackground;
/// <summary> Sets the color range for the IDF scores</summary>
/// <param name="maxScore">
/// The score (and above) displayed as maxColor (See <see cref="QueryScorer.MaxTermWeight"/>
/// which can be used to calibrate scoring scale)
/// </param>
/// <param name="minForegroundColor">
/// The hex color used for representing IDF scores of zero eg
/// #FFFFFF (white) or null if no foreground color required
/// </param>
/// <param name="maxForegroundColor">
/// The largest hex color used for representing IDF scores eg
/// #000000 (black) or null if no foreground color required
/// </param>
/// <param name="minBackgroundColor">
/// The hex color used for representing IDF scores of zero eg
/// #FFFFFF (white) or null if no background color required
/// </param>
/// <param name="maxBackgroundColor">
/// The largest hex color used for representing IDF scores eg
/// #000000 (black) or null if no background color required
/// </param>
public GradientFormatter(float maxScore, string minForegroundColor,
string maxForegroundColor, string minBackgroundColor,
string maxBackgroundColor)
{
m_highlightForeground = (minForegroundColor != null) && (maxForegroundColor != null);
if (m_highlightForeground)
{
if (minForegroundColor.Length != 7)
{
throw new ArgumentException("minForegroundColor is not 7 bytes long eg a hex "
+ "RGB value such as #FFFFFF");
}
if (maxForegroundColor.Length != 7)
{
throw new ArgumentException("minForegroundColor is not 7 bytes long eg a hex "
+ "RGB value such as #FFFFFF");
}
m_fgRMin = HexToInt32(minForegroundColor, 1, 3 - 1);
m_fgGMin = HexToInt32(minForegroundColor, 3, 5 - 3);
m_fgBMin = HexToInt32(minForegroundColor, 5, 7 - 5);
m_fgRMax = HexToInt32(maxForegroundColor, 1, 3 - 1);
m_fgGMax = HexToInt32(maxForegroundColor, 3, 5 - 3);
m_fgBMax = HexToInt32(maxForegroundColor, 5, 7 - 5);
}
m_highlightBackground = (minBackgroundColor != null)
&& (maxBackgroundColor != null);
if (m_highlightBackground)
{
if (minBackgroundColor.Length != 7)
{
throw new ArgumentException("minBackgroundColor is not 7 bytes long eg a hex "
+ "RGB value such as #FFFFFF");
}
if (maxBackgroundColor.Length != 7)
{
throw new ArgumentException("minBackgroundColor is not 7 bytes long eg a hex "
+ "RGB value such as #FFFFFF");
}
m_bgRMin = HexToInt32(minBackgroundColor, 1, 3 - 1);
m_bgGMin = HexToInt32(minBackgroundColor, 3, 5 - 3);
m_bgBMin = HexToInt32(minBackgroundColor, 5, 7 - 5);
m_bgRMax = HexToInt32(maxBackgroundColor, 1, 3 - 1);
m_bgGMax = HexToInt32(maxBackgroundColor, 3, 5 - 3);
m_bgBMax = HexToInt32(maxBackgroundColor, 5, 7 - 5);
}
// this.corpusReader = corpusReader;
this.maxScore = maxScore;
// totalNumDocs = corpusReader.numDocs();
}
public virtual string HighlightTerm(string originalText, TokenGroup tokenGroup)
{
if (tokenGroup.TotalScore == 0)
return originalText;
float score = tokenGroup.TotalScore;
if (score == 0)
{
return originalText;
}
var sb = new StringBuilder();
sb.Append("<font ");
if (m_highlightForeground)
{
sb.Append("color=\"");
sb.Append(GetForegroundColorString(score));
sb.Append("\" ");
}
if (m_highlightBackground)
{
sb.Append("bgcolor=\"");
sb.Append(GetBackgroundColorString(score));
sb.Append("\" ");
}
sb.Append('>');
sb.Append(originalText);
sb.Append("</font>");
return sb.ToString();
}
protected internal virtual string GetForegroundColorString(float score)
{
int rVal = GetColorVal(m_fgRMin, m_fgRMax, score);
int gVal = GetColorVal(m_fgGMin, m_fgGMax, score);
int bVal = GetColorVal(m_fgBMin, m_fgBMax, score);
var sb = new StringBuilder();
sb.Append('#');
sb.Append(Int32ToHex(rVal));
sb.Append(Int32ToHex(gVal));
sb.Append(Int32ToHex(bVal));
return sb.ToString();
}
protected internal virtual string GetBackgroundColorString(float score)
{
int rVal = GetColorVal(m_bgRMin, m_bgRMax, score);
int gVal = GetColorVal(m_bgGMin, m_bgGMax, score);
int bVal = GetColorVal(m_bgBMin, m_bgBMax, score);
var sb = new StringBuilder();
sb.Append('#');
sb.Append(Int32ToHex(rVal));
sb.Append(Int32ToHex(gVal));
sb.Append(Int32ToHex(bVal));
return sb.ToString();
}
private int GetColorVal(int colorMin, int colorMax, float score)
{
if (colorMin == colorMax)
{
return colorMin;
}
float scale = Math.Abs(colorMin - colorMax);
float relScorePercent = Math.Min(maxScore, score) / maxScore;
float colScore = scale * relScorePercent;
return Math.Min(colorMin, colorMax) + (int)colScore;
}
private static readonly char[] hexDigits = new char[] { // LUCENENET: marked readonly
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
/// <summary>
/// NOTE: This was intToHex() in Lucene
/// </summary>
private static string Int32ToHex(int i)
{
return "" + hexDigits[(i & 0xF0) >> 4] + hexDigits[i & 0x0F];
}
/// <summary>
/// Converts a hex string into an <see cref="int"/>.
/// <para/>
/// NOTE: This was hexToInt() in Lucene
/// </summary>
/// <param name="hex">
/// A string in capital or lower case hex, of no more then 16
/// characters.
/// </param>
/// <exception cref="FormatException">if the string is more than 16 characters long, or if any
/// character is not in the set [0-9a-fA-f]</exception>
public static int HexToInt32(string hex)
{
if ((hex.Length > 16) || !Integer.TryParse(hex, radix: 16, out int result))
{
throw NumberFormatException.Create();
}
return result;
}
/// <summary>
/// Converts a hex string at the specified index and length of <paramref name="hex"/>
/// into an <see cref="int"/>.
/// <para/>
/// NOTE: This was hexToInt() in Lucene
/// </summary>
/// <param name="hex">
/// A string in capital or lower case hex, of no more then 16
/// characters.
/// </param>
/// <param name="startIndex">The index of the first character to begin parsing.</param>
/// <param name="length">The number of characters to parse.</param>
/// <exception cref="FormatException">if the string is more than 16 characters long, or if any
/// character is not in the set [0-9a-fA-f]</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="startIndex"/> or <paramref name="length"/> is less than zero.
/// <para/>
/// -or-
/// <para/>
/// <paramref name="startIndex"/> and <paramref name="length"/> refer to a location outside of <paramref name="hex"/>.
/// </exception>
// LUCENENET specific overload
public static int HexToInt32(string hex, int startIndex, int length)
{
if ((length > 16) || !Integer.TryParse(hex, startIndex, length, radix: 16, out int result))
{
throw NumberFormatException.Create();
}
return result;
}
}
}