forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
StopwordAnalyzerBase.cs
157 lines (148 loc) · 6.41 KB
/
StopwordAnalyzerBase.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
// Lucene version compatibility level 4.8.1
using J2N;
using Lucene.Net.Util;
using System;
using System.IO;
using System.Reflection;
using System.Text;
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>
/// Base class for <see cref="Analyzer"/>s that need to make use of stopword sets.
/// </summary>
public abstract class StopwordAnalyzerBase : Analyzer
{
/// <summary>
/// An immutable stopword set
/// </summary>
protected readonly CharArraySet m_stopwords;
protected readonly LuceneVersion m_matchVersion;
/// <summary>
/// Returns the analyzer's stopword set or an empty set if the analyzer has no
/// stopwords
/// </summary>
/// <returns> the analyzer's stopword set or an empty set if the analyzer has no
/// stopwords </returns>
public virtual CharArraySet StopwordSet => m_stopwords;
/// <summary>
/// Creates a new instance initialized with the given stopword set
/// </summary>
/// <param name="version">
/// the Lucene version for cross version compatibility </param>
/// <param name="stopwords">
/// the analyzer's stopword set </param>
protected StopwordAnalyzerBase(LuceneVersion version, CharArraySet stopwords)
{
m_matchVersion = version;
// analyzers should use char array set for stopwords!
this.m_stopwords = stopwords is null ? CharArraySet.EMPTY_SET : CharArraySet.UnmodifiableSet(CharArraySet.Copy(version, stopwords));
}
/// <summary>
/// Creates a new <see cref="Analyzer"/> with an empty stopword set
/// </summary>
/// <param name="version">
/// the Lucene version for cross version compatibility </param>
protected StopwordAnalyzerBase(LuceneVersion version)
: this(version, null)
{
}
/// <summary>
/// Creates a <see cref="CharArraySet"/> from an embedded resource associated with a class. (See
/// <see cref="Assembly.GetManifestResourceStream(string)"/>).
/// </summary>
/// <param name="ignoreCase">
/// <c>true</c> if the set should ignore the case of the
/// stopwords, otherwise <c>false</c> </param>
/// <param name="aClass">
/// a class that is associated with the given stopwordResource </param>
/// <param name="resource">
/// name of the resource file associated with the given class </param>
/// <param name="comment">
/// comment string to ignore in the stopword file </param>
/// <returns> a <see cref="CharArraySet"/> containing the distinct stopwords from the given
/// file </returns>
/// <exception cref="IOException">
/// if loading the stopwords throws an <see cref="IOException"/> </exception>
protected static CharArraySet LoadStopwordSet(bool ignoreCase, Type aClass, string resource, string comment)
{
TextReader reader = null;
try
{
var resourceStream = aClass.FindAndGetManifestResourceStream(resource);
reader = IOUtils.GetDecodingReader(resourceStream, Encoding.UTF8);
return WordlistLoader.GetWordSet(reader, comment, new CharArraySet(
#pragma warning disable 612, 618
LuceneVersion.LUCENE_CURRENT, 16, ignoreCase));
#pragma warning restore 612, 618
}
finally
{
IOUtils.Dispose(reader);
}
}
/// <summary>
/// Creates a <see cref="CharArraySet"/> from a file.
/// </summary>
/// <param name="stopwords">
/// the stopwords file to load
/// </param>
/// <param name="matchVersion">
/// the Lucene version for cross version compatibility </param>
/// <returns> a <see cref="CharArraySet"/> containing the distinct stopwords from the given
/// file </returns>
/// <exception cref="IOException">
/// if loading the stopwords throws an <see cref="IOException"/> </exception>
protected static CharArraySet LoadStopwordSet(FileInfo stopwords, LuceneVersion matchVersion)
{
TextReader reader = null;
try
{
reader = IOUtils.GetDecodingReader(stopwords, Encoding.UTF8);
return WordlistLoader.GetWordSet(reader, matchVersion);
}
finally
{
IOUtils.Dispose(reader);
}
}
/// <summary>
/// Creates a <see cref="CharArraySet"/> from a file.
/// </summary>
/// <param name="stopwords">
/// the stopwords reader to load
/// </param>
/// <param name="matchVersion">
/// the Lucene version for cross version compatibility </param>
/// <returns> a <see cref="CharArraySet"/> containing the distinct stopwords from the given
/// reader </returns>
/// <exception cref="IOException">
/// if loading the stopwords throws an <see cref="IOException"/> </exception>
protected static CharArraySet LoadStopwordSet(TextReader stopwords, LuceneVersion matchVersion)
{
try
{
return WordlistLoader.GetWordSet(stopwords, matchVersion);
}
finally
{
IOUtils.Dispose(stopwords);
}
}
}
}