forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MockGraphTokenFilter.cs
140 lines (124 loc) · 4.92 KB
/
MockGraphTokenFilter.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
using Lucene.Net.Analysis.TokenAttributes;
using Lucene.Net.Util;
using RandomizedTesting.Generators;
using System;
using Console = Lucene.Net.Util.SystemConsole;
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: sometimes remove tokens too...?
/// <summary>
/// Randomly inserts overlapped (posInc=0) tokens with
/// posLength sometimes > 1. The chain must have
/// an <see cref="IOffsetAttribute"/>.
/// </summary>
public sealed class MockGraphTokenFilter : LookaheadTokenFilter<LookaheadTokenFilter.Position>
{
#pragma warning disable CA1802 // Use literals where appropriate
new private static readonly bool DEBUG = false;
#pragma warning restore CA1802 // Use literals where appropriate
private readonly ICharTermAttribute termAtt;
private readonly long seed;
private Random random;
public MockGraphTokenFilter(Random random, TokenStream input)
: base(input)
{
seed = random.NextInt64();
termAtt = AddAttribute<ICharTermAttribute>();
}
protected override Position NewPosition()
{
return new Position();
}
protected override void AfterPosition()
{
if (DEBUG)
{
Console.WriteLine("MockGraphTF.afterPos");
}
if (random.Next(7) == 5)
{
int posLength = TestUtil.NextInt32(random, 1, 5);
if (DEBUG)
{
Console.WriteLine(" do insert! posLen=" + posLength);
}
Position posEndData = m_positions.Get(m_outputPos + posLength);
// Look ahead as needed until we figure out the right
// endOffset:
while (!m_end && posEndData.EndOffset == -1 && m_inputPos <= (m_outputPos + posLength))
{
if (!PeekToken())
{
break;
}
}
if (posEndData.EndOffset != -1)
{
// Notify super class that we are injecting a token:
InsertToken();
ClearAttributes();
m_posLenAtt.PositionLength = posLength;
termAtt.Append(TestUtil.RandomUnicodeString(random));
m_posIncAtt.PositionIncrement = 0;
m_offsetAtt.SetOffset(m_positions.Get(m_outputPos).StartOffset, posEndData.EndOffset);
if (DEBUG)
{
Console.WriteLine(" inject: outputPos=" + m_outputPos + " startOffset=" + m_offsetAtt.StartOffset + " endOffset=" + m_offsetAtt.EndOffset + " posLength=" + m_posLenAtt.PositionLength);
}
// TODO: set TypeAtt too?
}
else
{
// Either 1) the tokens ended before our posLength,
// or 2) our posLength ended inside a hole from the
// input. In each case we just skip the inserted
// token.
}
}
}
public override void Reset()
{
base.Reset();
// NOTE: must be "deterministically random" because
// baseTokenStreamTestCase pulls tokens twice on the
// same input and asserts they are the same:
this.random = new J2N.Randomizer(seed);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
base.Dispose(disposing);
this.random = null;
}
}
public override bool IncrementToken()
{
if (DEBUG)
{
Console.WriteLine("MockGraphTF.incr inputPos=" + m_inputPos + " outputPos=" + m_outputPos);
}
if (random is null)
{
throw IllegalStateException.Create("IncrementToken() called in wrong state!");
}
return NextToken();
}
}
}