| 1 |
package morfologik.stemming; |
| 2 |
|
| 3 |
import java.nio.charset.Charset; |
| 4 |
import java.util.EnumMap; |
| 5 |
import java.util.List; |
| 6 |
import java.util.Locale; |
| 7 |
import java.util.Map; |
| 8 |
|
| 9 |
/** |
| 10 |
* Helper class to build {@link DictionaryMetadata} instances. |
| 11 |
*/ |
| 12 |
public final class DictionaryMetadataBuilder { |
| 13 |
private final EnumMap<DictionaryAttribute, String> attrs = |
| 14 |
new EnumMap<>(DictionaryAttribute.class); |
| 15 |
|
| 16 |
public DictionaryMetadataBuilder separator(char c) { |
| 17 |
this.attrs.put(DictionaryAttribute.SEPARATOR, Character.toString(c)); |
| 18 |
return this; |
| 19 |
} |
| 20 |
|
| 21 |
public DictionaryMetadataBuilder encoding(Charset charset) { |
| 22 |
return encoding(charset.name()); |
| 23 |
} |
| 24 |
|
| 25 |
public DictionaryMetadataBuilder encoding(String charsetName) { |
| 26 |
this.attrs.put(DictionaryAttribute.ENCODING, charsetName); |
| 27 |
return this; |
| 28 |
} |
| 29 |
|
| 30 |
public DictionaryMetadataBuilder frequencyIncluded() { return frequencyIncluded(true); } |
| 31 |
public DictionaryMetadataBuilder frequencyIncluded(boolean v) { this.attrs.put(DictionaryAttribute.FREQUENCY_INCLUDED, Boolean.valueOf(v).toString()); return this; } |
| 32 |
|
| 33 |
public DictionaryMetadataBuilder ignorePunctuation() { return ignorePunctuation(true); } |
| 34 |
public DictionaryMetadataBuilder ignorePunctuation(boolean v) { this.attrs.put(DictionaryAttribute.IGNORE_PUNCTUATION, Boolean.valueOf(v).toString()); return this; } |
| 35 |
|
| 36 |
public DictionaryMetadataBuilder ignoreNumbers() { return ignoreNumbers(true); } |
| 37 |
public DictionaryMetadataBuilder ignoreNumbers(boolean v) { this.attrs.put(DictionaryAttribute.IGNORE_NUMBERS, Boolean.valueOf(v).toString()); return this; } |
| 38 |
|
| 39 |
public DictionaryMetadataBuilder ignoreCamelCase() { return ignoreCamelCase(true); } |
| 40 |
public DictionaryMetadataBuilder ignoreCamelCase(boolean v) { this.attrs.put(DictionaryAttribute.IGNORE_CAMEL_CASE, Boolean.valueOf(v).toString()); return this; } |
| 41 |
|
| 42 |
public DictionaryMetadataBuilder ignoreAllUppercase() { return ignoreAllUppercase(true); } |
| 43 |
public DictionaryMetadataBuilder ignoreAllUppercase(boolean v) { this.attrs.put(DictionaryAttribute.IGNORE_ALL_UPPERCASE, Boolean.valueOf(v).toString()); return this; } |
| 44 |
|
| 45 |
public DictionaryMetadataBuilder ignoreDiacritics() { return ignoreDiacritics(true); } |
| 46 |
public DictionaryMetadataBuilder ignoreDiacritics(boolean v) { this.attrs.put(DictionaryAttribute.IGNORE_DIACRITICS, Boolean.valueOf(v).toString()); return this; } |
| 47 |
|
| 48 |
public DictionaryMetadataBuilder convertCase() { return convertCase(true); } |
| 49 |
public DictionaryMetadataBuilder convertCase(boolean v) { this.attrs.put(DictionaryAttribute.CONVERT_CASE, Boolean.valueOf(v).toString()); return this; } |
| 50 |
|
| 51 |
public DictionaryMetadataBuilder supportRunOnWords() { return supportRunOnWords(true); } |
| 52 |
public DictionaryMetadataBuilder supportRunOnWords(boolean v) { this.attrs.put(DictionaryAttribute.RUN_ON_WORDS, Boolean.valueOf(v).toString()); return this; } |
| 53 |
|
| 54 |
public DictionaryMetadataBuilder encoder(EncoderType type) { |
| 55 |
this.attrs.put(DictionaryAttribute.ENCODER, type.name()); |
| 56 |
return this; |
| 57 |
} |
| 58 |
|
| 59 |
public DictionaryMetadataBuilder locale(Locale locale) { |
| 60 |
return locale(locale.toString()); |
| 61 |
} |
| 62 |
|
| 63 |
public DictionaryMetadataBuilder locale(String localeName) { |
| 64 |
this.attrs.put(DictionaryAttribute.LOCALE, localeName); |
| 65 |
return this; |
| 66 |
} |
| 67 |
|
| 68 |
public DictionaryMetadataBuilder withReplacementPairs(Map<String, List<String>> replacementPairs) { |
| 69 |
StringBuilder builder = new StringBuilder(); |
| 70 |
for (Map.Entry<String,List<String>> e : replacementPairs.entrySet()) { |
| 71 |
String k = e.getKey(); |
| 72 |
for (String v : e.getValue()) { |
| 73 |
if (builder.length() > 0) builder.append(", "); |
| 74 |
builder.append(k).append(" ").append(v); |
| 75 |
} |
| 76 |
} |
| 77 |
this.attrs.put(DictionaryAttribute.REPLACEMENT_PAIRS, builder.toString()); |
| 78 |
return this; |
| 79 |
} |
| 80 |
|
| 81 |
public DictionaryMetadataBuilder withEquivalentChars(Map<Character, List<Character>> equivalentChars) { |
| 82 |
StringBuilder builder = new StringBuilder(); |
| 83 |
for (Map.Entry<Character,List<Character>> e : equivalentChars.entrySet()) { |
| 84 |
Character k = e.getKey(); |
| 85 |
for (Character v : e.getValue()) { |
| 86 |
if (builder.length() > 0) builder.append(", "); |
| 87 |
builder.append(k).append(" ").append(v); |
| 88 |
} |
| 89 |
} |
| 90 |
this.attrs.put(DictionaryAttribute.EQUIVALENT_CHARS, builder.toString()); |
| 91 |
return this; |
| 92 |
} |
| 93 |
|
| 94 |
public DictionaryMetadataBuilder withInputConversionPairs(Map<String, String> conversionPairs) { |
| 95 |
StringBuilder builder = new StringBuilder(); |
| 96 |
for (Map.Entry<String,String> e : conversionPairs.entrySet()) { |
| 97 |
String k = e.getKey(); |
| 98 |
if (builder.length() > 0) builder.append(", "); |
| 99 |
builder.append(k).append(" ").append(conversionPairs.get(k)); |
| 100 |
} |
| 101 |
this.attrs.put(DictionaryAttribute.INPUT_CONVERSION, builder.toString()); |
| 102 |
return this; |
| 103 |
} |
| 104 |
|
| 105 |
public DictionaryMetadataBuilder withOutputConversionPairs(Map<String, String> conversionPairs) { |
| 106 |
StringBuilder builder = new StringBuilder(); |
| 107 |
for (Map.Entry<String,String> e : conversionPairs.entrySet()) { |
| 108 |
String k = e.getKey(); |
| 109 |
if (builder.length() > 0) builder.append(", "); |
| 110 |
builder.append(k).append(" ").append(conversionPairs.get(k)); |
| 111 |
} |
| 112 |
this.attrs.put(DictionaryAttribute.OUTPUT_CONVERSION, builder.toString()); |
| 113 |
return this; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
public DictionaryMetadataBuilder author(String author) { |
| 118 |
this.attrs.put(DictionaryAttribute.AUTHOR, author); |
| 119 |
return this; |
| 120 |
} |
| 121 |
|
| 122 |
public DictionaryMetadataBuilder creationDate(String creationDate) { |
| 123 |
this.attrs.put(DictionaryAttribute.CREATION_DATE, creationDate); |
| 124 |
return this; |
| 125 |
} |
| 126 |
|
| 127 |
public DictionaryMetadataBuilder license(String license) { |
| 128 |
this.attrs.put(DictionaryAttribute.LICENSE, license); |
| 129 |
return this; |
| 130 |
} |
| 131 |
|
| 132 |
public DictionaryMetadata build() { |
| 133 |
return new DictionaryMetadata(attrs); |
| 134 |
} |
| 135 |
|
| 136 |
public EnumMap<DictionaryAttribute, String> toMap() { |
| 137 |
return new EnumMap<>(attrs); |
| 138 |
} |
| 139 |
} |