| 1 |
package morfologik.stemming; |
| 2 |
|
| 3 |
import static org.assertj.core.api.Assertions.*; |
| 4 |
import static org.junit.Assert.*; |
| 5 |
|
| 6 |
import java.io.IOException; |
| 7 |
import java.net.URL; |
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.Collections; |
| 10 |
import java.util.HashSet; |
| 11 |
import java.util.LinkedHashMap; |
| 12 |
|
| 13 |
import morfologik.fsa.FSA; |
| 14 |
|
| 15 |
import org.assertj.core.api.Assertions; |
| 16 |
import org.junit.Assert; |
| 17 |
import org.junit.Test; |
| 18 |
|
| 19 |
public class DictionaryLookupTest { |
| 20 |
@Test |
| 21 |
public void testApplyReplacements() { |
| 22 |
LinkedHashMap<String, String> conversion = new LinkedHashMap<>(); |
| 23 |
conversion.put("'", "`"); |
| 24 |
conversion.put("fi", "fi"); |
| 25 |
conversion.put("\\a", "ą"); |
| 26 |
conversion.put("Barack", "George"); |
| 27 |
conversion.put("_", "xx"); |
| 28 |
assertEquals("filut", DictionaryLookup.applyReplacements("filut", conversion)); |
| 29 |
assertEquals("fizdrygałką", DictionaryLookup.applyReplacements("fizdrygałk\\a", conversion)); |
| 30 |
assertEquals("George Bush", DictionaryLookup.applyReplacements("Barack Bush", conversion)); |
| 31 |
assertEquals("xxxxxxxx", DictionaryLookup.applyReplacements("____", conversion)); |
| 32 |
} |
| 33 |
|
| 34 |
@Test |
| 35 |
public void testRemovedEncoderProperties() throws IOException { |
| 36 |
final URL url = this.getClass().getResource("test-removed-props.dict"); |
| 37 |
try { |
| 38 |
new DictionaryLookup(Dictionary.read(url)); |
| 39 |
Assert.fail(); |
| 40 |
} catch (IOException e) { |
| 41 |
assertThat(e).hasMessageContaining( |
| 42 |
DictionaryAttribute.ENCODER.propertyName); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
@Test |
| 47 |
public void testPrefixDictionaries() throws IOException { |
| 48 |
final URL url = this.getClass().getResource("test-prefix.dict"); |
| 49 |
final IStemmer s = new DictionaryLookup(Dictionary.read(url)); |
| 50 |
|
| 51 |
assertArrayEquals(new String[] { "Rzeczpospolita", "subst:irreg" }, |
| 52 |
stem(s, "Rzeczypospolitej")); |
| 53 |
assertArrayEquals(new String[] { "Rzeczpospolita", "subst:irreg" }, |
| 54 |
stem(s, "Rzecząpospolitą")); |
| 55 |
|
| 56 |
// This word is not in the dictionary. |
| 57 |
assertNoStemFor(s, "martygalski"); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void testInputConversion() throws IOException { |
| 62 |
final URL url = this.getClass().getResource("test-prefix.dict"); |
| 63 |
final IStemmer s = new DictionaryLookup(Dictionary.read(url)); |
| 64 |
|
| 65 |
assertArrayEquals(new String[] { "Rzeczpospolita", "subst:irreg" }, |
| 66 |
stem(s, "Rzecz\\apospolit\\a")); |
| 67 |
|
| 68 |
assertArrayEquals(new String[] { "Rzeczpospolita", "subst:irreg" }, |
| 69 |
stem(s, "krowa\\apospolit\\a")); |
| 70 |
} |
| 71 |
|
| 72 |
/* */ |
| 73 |
@Test |
| 74 |
public void testInfixDictionaries() throws IOException { |
| 75 |
final URL url = this.getClass().getResource("test-infix.dict"); |
| 76 |
final IStemmer s = new DictionaryLookup(Dictionary.read(url)); |
| 77 |
|
| 78 |
Assertions.assertThat(stem(s, "Rzeczypospolitej")) |
| 79 |
.containsExactly("Rzeczpospolita", "subst:irreg"); |
| 80 |
|
| 81 |
Assertions.assertThat(stem(s, "Rzeczyccy")) |
| 82 |
.containsExactly("Rzeczycki", "adj:pl:nom:m"); |
| 83 |
|
| 84 |
Assertions.assertThat(stem(s, "Rzecząpospolitą")) |
| 85 |
.containsExactly("Rzeczpospolita", "subst:irreg"); |
| 86 |
|
| 87 |
// This word is not in the dictionary. |
| 88 |
assertNoStemFor(s, "martygalski"); |
| 89 |
|
| 90 |
// This word uses characters that are outside of the encoding range of the dictionary. |
| 91 |
assertNoStemFor(s, "Rzeczyckiõh"); |
| 92 |
} |
| 93 |
|
| 94 |
/* */ |
| 95 |
@Test |
| 96 |
public void testWordDataIterator() throws IOException { |
| 97 |
final URL url = this.getClass().getResource("test-infix.dict"); |
| 98 |
final DictionaryLookup s = new DictionaryLookup(Dictionary.read(url)); |
| 99 |
|
| 100 |
final HashSet<String> entries = new HashSet<String>(); |
| 101 |
for (WordData wd : s) { |
| 102 |
entries.add(wd.getWord() + " " + wd.getStem() + " " + wd.getTag()); |
| 103 |
} |
| 104 |
|
| 105 |
// Make sure a sample of the entries is present. |
| 106 |
Assertions.assertThat(entries) |
| 107 |
.contains( |
| 108 |
"Rzekunia Rzekuń subst:sg:gen:m", |
| 109 |
"Rzeczkowskie Rzeczkowski adj:sg:nom.acc.voc:n+adj:pl:acc.nom.voc:f.n", |
| 110 |
"Rzecząpospolitą Rzeczpospolita subst:irreg", |
| 111 |
"Rzeczypospolita Rzeczpospolita subst:irreg", |
| 112 |
"Rzeczypospolitych Rzeczpospolita subst:irreg", |
| 113 |
"Rzeczyckiej Rzeczycki adj:sg:gen.dat.loc:f"); |
| 114 |
} |
| 115 |
|
| 116 |
/* */ |
| 117 |
@Test |
| 118 |
public void testWordDataCloning() throws IOException { |
| 119 |
final URL url = this.getClass().getResource("test-infix.dict"); |
| 120 |
final DictionaryLookup s = new DictionaryLookup(Dictionary.read(url)); |
| 121 |
|
| 122 |
ArrayList<WordData> words = new ArrayList<WordData>(); |
| 123 |
for (WordData wd : s) { |
| 124 |
WordData clone = wd.clone(); |
| 125 |
words.add(clone); |
| 126 |
} |
| 127 |
|
| 128 |
// Reiterate and verify that we have the same entries. |
| 129 |
final DictionaryLookup s2 = new DictionaryLookup(Dictionary.read(url)); |
| 130 |
int i = 0; |
| 131 |
for (WordData wd : s2) { |
| 132 |
WordData clone = words.get(i++); |
| 133 |
assertEqualSequences(clone.getStem(), wd.getStem()); |
| 134 |
assertEqualSequences(clone.getTag(), wd.getTag()); |
| 135 |
assertEqualSequences(clone.getWord(), wd.getWord()); |
| 136 |
} |
| 137 |
|
| 138 |
// Check collections contract. |
| 139 |
final HashSet<WordData> entries = new HashSet<WordData>(); |
| 140 |
try { |
| 141 |
entries.add(words.get(0)); |
| 142 |
fail(); |
| 143 |
} catch (RuntimeException e) { |
| 144 |
// Expected. |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
private void assertEqualSequences(CharSequence s1, CharSequence s2) { |
| 149 |
assertEquals(s1.toString(), s2.toString()); |
| 150 |
} |
| 151 |
|
| 152 |
/* */ |
| 153 |
@Test |
| 154 |
public void testMultibyteEncodingUTF8() throws IOException { |
| 155 |
final URL url = this.getClass().getResource("test-diacritics-utf8.dict"); |
| 156 |
Dictionary read = Dictionary.read(url); |
| 157 |
final IStemmer s = new DictionaryLookup(read); |
| 158 |
|
| 159 |
assertArrayEquals(new String[] { "merge", "001" }, stem(s, "mergeam")); |
| 160 |
assertArrayEquals(new String[] { "merge", "002" }, stem(s, "merseserăm")); |
| 161 |
} |
| 162 |
|
| 163 |
/* */ |
| 164 |
@Test |
| 165 |
public void testSynthesis() throws IOException { |
| 166 |
final URL url = this.getClass().getResource("test-synth.dict"); |
| 167 |
final IStemmer s = new DictionaryLookup(Dictionary.read(url)); |
| 168 |
|
| 169 |
assertArrayEquals(new String[] { "miała", null }, stem(s, |
| 170 |
"mieć|verb:praet:sg:ter:f:?perf")); |
| 171 |
assertArrayEquals(new String[] { "a", null }, stem(s, "a|conj")); |
| 172 |
assertArrayEquals(new String[] {}, stem(s, "dziecko|subst:sg:dat:n")); |
| 173 |
|
| 174 |
// This word is not in the dictionary. |
| 175 |
assertNoStemFor(s, "martygalski"); |
| 176 |
} |
| 177 |
|
| 178 |
/* */ |
| 179 |
@Test |
| 180 |
public void testInputWithSeparators() throws IOException { |
| 181 |
final URL url = this.getClass().getResource("test-separators.dict"); |
| 182 |
final DictionaryLookup s = new DictionaryLookup(Dictionary.read(url)); |
| 183 |
|
| 184 |
/* |
| 185 |
* Attemp to reconstruct input sequences using WordData iterator. |
| 186 |
*/ |
| 187 |
ArrayList<String> sequences = new ArrayList<String>(); |
| 188 |
for (WordData wd : s) { |
| 189 |
sequences.add("" + wd.getWord() + " " + wd.getStem() + " " |
| 190 |
+ wd.getTag()); |
| 191 |
} |
| 192 |
Collections.sort(sequences); |
| 193 |
|
| 194 |
assertEquals("token1 null null", sequences.get(0)); |
| 195 |
assertEquals("token2 null null", sequences.get(1)); |
| 196 |
assertEquals("token3 null +", sequences.get(2)); |
| 197 |
assertEquals("token4 token2 null", sequences.get(3)); |
| 198 |
assertEquals("token5 token2 null", sequences.get(4)); |
| 199 |
assertEquals("token6 token2 +", sequences.get(5)); |
| 200 |
assertEquals("token7 token2 token3+", sequences.get(6)); |
| 201 |
assertEquals("token8 token2 token3++", sequences.get(7)); |
| 202 |
} |
| 203 |
|
| 204 |
/* */ |
| 205 |
@Test |
| 206 |
public void testSeparatorInLookupTerm() throws IOException { |
| 207 |
FSA fsa = FSA.read(getClass().getResourceAsStream("test-separator-in-lookup.fsa")); |
| 208 |
|
| 209 |
DictionaryMetadata metadata = new DictionaryMetadataBuilder() |
| 210 |
.separator('+') |
| 211 |
.encoding("iso8859-1") |
| 212 |
.encoder(EncoderType.INFIX) |
| 213 |
.build(); |
| 214 |
|
| 215 |
final DictionaryLookup s = new DictionaryLookup(new Dictionary(fsa, metadata)); |
| 216 |
assertEquals(0, s.lookup("l+A").size()); |
| 217 |
} |
| 218 |
|
| 219 |
/* */ |
| 220 |
@Test |
| 221 |
public void testGetSeparator() throws IOException { |
| 222 |
final URL url = this.getClass().getResource("test-separators.dict"); |
| 223 |
final DictionaryLookup s = new DictionaryLookup(Dictionary.read(url)); |
| 224 |
assertEquals('+', s.getSeparatorChar()); |
| 225 |
} |
| 226 |
|
| 227 |
/* */ |
| 228 |
public static String asString(CharSequence s) { |
| 229 |
if (s == null) |
| 230 |
return null; |
| 231 |
return s.toString(); |
| 232 |
} |
| 233 |
|
| 234 |
/* */ |
| 235 |
public static String[] stem(IStemmer s, String word) { |
| 236 |
ArrayList<String> result = new ArrayList<String>(); |
| 237 |
for (WordData wd : s.lookup(word)) { |
| 238 |
result.add(asString(wd.getStem())); |
| 239 |
result.add(asString(wd.getTag())); |
| 240 |
} |
| 241 |
return result.toArray(new String[result.size()]); |
| 242 |
} |
| 243 |
|
| 244 |
/* */ |
| 245 |
public static void assertNoStemFor(IStemmer s, String word) { |
| 246 |
assertArrayEquals(new String[] {}, stem(s, word)); |
| 247 |
} |
| 248 |
} |