Debian Codegraph @

morfologik-stemming/src/main/java/morfologik/stemming/DictionaryAttribute.java

raw
1 package morfologik.stemming;
2
3 import java.nio.charset.Charset;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import java.util.Locale;
9 import java.util.Map;
10 import java.util.Arrays;
11
12 /**
13 * Attributes applying to {@link Dictionary} and {@link DictionaryMetadata}.
14 */
15 public enum DictionaryAttribute {
16 /**
17 * Logical fields separator inside the FSA.
18 */
19 SEPARATOR("fsa.dict.separator") {
20 @Override
21 public Character fromString(String separator) {
22 if (separator == null || separator.length() != 1) {
23 throw new IllegalArgumentException("Attribute " + propertyName
24 + " must be a single character.");
25 }
26
27 char charValue = separator.charAt(0);
28 if (Character.isHighSurrogate(charValue) ||
29 Character.isLowSurrogate(charValue)) {
30 throw new IllegalArgumentException(
31 "Field separator character cannot be part of a surrogate pair: " + separator);
32 }
33
34 return charValue;
35 }
36 },
37
38 /**
39 * Character to byte encoding used for strings inside the FSA.
40 */
41 ENCODING("fsa.dict.encoding") {
42 @Override
43 public Charset fromString(String charsetName) {
44 return Charset.forName(charsetName);
45 }
46 },
47
48 /**
49 * If the FSA dictionary includes frequency data.
50 */
51 FREQUENCY_INCLUDED("fsa.dict.frequency-included") {
52 @Override
53 public Boolean fromString(String value) {
54 return booleanValue(value);
55 }
56 },
57
58 /**
59 * If the spelling dictionary is supposed to ignore words containing digits
60 */
61 IGNORE_NUMBERS("fsa.dict.speller.ignore-numbers") {
62 @Override
63 public Boolean fromString(String value) {
64 return booleanValue(value);
65 }
66 },
67
68 /**
69 * If the spelling dictionary is supposed to ignore punctuation.
70 */
71 IGNORE_PUNCTUATION("fsa.dict.speller.ignore-punctuation") {
72 @Override
73 public Boolean fromString(String value) {
74 return booleanValue(value);
75 }
76 },
77
78 /**
79 * If the spelling dictionary is supposed to ignore CamelCase words.
80 */
81 IGNORE_CAMEL_CASE("fsa.dict.speller.ignore-camel-case") {
82 @Override
83 public Boolean fromString(String value) {
84 return booleanValue(value);
85 }
86 },
87
88 /**
89 * If the spelling dictionary is supposed to ignore ALL UPPERCASE words.
90 */
91 IGNORE_ALL_UPPERCASE("fsa.dict.speller.ignore-all-uppercase") {
92 @Override
93 public Boolean fromString(String value) {
94 return booleanValue(value);
95 }
96 },
97
98 /**
99 * If the spelling dictionary is supposed to ignore diacritics, so that
100 * 'a' would be treated as equivalent to 'ą'.
101 */
102 IGNORE_DIACRITICS("fsa.dict.speller.ignore-diacritics") {
103 @Override
104 public Boolean fromString(String value) {
105 return booleanValue(value);
106 }
107 },
108
109 /**
110 * if the spelling dictionary is supposed to treat upper and lower case
111 * as equivalent.
112 */
113 CONVERT_CASE("fsa.dict.speller.convert-case") {
114 @Override
115 public Boolean fromString(String value) {
116 return booleanValue(value);
117 }
118 },
119
120 /**
121 * If the spelling dictionary is supposed to split runOnWords.
122 */
123 RUN_ON_WORDS("fsa.dict.speller.runon-words") {
124 @Override
125 public Boolean fromString(String value) {
126 return booleanValue(value);
127 }
128 },
129
130 /** Locale associated with the dictionary. */
131 LOCALE("fsa.dict.speller.locale") {
132 @Override
133 public Locale fromString(String value) {
134 return new Locale(value);
135 }
136 },
137
138 /** Locale associated with the dictionary. */
139 ENCODER("fsa.dict.encoder") {
140 @Override
141 public EncoderType fromString(String value) {
142 try {
143 return EncoderType.valueOf(value.trim().toUpperCase(Locale.ROOT));
144 } catch (IllegalArgumentException e) {
145 throw new IllegalArgumentException("Invalid encoder name '" + value.trim() + "', only these coders are valid: " + Arrays.toString(EncoderType.values()));
146 }
147 }
148 },
149
150 /**
151 * Input conversion pairs to replace non-standard characters before search in a speller dictionary.
152 * For example, common ligatures can be replaced here.
153 */
154 INPUT_CONVERSION("fsa.dict.input-conversion") {
155 @Override
156 public LinkedHashMap<String, String> fromString(String value) throws IllegalArgumentException {
157 LinkedHashMap<String, String> conversionPairs = new LinkedHashMap<>();
158 final String[] replacements = value.split(",\\s*");
159 for (final String stringPair : replacements) {
160 final String[] twoStrings = stringPair.trim().split(" ");
161 if (twoStrings.length == 2) {
162 if (!conversionPairs.containsKey(twoStrings[0])) {
163 conversionPairs.put(twoStrings[0], twoStrings[1]);
164 } else {
165 throw new IllegalArgumentException(
166 "Input conversion cannot specify different values for the same input string: " + twoStrings[0]);
167 }
168 } else {
169 throw new IllegalArgumentException("Attribute " + propertyName
170 + " is not in the proper format: " + value);
171 }
172 }
173 return conversionPairs;
174 }
175 },
176
177 /**
178 * Output conversion pairs to replace non-standard characters before search in a speller dictionary.
179 * For example, standard characters can be replaced here into ligatures.
180 *
181 * Useful for dictionaries that do have certain standards imposed.
182 *
183 */
184 OUTPUT_CONVERSION ("fsa.dict.output-conversion") {
185 @Override
186 public LinkedHashMap<String, String> fromString(String value) throws IllegalArgumentException {
187 LinkedHashMap<String, String> conversionPairs = new LinkedHashMap<String, String>();
188 final String[] replacements = value.split(",\\s*");
189 for (final String stringPair : replacements) {
190 final String[] twoStrings = stringPair.trim().split(" ");
191 if (twoStrings.length == 2) {
192 if (!conversionPairs.containsKey(twoStrings[0])) {
193 conversionPairs.put(twoStrings[0], twoStrings[1]);
194 } else {
195 throw new IllegalArgumentException(
196 "Input conversion cannot specify different values for the same input string: " + twoStrings[0]);
197 }
198 } else {
199 throw new IllegalArgumentException("Attribute " + propertyName
200 + " is not in the proper format: " + value);
201 }
202 }
203 return conversionPairs;
204 }
205 },
206
207 /**
208 * Replacement pairs for non-obvious candidate search in a speller dictionary.
209 * For example, Polish <tt>rz</tt> is phonetically equivalent to <tt>ż</tt>,
210 * and this may be specified here to allow looking for replacements of <tt>rz</tt> with <tt>ż</tt>
211 * and vice versa.
212 */
213 REPLACEMENT_PAIRS("fsa.dict.speller.replacement-pairs") {
214 @Override
215 public LinkedHashMap<String, List<String>> fromString(String value) throws IllegalArgumentException {
216 LinkedHashMap<String, List<String>> replacementPairs = new LinkedHashMap<>();
217 final String[] replacements = value.split(",\\s*");
218 for (final String stringPair : replacements) {
219 final String[] twoStrings = stringPair.trim().split(" ");
220 if (twoStrings.length == 2) {
221 if (!replacementPairs.containsKey(twoStrings[0])) {
222 List<String> strList = new ArrayList<String>();
223 strList.add(twoStrings[1]);
224 replacementPairs.put(twoStrings[0], strList);
225 } else {
226 replacementPairs.get(twoStrings[0]).add(twoStrings[1]);
227 }
228 } else {
229 throw new IllegalArgumentException("Attribute " + propertyName
230 + " is not in the proper format: " + value);
231 }
232 }
233 return replacementPairs;
234 }
235 },
236
237 /**
238 * Equivalent characters (treated similarly as equivalent chars with and without
239 * diacritics). For example, Polish <tt>ł</tt> can be specified as equivalent to <tt>l</tt>.
240 *
241 * <p>This implements a feature similar to hunspell MAP in the affix file.
242 */
243 EQUIVALENT_CHARS("fsa.dict.speller.equivalent-chars") {
244 @Override
245 public LinkedHashMap<Character, List<Character>> fromString(String value) throws IllegalArgumentException {
246 LinkedHashMap<Character, List<Character>> equivalentCharacters = new LinkedHashMap<>();
247 final String[] eqChars = value.split(",\\s*");
248 for (final String characterPair : eqChars) {
249 final String[] twoChars = characterPair.trim().split(" ");
250 if (twoChars.length == 2
251 && twoChars[0].length() == 1
252 && twoChars[1].length() == 1) {
253 char fromChar = twoChars[0].charAt(0);
254 char toChar = twoChars[1].charAt(0);
255 if (!equivalentCharacters.containsKey(fromChar)) {
256 List<Character> chList = new ArrayList<Character>();
257 equivalentCharacters.put(fromChar, chList);
258 }
259 equivalentCharacters.get(fromChar).add(toChar);
260 } else {
261 throw new IllegalArgumentException("Attribute " + propertyName
262 + " is not in the proper format: " + value);
263 }
264 }
265 return equivalentCharacters;
266 }
267 },
268
269 /**
270 * Dictionary license attribute.
271 */
272 LICENSE("fsa.dict.license"),
273
274 /**
275 * Dictionary author.
276 */
277 AUTHOR("fsa.dict.author"),
278
279 /**
280 * Dictionary creation date.
281 */
282 CREATION_DATE("fsa.dict.created");
283
284 /**
285 * Property name for this attribute.
286 */
287 public final String propertyName;
288
289 /**
290 * Converts a string to the given attribute's value.
291
292 * @param value The value to convert to an attribute value.
293 * @return Returns the attribute's value converted from a string.
294 *
295 * @throws IllegalArgumentException
296 * If the input string cannot be converted to the attribute's
297 * value.
298 */
299 public Object fromString(String value) throws IllegalArgumentException {
300 return value;
301 }
302
303 /**
304 * @param propertyName The property of a {@link DictionaryAttribute}.
305 * @return Return a {@link DictionaryAttribute} associated with
306 * a given {@link #propertyName}.
307 */
308 public static DictionaryAttribute fromPropertyName(String propertyName) {
309 DictionaryAttribute value = attrsByPropertyName.get(propertyName);
310 if (value == null) {
311 throw new IllegalArgumentException("No attribute for property: " + propertyName);
312 }
313 return value;
314 }
315
316 private static final Map<String,DictionaryAttribute> attrsByPropertyName;
317 static {
318 attrsByPropertyName = new HashMap<String,DictionaryAttribute>();
319 for (DictionaryAttribute attr : DictionaryAttribute.values()) {
320 if (attrsByPropertyName.put(attr.propertyName, attr) != null) {
321 throw new RuntimeException("Duplicate property key for: " + attr);
322 }
323 }
324 }
325
326 /**
327 * Private enum instance constructor.
328 */
329 private DictionaryAttribute(String propertyName) {
330 this.propertyName = propertyName;
331 }
332
333 private static Boolean booleanValue(String value) {
334 value = value.toLowerCase(Locale.ROOT);
335 if ("true".equals(value) || "yes".equals(value) || "on".equals(value)) {
336 return Boolean.TRUE;
337 }
338 if ("false".equals(value) || "no".equals(value) || "off".equals(value)) {
339 return Boolean.FALSE;
340 }
341 throw new IllegalArgumentException("Not a boolean value: " + value);
342 }
343 }