public class KeywordDictionary
extends java.lang.Object
Keyword
object. During the
lookup process each keyword can be literally matched or can be optionally
matched with a set of valid abbreviations.
The namespace for the dictionary is flat. Each keyword and all possible
abbreviations must uniquely reference a single Keyword
object.
Lookups can be done on a case-sensitive basis or can be case-insensitive. Case-insensitivity is the default behavior.
Modifier and Type | Field and Description |
---|---|
private java.util.HashMap |
list
Associates 1 or more match strings with a
Keyword . |
private boolean |
matchCase
Controls case-sensitivity of add, delete and lookup operations.
|
Constructor and Description |
---|
KeywordDictionary()
Default constructor which instantiates a
KeywordDictionary
object which matches on a case-insensitive basis. |
KeywordDictionary(boolean matchCase)
Instantiates a
KeywordDictionary object. |
Modifier and Type | Method and Description |
---|---|
void |
addKeyword(Keyword word)
Adds a
Keyword object to the dictionary. |
void |
deleteKeyword(Keyword word)
Removes a
Keyword object from the dictionary. |
void |
dump()
Prints a report to
stderr which includes 1 line per
entry in the dictionary. |
(package private) java.util.Iterator |
keywordList()
Returns an iterator list of all
Keyword objects in the
dictionary. |
Keyword |
lookupKeyword(java.lang.String text)
Processes a dictionary lookup for a specified keyword.
|
private void |
processKeyword(Keyword word,
boolean add)
Worker method to add and remove
Keyword objects in the
dictionary. |
int |
size()
Report the number of keywords in this dictionary.
|
private java.util.HashMap list
Keyword
.private boolean matchCase
public KeywordDictionary()
KeywordDictionary
object which matches on a case-insensitive basis.public KeywordDictionary(boolean matchCase)
KeywordDictionary
object.matchCase
- Controls lookup algorithm case-sensitivity.public void addKeyword(Keyword word)
Keyword
object to the dictionary. The keyword is
mapped to its full text and any abbreviations, if supported by this
Keyword
object. All such mappings must be unique in the
dictionary because each unique text mapping can only have one
associated Keyword
.word
- Keyword object to add.public void deleteKeyword(Keyword word)
Keyword
object from the dictionary. Each
possible mapping representing this Keyword
is removed,
including its full text and all valid abbreviations, if supported.word
- Keyword object to remove.public Keyword lookupKeyword(java.lang.String text)
Keyword
object is returned. This
method honors the case-sensitivity setting defined at time of
this instance's construction.text
- The match string to be used as a key in the lookup. If case-
sensitivity is off, this key may be in any case (including
mixed case) and the result will always match.Keyword
object or null if no match was found.public int size()
public void dump()
stderr
which includes 1 line per
entry in the dictionary. If a Keyword
supports
abbreviations, then multiple entries will be found that refer to the
same Keyword
.java.util.Iterator keywordList()
Keyword
objects in the
dictionary.private void processKeyword(Keyword word, boolean add)
Keyword
objects in the
dictionary.
This method handles all logic related to abbreviation processing. Each
Keyword
object contains a
member. If this member is less
than 1 or greater than the size of the Keyword.minChars
Keyword's
full
text, no abbreviations are supported. Otherwise, minChars
specifies the minimum number of the left-most full text characters that
constitute a valid match with this Keyword
. To simplify
the lookup processing, all possible abbreviations are calculated and
inserted into our HashMap
. Each map entry stores a
reference to the Keyword
object being added. On removal,
the same abbreviation algorithm is used to determine all valid keys
which should be removed from the map.
Depending on the setting of the
instance variable, the map keys are optionally lowercased before
insertion into the map. On removal, the same case-sensitivity setting
is used to correctly match the case of the keys to be removed.matchCase
word
- The Keyword
to add or remove.add
- The operation to perform. If true, add to the map. If false,
remove from the map.