diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-07-27 23:10:16 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-07-27 23:10:16 -0400 |
| commit | fadc9d01f662ab001d9fdd0fd92c5f0c177557cd (patch) | |
| tree | 0747e6746c3b7f33016bf8ed0324bd6972b09795 /src/main/java/bjc/esodata/AbbrevMap2.java | |
| parent | a623c8fafaa103a902fbdb4936b2ee78463ae5ba (diff) | |
Reimplement AbbrevMap, and implement Multimap
This reimplements the old AbbrevMap structure as AbbrevMap2, and created
a new Multimap structure as a apart of it.
Multimap is exactly what it sounds like; a map that allows multiple
values for a given key. The only real thing that is different about it,
is that if you add a key-value pair multiple times, you'll have to
remove it multiple times.
Diffstat (limited to 'src/main/java/bjc/esodata/AbbrevMap2.java')
| -rw-r--r-- | src/main/java/bjc/esodata/AbbrevMap2.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/main/java/bjc/esodata/AbbrevMap2.java b/src/main/java/bjc/esodata/AbbrevMap2.java new file mode 100644 index 0000000..f79a2d2 --- /dev/null +++ b/src/main/java/bjc/esodata/AbbrevMap2.java @@ -0,0 +1,93 @@ +package bjc.esodata; + +import java.util.*; + +/** + * A revised version of {@link AbbrevMap} + * + * @author Ben Culkin + */ +public class AbbrevMap2 { + // Stores a mapping from strings, to strings that they could be abbreviations for + private Multimap<String, String> backing; + + /** + * Create a new abbreviation map. + */ + public AbbrevMap2() { + backing = new Multimap<>(); + } + + /** + * Add words to the map. + * + * @param words + * The words to add to the map. + */ + public void add(String... words) { + for (String word : words) { + for (String substr : genAbbrevs(word)) { + backing.add(substr, word); + } + } + } + + // Generate all of the strings a given word could be abbreviated as + private List<String> genAbbrevs(String word) { + List<String> retList = new ArrayList<>(); + + int len = word.length(); + + for (int i = 1; i <= len; i++) { + String substr = word.substring(0, i); + + retList.add(substr); + } + + return retList; + } + + /** + * Remove words from the map. + * + * @param words + * The words to remove from the map. + */ + public void removeWords(String... words) { + for (String word : words) { + for (String substr : genAbbrevs(word)) { + backing.remove(substr, word); + } + } + } + + /** + * Get all of the strings that a string could be an abbreviation for. + * + * @param word + * The word to attempt to deabbreviate. + * + * @return All of the possible deabbreviations for that word. + */ + public Set<String> deabbrevAll(String word) { + return backing.get(word); + } + + /** + * Get the unambiguous thing the string is an abbreviation for. + * + * @param word + * The word to attempt to deabbreviate. + * + * @return The unambiguous deabbreviation of the string, or null if there isn't one. + */ + public String deabbrev(String word) { + Set<String> st = backing.get(word); + + if (st.size() == 1) { + return st.iterator().next(); + } else { + return null; + } + } +} |
