forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestKoreanTokenizerFactory.cs
166 lines (157 loc) · 6.83 KB
/
TestKoreanTokenizerFactory.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
// Lucene version compatibility level 8.2.0
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Analysis.Ko
{
/*
* 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>
/// Simple tests for <see cref="KoreanTokenizerFactory"/>
/// </summary>
public class TestKoreanTokenizerFactory : BaseTokenStreamTestCase
{
[Test]
public void TestSimple()
{
KoreanTokenizerFactory factory = new KoreanTokenizerFactory(new Dictionary<string, string>());
factory.Inform(new StringMockResourceLoader(""));
TokenStream ts = factory.Create(NewAttributeFactory(), new StringReader("안녕하세요"));
//((Tokenizer) ts).setReader(new StringReader("안녕하세요"));
AssertTokenStreamContents(ts,
new String[] { "안녕", "하", "시", "어요" },
new int[] { 0, 2, 3, 3 },
new int[] { 2, 3, 5, 5 }
);
}
/**
* Test decompoundMode
*/
[Test]
public void TestDiscardDecompound()
{
IDictionary<String, String> args = new JCG.Dictionary<String, String>();
args["decompoundMode"] = "discard";
KoreanTokenizerFactory factory = new KoreanTokenizerFactory(args);
factory.Inform(new StringMockResourceLoader(""));
TokenStream ts = factory.Create(NewAttributeFactory(), new StringReader("갠지스강"));
//((Tokenizer) ts).setReader(new StringReader("갠지스강"));
AssertTokenStreamContents(ts,
new String[] { "갠지스", "강" }
);
}
[Test]
public void TestNoDecompound()
{
IDictionary<String, String> args = new JCG.Dictionary<String, String>();
args["decompoundMode"] = "none";
KoreanTokenizerFactory factory = new KoreanTokenizerFactory(args);
factory.Inform(new StringMockResourceLoader(""));
TokenStream ts = factory.Create(NewAttributeFactory(), new StringReader("갠지스강"));
//((Tokenizer) ts).setReader(new StringReader("갠지스강"));
AssertTokenStreamContents(ts,
new String[] { "갠지스강" }
);
}
[Test]
public void TestMixedDecompound()
{
IDictionary<String, String> args = new JCG.Dictionary<String, String>();
args["decompoundMode"] = "mixed";
KoreanTokenizerFactory factory = new KoreanTokenizerFactory(args);
factory.Inform(new StringMockResourceLoader(""));
TokenStream ts = factory.Create(NewAttributeFactory(), new StringReader("갠지스강"));
//((Tokenizer) ts).setReader(new StringReader("갠지스강"));
AssertTokenStreamContents(ts,
new String[] { "갠지스강", "갠지스", "강" }
);
}
/**
* Test user dictionary
*/
[Test]
public void TestUserDict()
{
String userDict =
"# Additional nouns\n" +
"세종시 세종 시\n" +
"# \n" +
"c++\n";
IDictionary<String, String> args = new JCG.Dictionary<String, String>();
args["userDictionary"] = "userdict.txt";
KoreanTokenizerFactory factory = new KoreanTokenizerFactory(args);
factory.Inform(new StringMockResourceLoader(userDict));
TokenStream ts = factory.Create(NewAttributeFactory(), new StringReader("세종시"));
((Tokenizer)ts).SetReader(new StringReader("세종시"));
//((Tokenizer) ts).setReader(new StringReader("세종시"));
AssertTokenStreamContents(ts,
new String[] { "세종", "시" }
);
}
/**
* Test discardPunctuation True
*/
[Test]
public void TestDiscardPunctuation_true()
{
IDictionary<String, String> args = new JCG.Dictionary<String, String>();
args["discardPunctuation"] = "true";
KoreanTokenizerFactory factory = new KoreanTokenizerFactory(args);
factory.Inform(new StringMockResourceLoader(""));
TokenStream ts = factory.Create(NewAttributeFactory(), new StringReader("10.1 인치 모니터"));
//((Tokenizer) ts).setReader(new StringReader("10.1 인치 모니터"));
AssertTokenStreamContents(ts,
new String[] { "10", "1", "인치", "모니터" }
);
}
/**
* Test discardPunctuation False
*/
[Test]
public void TestDiscardPunctuation_false()
{
IDictionary<String, String> args = new JCG.Dictionary<String, String>();
args["discardPunctuation"] = "false";
KoreanTokenizerFactory factory = new KoreanTokenizerFactory(args);
factory.Inform(new StringMockResourceLoader(""));
TokenStream ts = factory.Create(NewAttributeFactory(), new StringReader("10.1 인치 모니터"));
//((Tokenizer) ts).setReader(new StringReader("10.1 인치 모니터"));
AssertTokenStreamContents(ts,
new String[] { "10", ".", "1", " ", "인치", " ", "모니터" }
);
}
/** Test that bogus arguments result in exception */
[Test]
public void TestBogusArguments()
{
ArgumentException expected = Assert.Throws<ArgumentException>(() =>
{
new KoreanTokenizerFactory(new JCG.Dictionary<String, String>() {
{ "bogusArg", "bogusValue" }
});
});
//IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
// new KoreanTokenizerFactory(new JCG.Dictionary<String, String>() {{
// put("bogusArg", "bogusValue");
// }});
//});
assertTrue(expected.Message.Contains("Unknown parameters"));
}
}
}