Antony
Antony

Reputation: 63

get synonym of word with JWI

To find synonym of a word, I have below code. There is a one problem with below source code; since word coming from user, I donot know its POS type. Therefore, how can I find IIndexWord of a word in case of I dont know its POS type?

IIndexWord idxWord = dict . getIndexWord ("dog", POS. NOUN );
IWordID wordID = idxWord . getWordIDs ().get (0) ; // 1st meaning
IWord word = dict . getWord ( wordID );
ISynset synset = word . getSynset ();

for( IWord w : synset . getWords ())
 System .out . println (w. getLemma ());

signature of my method looks like;

 void synonym(Strng word)

I am going to use word in type of String in place of "dog" word but, at runtime, I dont know its POS type.

I have second problem, before getting its synonym of a word, I want to check whether it is English word or not but JWI does not have a method like isEnglish() or isInDictionary(). How can I check a word whether it is english or not so that I would avoid to look for synonym of non-english word? ( eventually, improve performance)

Upvotes: 2

Views: 1986

Answers (2)

neo m
neo m

Reputation: 222

You can use a simple loop on all the POS value:

Set<String> lexicon = new HashSet<>();

for (POS p : POS.values()) {
    IIndexWord idxWord = dict.getIndexWord("name", p);
    if (idxWord != null) {
        System.out.println("\t : " + idxWord.getWordIDs().size());
        IWordID wordID = idxWord.getWordIDs().get(0);
        IWord word = dict.getWord(wordID);
        ISynset synset = word.getSynset();
        System.out.print(synset.getWords().size());
        for (IWord w : synset.getWords()) {
            lexicon.add(w.getLemma());
        }

    }
}

for (String s : lexicon) {
    System.out.println("wordnet lexicon : " + s);
}

Upvotes: 2

Laura
Laura

Reputation: 1602

Let's think about this. If the user wants to know the synonyms for a word like 'bark', that can be both a noun or a verb, you actually need to show him all the results. So, it will be correct to search the IIndexWord with all POS-es.

If you cannot find a specific word after trying all POS-es, then you can tell the user that it does not exist.

Upvotes: 0

Related Questions