forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhoneticFilter.cs
109 lines (96 loc) · 3.95 KB
/
PhoneticFilter.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
// lucene version compatibility level: 4.8.1
using Lucene.Net.Analysis.Phonetic.Language;
using Lucene.Net.Analysis.TokenAttributes;
using System;
namespace Lucene.Net.Analysis.Phonetic
{
/*
* 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>
/// Create tokens for phonetic matches.
/// See the Language namespace.
/// </summary>
public sealed class PhoneticFilter : TokenFilter
{
/// <summary>true if encoded tokens should be added as synonyms</summary>
private readonly bool inject = true; // LUCENENET: marked readonly
/// <summary>phonetic encoder</summary>
private readonly IStringEncoder encoder = null; // LUCENENET: marked readonly
/// <summary>captured state, non-null when <c>inject=true</c> and a token is buffered</summary>
private State save = null;
private readonly ICharTermAttribute termAtt;
private readonly IPositionIncrementAttribute posAtt;
/// <summary>
/// Creates a <see cref="PhoneticFilter"/> with the specified encoder, and either
/// adding encoded forms as synonyms (<c>inject=true</c>) or
/// replacing them.
/// </summary>
public PhoneticFilter(TokenStream input, IStringEncoder encoder, bool inject)
: base(input)
{
this.encoder = encoder;
this.inject = inject;
this.termAtt = AddAttribute<ICharTermAttribute>();
this.posAtt = AddAttribute<IPositionIncrementAttribute>();
}
public override bool IncrementToken()
{
if (save != null)
{
// clearAttributes(); // not currently necessary
RestoreState(save);
save = null;
return true;
}
if (!m_input.IncrementToken()) return false;
// pass through zero-length terms
if (termAtt.Length == 0) return true;
string value = termAtt.ToString();
string phonetic = null;
try
{
string v = encoder.Encode(value);
if (v.Length > 0 && !value.Equals(v, StringComparison.Ordinal))
{
phonetic = v;
}
}
catch (Exception) { /* ignored */ } // just use the direct text
if (phonetic == null) return true;
if (!inject)
{
// just modify this token
termAtt.SetEmpty().Append(phonetic);
return true;
}
// We need to return both the original and the phonetic tokens.
// to avoid a orig=captureState() change_to_phonetic() saved=captureState() restoreState(orig)
// we return the phonetic alternative first
int origOffset = posAtt.PositionIncrement;
posAtt.PositionIncrement = 0;
save = CaptureState();
posAtt.PositionIncrement = origOffset;
termAtt.SetEmpty().Append(phonetic);
return true;
}
public override void Reset()
{
m_input.Reset();
save = null;
}
}
}