forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
StandardTokenizer.cs
267 lines (245 loc) · 9.65 KB
/
StandardTokenizer.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
261
262
263
264
265
266
267
// Lucene version compatibility level 4.8.1
using Lucene.Net.Analysis.Standard.Std31;
using Lucene.Net.Analysis.Standard.Std34;
using Lucene.Net.Analysis.Standard.Std40;
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Util;
using System;
using System.IO;
namespace Lucene.Net.Analysis.Standard
{
/*
* 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>
/// A grammar-based tokenizer constructed with JFlex.
/// <para>
/// As of Lucene version 3.1, this class implements the Word Break rules from the
/// Unicode Text Segmentation algorithm, as specified in
/// <a href="http://unicode.org/reports/tr29/">Unicode Standard Annex #29</a>.
/// <p/>
/// </para>
/// <para>Many applications have specific tokenizer needs. If this tokenizer does
/// not suit your application, please consider copying this source code
/// directory to your project and maintaining your own grammar-based tokenizer.
///
/// </para>
/// <para>You must specify the required <see cref="LuceneVersion"/>
/// compatibility when creating <see cref="StandardTokenizer"/>:
/// <list type="bullet">
/// <item><description> As of 3.4, Hiragana and Han characters are no longer wrongly split
/// from their combining characters. If you use a previous version number,
/// you get the exact broken behavior for backwards compatibility.</description></item>
/// <item><description> As of 3.1, StandardTokenizer implements Unicode text segmentation.
/// If you use a previous version number, you get the exact behavior of
/// <see cref="ClassicTokenizer"/> for backwards compatibility.</description></item>
/// </list>
/// </para>
/// </summary>
public sealed class StandardTokenizer : Tokenizer
{
/// <summary>
/// A private instance of the JFlex-constructed scanner </summary>
private IStandardTokenizerInterface scanner;
public const int ALPHANUM = 0;
/// @deprecated (3.1)
[Obsolete("(3.1)")]
public const int APOSTROPHE = 1;
/// @deprecated (3.1)
[Obsolete("(3.1)")]
public const int ACRONYM = 2;
/// @deprecated (3.1)
[Obsolete("(3.1)")]
public const int COMPANY = 3;
public const int EMAIL = 4;
/// @deprecated (3.1)
[Obsolete("(3.1)")]
public const int HOST = 5;
public const int NUM = 6;
/// @deprecated (3.1)
[Obsolete("(3.1)")]
public const int CJ = 7;
/// @deprecated (3.1)
[Obsolete("(3.1)")]
public const int ACRONYM_DEP = 8;
public const int SOUTHEAST_ASIAN = 9;
public const int IDEOGRAPHIC = 10;
public const int HIRAGANA = 11;
public const int KATAKANA = 12;
public const int HANGUL = 13;
/// <summary>
/// String token types that correspond to token type int constants </summary>
public static readonly string[] TOKEN_TYPES = {
"<ALPHANUM>",
"<APOSTROPHE>",
"<ACRONYM>",
"<COMPANY>",
"<EMAIL>",
"<HOST>",
"<NUM>",
"<CJ>",
"<ACRONYM_DEP>",
"<SOUTHEAST_ASIAN>",
"<IDEOGRAPHIC>",
"<HIRAGANA>",
"<KATAKANA>",
"<HANGUL>"
};
private int skippedPositions;
private int maxTokenLength = StandardAnalyzer.DEFAULT_MAX_TOKEN_LENGTH;
/// <summary>
/// Set the max allowed token length. Any token longer
/// than this is skipped.
/// </summary>
public int MaxTokenLength
{
get => maxTokenLength;
set
{
if (value < 1)
{
throw new ArgumentException("maxTokenLength must be greater than zero");
}
this.maxTokenLength = value;
}
}
/// <summary>
/// Creates a new instance of the <see cref="StandardTokenizer"/>. Attaches
/// the <paramref name="input"/> to the newly created JFlex-generated (then ported to .NET) scanner.
/// </summary>
/// <param name="matchVersion"> Lucene compatibility version - See <see cref="StandardTokenizer"/> </param>
/// <param name="input"> The input reader
///
/// See http://issues.apache.org/jira/browse/LUCENE-1068 </param>
public StandardTokenizer(LuceneVersion matchVersion, TextReader input)
: base(input)
{
Init(matchVersion);
}
/// <summary>
/// Creates a new <see cref="StandardTokenizer"/> with a given <see cref="AttributeSource.AttributeFactory"/>
/// </summary>
public StandardTokenizer(LuceneVersion matchVersion, AttributeFactory factory, TextReader input)
: base(factory, input)
{
Init(matchVersion);
}
private void Init(LuceneVersion matchVersion)
{
#pragma warning disable 612, 618
if (matchVersion.OnOrAfter(LuceneVersion.LUCENE_47))
{
this.scanner = new StandardTokenizerImpl(m_input);
}
else if (matchVersion.OnOrAfter(LuceneVersion.LUCENE_40))
{
this.scanner = new StandardTokenizerImpl40(m_input);
}
else if (matchVersion.OnOrAfter(LuceneVersion.LUCENE_34))
{
this.scanner = new StandardTokenizerImpl34(m_input);
}
else if (matchVersion.OnOrAfter(LuceneVersion.LUCENE_31))
{
this.scanner = new StandardTokenizerImpl31(m_input);
}
#pragma warning restore 612, 618
else
{
this.scanner = new ClassicTokenizerImpl(m_input);
}
termAtt = AddAttribute<ICharTermAttribute>();
posIncrAtt = AddAttribute<IPositionIncrementAttribute>();
offsetAtt = AddAttribute<IOffsetAttribute>();
typeAtt = AddAttribute<ITypeAttribute>();
}
// this tokenizer generates three attributes:
// term offset, positionIncrement and type
private ICharTermAttribute termAtt;
private IOffsetAttribute offsetAtt;
private IPositionIncrementAttribute posIncrAtt;
private ITypeAttribute typeAtt;
/*
* (non-Javadoc)
*
* @see org.apache.lucene.analysis.TokenStream#next()
*/
public override sealed bool IncrementToken()
{
ClearAttributes();
skippedPositions = 0;
while (true)
{
int tokenType = scanner.GetNextToken();
if (tokenType == StandardTokenizerInterface.YYEOF)
{
return false;
}
if (scanner.YyLength <= maxTokenLength)
{
posIncrAtt.PositionIncrement = skippedPositions + 1;
scanner.GetText(termAtt);
int start = scanner.YyChar;
offsetAtt.SetOffset(CorrectOffset(start), CorrectOffset(start + termAtt.Length));
// This 'if' should be removed in the next release. For now, it converts
// invalid acronyms to HOST. When removed, only the 'else' part should
// remain.
#pragma warning disable 612, 618
if (tokenType == StandardTokenizer.ACRONYM_DEP)
{
typeAtt.Type = StandardTokenizer.TOKEN_TYPES[StandardTokenizer.HOST];
#pragma warning restore 612, 618
termAtt.Length = termAtt.Length - 1; // remove extra '.'
}
else
{
typeAtt.Type = StandardTokenizer.TOKEN_TYPES[tokenType];
}
return true;
}
else
// When we skip a too-long term, we still increment the
// position increment
{
skippedPositions++;
}
}
}
public override sealed void End()
{
base.End();
// set final offset
int finalOffset = CorrectOffset(scanner.YyChar + scanner.YyLength);
offsetAtt.SetOffset(finalOffset, finalOffset);
// adjust any skipped tokens
posIncrAtt.PositionIncrement = posIncrAtt.PositionIncrement + skippedPositions;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
scanner.YyReset(m_input);
}
}
public override void Reset()
{
base.Reset();
scanner.YyReset(m_input);
skippedPositions = 0;
}
}
}