Search code examples
javawordnetsynonym

Java - WorNet - Synonym Detection (extjwnl 1.8)


I hope you can help me...

How to get synonym of a word into an array using extended java word-net library

Waiting for your valuable response...


Solution

  • After looking over the API, it seems that synonyms in WordNet are refered to as Synsets.

    Assuming you have already called System.setProperty("wordnet.database.dir", "<location_to_WordNet_database>/dict"), you can declare and initalize a WordNetDatabase like so:

    WordNetDatabase database = WordNetDatabase.getFileInstance();
    

    and then declare and initialize a Synset array:

    Synset[] synsets = database.getSynsets("your word", SynsetType.<WORDTYPE>/*like NOUN, or VERB*/);
    

    I'm assuming that setting SynsetType.NOUN as the second parameter would create an array of synonyms which are nouns only.

    You could then declare a Synset which corresponds to the synset array which you just initalized (for example, if you called database.getSynsets("your word", SynsetType.NOUN), you would do this):

    NounSynset nounSynset;
    

    and finally you could iterate through your synsets array in a for loop, setting

    nounSynset = (NounSynset) synsets[i];
    

    and assign its primary word form to a String via

    String currentSynonym = nounSynset.getWordForms()[0];
    

    For more information, see Java API for WordNet main page and the documentation overview