A thesaurus is a list of words in which each word is associated with a corresponding set of other words having the same meaning (synonyms).
The following shows an example of words and synonyms that could be listed in a thesaurus.
Word | Synonym Set |
---|---|
excellent | {brilliant, great, outstanding, tremendous} |
super | {excellent, fantastic, great, wonderful} |
wonderful | {amazing, brilliant, fantastic, great} |
The thesaurus can be represented using a Map in which each key is a word, and the value associated with a particular word is a Set of synonyms. A partial definition of the Thesaurus class is shown below.
public class Thesaurus { // stores (word, synonym set) pairs as (String, Set of Strings) private Map<String, Set<String>> wordMap; public Thesaurus() { wordMap = new HashMap<String, Set<String>>(); } // adds syn to the set of synonyms associated with word // in this Thesaurus; // if word is not a key in this Thesaurus, adds an // entry with word as key and a set containing syn // as the associated value{ /* to be implemented in part (a) */ } // removes the word syn from each synonym set in this Thesaurus; // returns a set of the words (keys) whose associated // synonym sets originally contained the word syn; // if syn was not contained in any synonym set, returns an empty setpublic void addSynonym(String word, String syn){ /* to be implemented in part (b) */ } // There may be fields, constructors, and methods that are not shown. }public Set<String> removeSynonym(String syn)
Write the Thesaurus method addSynonym. If word is already a key in wordMap, syn is added to the set associated with word. Otherwise, a new entry is added to wordMap with word as the key and a set containing syn as the associated value.
For example, assume that the Thesaurus object myThesaurus has been declared and initialized with the values shown at the beginning of this question. The following code segment contains two calls to addSynonym.
myThesaurus.addSynonym("wonderful", "magnificent"); myThesaurus.addSynonym("awesome", "wonderful");
After the code segment has executed, the contents of myThesaurus are as shown below.
Word
| Synonym Set
|
---|---|
excellent | {brilliant, great, outstanding, tremendous} |
super | {excellent, fantastic, great, wonderful} |
wonderful | {amazing, brilliant, fantastic, great, magnificent} |
awesome | {wonderful} |
Complete method addSynonym below.
// adds syn to the set of synonyms associated with word // in this Thesaurus; // if word is not a key in this Thesaurus, adds an // entry with word as key and a set containing syn // as the associated valuepublic void addSynonym(String word, String syn)
Write the Thesaurus method removeSynonym. The word syn should be removed from every synonym set in the thesaurus. Method removeSynonym returns a set (possibly empty) that contains the keys for which the associated synonym set was modified. No key is ever removed from the thesaurus, even if the synonym set associated with that key becomes empty.
For example, assume that the Thesaurus object myThesaurus has been declared and initialized with the following values.
Word
| Synonym Set
|
---|---|
excellent | {brilliant, great, outstanding, tremendous} |
super | {excellent, fantastic, great, wonderful} |
wonderful | {amazing, brilliant, fantastic, great, magnificent} |
awesome | {wonderful} |
Consider the following statement.
Set affectedWords = myThesaurus.removeSynonym("wonderful");
After the statement has executed, the contents of myThesaurus are as shown below, and the set affectedWords contains {super, awesome}. Note that the words "wonderful" and "awesome" still appear as keys in the thesaurus.
Word
| Synonym Set
|
---|---|
excellent | {brilliant, great, outstanding, tremendous} |
super | {excellent, fantastic, great} |
wonderful | {amazing, brilliant, fantastic, great, magnificent} |
awesome | {} |
Complete method removeSynonym below.
// removes the word syn from each synonym set in this Thesaurus; // returns a set of the words (keys) whose associated // synonym sets originally contained the word syn; // if syn was not contained in any synonym set, returns an empty set publicSet<String> removeSynonym(String syn)
*your words may not be in same order (set)
super {great,excellent,fantastic,wonderful} awesome {wonderful} excellent {tremendous,great,brillant,outstanding} wonderful {magnificent,amazing,great,brilliant,fantastic} super excellent wonderful super {excellent,fantastic,wonderful} awesome {wonderful} excellent {tremendous,brillant,outstanding} wonderful {magnificent,amazing,brilliant,fantastic}