-
Notifications
You must be signed in to change notification settings - Fork 624
/
WordlistLoader.cs
260 lines (244 loc) · 11.6 KB
/
WordlistLoader.cs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Lucene version compatibility level 4.8.1
using J2N.Text;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Analysis.Util
{
/*
* 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>
/// Loader for text files that represent a list of stopwords.
/// <para/>
/// <see cref="IOUtils"/> to obtain <see cref="TextReader"/> instances.
/// @lucene.internal
/// </summary>
public static class WordlistLoader // LUCENENET specific: CA1052 Static holder types should be Static or NotInheritable
{
private const int INITIAL_CAPACITY = 16;
// LUCENENET specific
private readonly static Regex WHITESPACE = new Regex("\\s+", RegexOptions.Compiled);
// LUCENENET TODO: Add .NET overloads that accept a file name? Or at least a FileInfo object as was done in 3.0.3?
/// <summary>
/// Reads lines from a <see cref="TextReader"/> and adds every line as an entry to a <see cref="CharArraySet"/> (omitting
/// leading and trailing whitespace). Every line of the <see cref="TextReader"/> should contain only
/// one word. The words need to be in lowercase if you make use of an
/// <see cref="Analyzer"/> which uses <see cref="Core.LowerCaseFilter"/> (like <see cref="Standard.StandardAnalyzer"/>).
/// </summary>
/// <param name="reader"> <see cref="TextReader"/> containing the wordlist </param>
/// <param name="result"> the <see cref="CharArraySet"/> to fill with the readers words </param>
/// <returns> the given <see cref="CharArraySet"/> with the reader's words </returns>
public static CharArraySet GetWordSet(TextReader reader, CharArraySet result)
{
try
{
string word = null;
while ((word = reader.ReadLine()) != null)
{
result.Add(word.Trim());
}
}
finally
{
IOUtils.Dispose(reader);
}
return result;
}
/// <summary>
/// Reads lines from a <see cref="TextReader"/> and adds every line as an entry to a <see cref="CharArraySet"/> (omitting
/// leading and trailing whitespace). Every line of the <see cref="TextReader"/> should contain only
/// one word. The words need to be in lowercase if you make use of an
/// <see cref="Analyzer"/> which uses <see cref="Core.LowerCaseFilter"/> (like <see cref="Standard.StandardAnalyzer"/>).
/// </summary>
/// <param name="reader"> <see cref="TextReader"/> containing the wordlist </param>
/// <param name="matchVersion"> the <see cref="LuceneVersion"/> </param>
/// <returns> A <see cref="CharArraySet"/> with the reader's words </returns>
public static CharArraySet GetWordSet(TextReader reader, LuceneVersion matchVersion)
{
return GetWordSet(reader, new CharArraySet(matchVersion, INITIAL_CAPACITY, false));
}
/// <summary>
/// Reads lines from a <see cref="TextReader"/> and adds every non-comment line as an entry to a <see cref="CharArraySet"/> (omitting
/// leading and trailing whitespace). Every line of the <see cref="TextReader"/> should contain only
/// one word. The words need to be in lowercase if you make use of an
/// <see cref="Analyzer"/> which uses <see cref="Core.LowerCaseFilter"/> (like <see cref="Standard.StandardAnalyzer"/>).
/// </summary>
/// <param name="reader"> <see cref="TextReader"/> containing the wordlist </param>
/// <param name="comment"> The string representing a comment. </param>
/// <param name="matchVersion"> the <see cref="LuceneVersion"/> </param>
/// <returns> A CharArraySet with the reader's words </returns>
public static CharArraySet GetWordSet(TextReader reader, string comment, LuceneVersion matchVersion)
{
return GetWordSet(reader, comment, new CharArraySet(matchVersion, INITIAL_CAPACITY, false));
}
/// <summary>
/// Reads lines from a <see cref="TextReader"/> and adds every non-comment line as an entry to a <see cref="CharArraySet"/> (omitting
/// leading and trailing whitespace). Every line of the <see cref="TextReader"/> should contain only
/// one word. The words need to be in lowercase if you make use of an
/// <see cref="Analyzer"/> which uses <see cref="Core.LowerCaseFilter"/> (like <see cref="Standard.StandardAnalyzer"/>).
/// </summary>
/// <param name="reader"> <see cref="TextReader"/> containing the wordlist </param>
/// <param name="comment"> The string representing a comment. </param>
/// <param name="result"> the <see cref="CharArraySet"/> to fill with the readers words </param>
/// <returns> the given <see cref="CharArraySet"/> with the reader's words </returns>
public static CharArraySet GetWordSet(TextReader reader, string comment, CharArraySet result)
{
try
{
string word = null;
while ((word = reader.ReadLine()) != null)
{
if (word.StartsWith(comment, StringComparison.Ordinal) == false)
{
result.Add(word.Trim());
}
}
}
finally
{
IOUtils.Dispose(reader);
}
return result;
}
/// <summary>
/// Reads stopwords from a stopword list in Snowball format.
/// <para>
/// The snowball format is the following:
/// <list type="bullet">
/// <item><description>Lines may contain multiple words separated by whitespace.</description></item>
/// <item><description>The comment character is the vertical line (|).</description></item>
/// <item><description>Lines may contain trailing comments.</description></item>
/// </list>
/// </para>
/// </summary>
/// <param name="reader"> <see cref="TextReader"/> containing a Snowball stopword list </param>
/// <param name="result"> the <see cref="CharArraySet"/> to fill with the readers words </param>
/// <returns> the given <see cref="CharArraySet"/> with the reader's words </returns>
public static CharArraySet GetSnowballWordSet(TextReader reader, CharArraySet result)
{
try
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
int comment = line.IndexOf('|');
if (comment >= 0)
{
line = line.Substring(0, comment);
}
string[] words = WHITESPACE.Split(line).TrimEnd();
foreach (var word in words)
{
if (word.Length > 0)
{
result.Add(word);
}
}
}
}
finally
{
IOUtils.Dispose(reader);
}
return result;
}
/// <summary>
/// Reads stopwords from a stopword list in Snowball format.
/// <para>
/// The snowball format is the following:
/// <list type="bullet">
/// <item><description>Lines may contain multiple words separated by whitespace.</description></item>
/// <item><description>The comment character is the vertical line (|).</description></item>
/// <item><description>Lines may contain trailing comments.</description></item>
/// </list>
/// </para>
/// </summary>
/// <param name="reader"> <see cref="TextReader"/> containing a Snowball stopword list </param>
/// <param name="matchVersion"> the Lucene <see cref="LuceneVersion"/> </param>
/// <returns> A <see cref="CharArraySet"/> with the reader's words </returns>
public static CharArraySet GetSnowballWordSet(TextReader reader, LuceneVersion matchVersion)
{
return GetSnowballWordSet(reader, new CharArraySet(matchVersion, INITIAL_CAPACITY, false));
}
/// <summary>
/// Reads a stem dictionary. Each line contains:
/// <code>word<b>\t</b>stem</code>
/// (i.e. two tab separated words)
/// </summary>
/// <returns> stem dictionary that overrules the stemming algorithm </returns>
/// <exception cref="IOException"> If there is a low-level I/O error. </exception>
public static CharArrayMap<string> GetStemDict(TextReader reader, CharArrayMap<string> result)
{
try
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] wordstem = line.Split(new char[] { '\t' }, 2);
result.Put(wordstem[0], wordstem[1]);
}
}
finally
{
IOUtils.Dispose(reader);
}
return result;
}
/// <summary>
/// Accesses a resource by name and returns the (non comment) lines containing
/// data using the given character encoding.
/// <para>
/// A comment line is any line that starts with the character "#"
/// </para>
/// </summary>
/// <returns> a list of non-blank non-comment lines with whitespace trimmed </returns>
/// <exception cref="IOException"> If there is a low-level I/O error. </exception>
public static IList<string> GetLines(Stream stream, Encoding encoding)
{
IList<string> lines = new JCG.List<string>();
using (StreamReader reader = new StreamReader(stream, encoding))
{
string word;
while ((word = reader.ReadLine()) != null)
{
// skip initial bom marker
if (lines.Count == 0 && word.Length > 0 && word[0] == '\uFEFF')
{
word = word.Substring(1);
}
// skip comments
if (word.StartsWith("#", StringComparison.Ordinal))
{
continue;
}
word = word.Trim();
// skip blank lines
if (word.Length == 0)
{
continue;
}
lines.Add(word);
}
}
return lines;
}
}
}