forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidatingTokenFilter.cs
181 lines (155 loc) · 6.64 KB
/
ValidatingTokenFilter.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
using Lucene.Net.Analysis.TokenAttributes;
using System;
using System.Collections.Generic;
namespace Lucene.Net.Analysis
{
/*
* 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.
*/
// TODO: rename to OffsetsXXXTF? ie we only validate
// offsets (now anyway...)
// TODO: also make a DebuggingTokenFilter, that just prints
// all att values that come through it...
// TODO: BTSTC should just append this to the chain
// instead of checking itself:
/// <summary>
/// A <see cref="TokenFilter"/> that checks consistency of the tokens (eg
/// offsets are consistent with one another).
/// </summary>
public sealed class ValidatingTokenFilter : TokenFilter
{
private int pos;
private int lastStartOffset;
// Maps position to the start/end offset:
private readonly IDictionary<int, int> posToStartOffset = new Dictionary<int, int>();
private readonly IDictionary<int, int> posToEndOffset = new Dictionary<int, int>();
private readonly PositionIncrementAttribute posIncAtt;
private readonly PositionLengthAttribute posLenAtt;
private readonly OffsetAttribute offsetAtt;
private readonly CharTermAttribute termAtt;
private readonly bool offsetsAreCorrect;
private readonly string name;
// Returns null if the attr wasn't already added
private A GetAttrIfExists<A>() where A : Lucene.Net.Util.Attribute
{
if (HasAttribute<A>())
{
return GetAttribute<A>();
}
else
{
return default;
}
}
/// <summary>
/// The <paramref name="name"/> is used to identify this stage when
/// throwing exceptions (useful if you have more than one
/// instance in your chain).
/// </summary>
public ValidatingTokenFilter(TokenStream @in, string name, bool offsetsAreCorrect)
: base(@in)
{
posIncAtt = GetAttrIfExists<PositionIncrementAttribute>();
posLenAtt = GetAttrIfExists<PositionLengthAttribute>();
offsetAtt = GetAttrIfExists<OffsetAttribute>();
termAtt = GetAttrIfExists<CharTermAttribute>();
this.name = name;
this.offsetsAreCorrect = offsetsAreCorrect;
}
public override bool IncrementToken()
{
if (!m_input.IncrementToken())
{
return false;
}
int startOffset = 0;
int endOffset = 0;
int posLen; // LUCENENET: IDE0059: Remove unnecessary value assignment
if (posIncAtt != null)
{
pos += posIncAtt.PositionIncrement;
if (pos == -1)
{
throw IllegalStateException.Create("first posInc must be > 0");
}
}
// System.out.println(" got token=" + termAtt + " pos=" + pos);
if (offsetAtt != null)
{
startOffset = offsetAtt.StartOffset;
endOffset = offsetAtt.EndOffset;
if (offsetsAreCorrect && offsetAtt.StartOffset < lastStartOffset)
{
throw IllegalStateException.Create(name + ": offsets must not go backwards startOffset=" + startOffset + " is < lastStartOffset=" + lastStartOffset);
}
lastStartOffset = offsetAtt.StartOffset;
}
posLen = posLenAtt is null ? 1 : posLenAtt.PositionLength;
if (offsetAtt != null && posIncAtt != null && offsetsAreCorrect)
{
if (!posToStartOffset.TryGetValue(pos, out int oldStartOffset))
{
// First time we've seen a token leaving from this position:
posToStartOffset[pos] = startOffset;
//System.out.println(" + s " + pos + " -> " + startOffset);
}
else
{
// We've seen a token leaving from this position
// before; verify the startOffset is the same:
//System.out.println(" + vs " + pos + " -> " + startOffset);
if (oldStartOffset != startOffset)
{
throw IllegalStateException.Create(name + ": inconsistent startOffset at pos=" + pos + ": " + oldStartOffset + " vs " + startOffset + "; token=" + termAtt);
}
}
int endPos = pos + posLen;
if (!posToEndOffset.TryGetValue(endPos, out int oldEndOffset))
{
// First time we've seen a token arriving to this position:
posToEndOffset[endPos] = endOffset;
//System.out.println(" + e " + endPos + " -> " + endOffset);
}
else
{
// We've seen a token arriving to this position
// before; verify the endOffset is the same:
//System.out.println(" + ve " + endPos + " -> " + endOffset);
if (oldEndOffset != endOffset)
{
throw IllegalStateException.Create(name + ": inconsistent endOffset at pos=" + endPos + ": " + oldEndOffset + " vs " + endOffset + "; token=" + termAtt);
}
}
}
return true;
}
public override void End()
{
base.End();
// TODO: what else to validate
// TODO: check that endOffset is >= max(endOffset)
// we've seen
}
public override void Reset()
{
base.Reset();
pos = -1;
posToStartOffset.Clear();
posToEndOffset.Clear();
lastStartOffset = 0;
}
}
}