-
Notifications
You must be signed in to change notification settings - Fork 623
/
JapaneseAnalyzer.cs
117 lines (107 loc) · 4.99 KB
/
JapaneseAnalyzer.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
using Lucene.Net.Analysis.Cjk;
using Lucene.Net.Analysis.Core;
using Lucene.Net.Analysis.Ja.Dict;
using Lucene.Net.Analysis.Util;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.IO;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Analysis.Ja
{
/*
* 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>
/// Analyzer for Japanese that uses morphological analysis.
/// </summary>
/// <seealso cref="JapaneseTokenizer"/>
public class JapaneseAnalyzer : StopwordAnalyzerBase
{
private readonly JapaneseTokenizerMode mode;
private readonly ISet<string> stoptags;
private readonly UserDictionary userDict;
public JapaneseAnalyzer(LuceneVersion matchVersion)
: this(matchVersion, null, JapaneseTokenizer.DEFAULT_MODE, DefaultSetHolder.DEFAULT_STOP_SET, DefaultSetHolder.DEFAULT_STOP_TAGS)
{
}
public JapaneseAnalyzer(LuceneVersion matchVersion, UserDictionary userDict, JapaneseTokenizerMode mode, CharArraySet stopwords, ISet<string> stoptags)
: base(matchVersion, stopwords)
{
this.userDict = userDict;
this.mode = mode;
this.stoptags = stoptags;
}
public static CharArraySet GetDefaultStopSet()
{
return DefaultSetHolder.DEFAULT_STOP_SET;
}
public static ISet<string> GetDefaultStopTags()
{
return DefaultSetHolder.DEFAULT_STOP_TAGS;
}
/// <summary>
/// Atomically loads DEFAULT_STOP_SET, DEFAULT_STOP_TAGS in a lazy fashion once the
/// outer class accesses the static final set the first time.
/// </summary>
private static class DefaultSetHolder
{
internal static readonly CharArraySet DEFAULT_STOP_SET = LoadDefaultStopSet();
internal static readonly ISet<string> DEFAULT_STOP_TAGS = LoadDefaultStopTagSet();
private static CharArraySet LoadDefaultStopSet() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006)
{
try
{
return LoadStopwordSet(true, typeof(JapaneseAnalyzer), "stopwords.txt", "#"); // ignore case
}
catch (Exception ex) when (ex.IsIOException())
{
// default set should always be present as it is part of the distribution (JAR)
throw RuntimeException.Create("Unable to load default stopword set", ex);
}
}
private static ISet<string> LoadDefaultStopTagSet() // LUCENENET: Avoid static constructors (see https://github.com/apache/lucenenet/pull/224#issuecomment-469284006)
{
try
{
CharArraySet tagset = LoadStopwordSet(false, typeof(JapaneseAnalyzer), "stoptags.txt", "#");
var DEFAULT_STOP_TAGS = new JCG.HashSet<string>();
foreach (string element in tagset)
{
DEFAULT_STOP_TAGS.Add(element);
}
return DEFAULT_STOP_TAGS;
}
catch (Exception ex) when (ex.IsIOException())
{
// default set should always be present as it is part of the distribution (JAR)
throw RuntimeException.Create("Unable to load default stoptag set", ex);
}
}
}
protected internal override TokenStreamComponents CreateComponents(string fieldName, TextReader reader)
{
Tokenizer tokenizer = new JapaneseTokenizer(reader, userDict, true, mode);
TokenStream stream = new JapaneseBaseFormFilter(tokenizer);
stream = new JapanesePartOfSpeechStopFilter(m_matchVersion, stream, stoptags);
stream = new CJKWidthFilter(stream);
stream = new StopFilter(m_matchVersion, stream, m_stopwords);
stream = new JapaneseKatakanaStemFilter(stream);
stream = new LowerCaseFilter(m_matchVersion, stream);
return new TokenStreamComponents(tokenizer, stream);
}
}
}