Title: Searching WordNet for Synonym gives only one result
Tags: java;wordnet;synonym;jwi
Question: I am using java jwi API for searching the wordnet to get the synonyms of a word. The problem is that it only gives me one result the word to find its synonyms itself. Please guide me. Is it possible to get the list of all possible synonyms of a given word? My code is:

```  public void searcher() {
    try {

        url = new URL("file", null, path);


        dict = new Dictionary(url);
        try {
            dict.open();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Dictionary directory does not exist\n" + ex + "\nClass:Meaning Thread", "Dictionary Not Found Error", JOptionPane.ERROR_MESSAGE);

        }

        IIndexWord idxWord = dict.getIndexWord("capacity", POS.NOUN);
        IWordID wordID = idxWord.getWordIDs().get(0);
        IWord word = dict.getWord(wordID);


        //Adding Related Words to List of Realted Words
        ISynset synset = word.getSynset();
        for (IWord w : synset.getWords()) {
            System.out.println(w.getLemma());
        }


    } catch (Exception e) {
    }

}
```

The output is only:

```capacity
```

itself! The actual synonyms must be:

```  capability
  capacitance 
  content
  electrical capacitance
  mental ability...(so on)
```

So is there anything I missed in the code or can somebodygive me any ideas what is the real problem?

Thanks in advance

Comment: http://lyle.smu.edu/~tspell/jaws/TestJAWS.java here is the code can work for you it need jaws.jar to integrate with it

Comment: You can contact me icombs@example.org i am working on data mining projects since last 3 years!

Comment: http://stackoverflow.com/a/27896853/3411946 Helpful

Comment: here is a related question on finding antonyms: https://stackoverflow.com/questions/65403290/get-antonyms-for-a-word-in-java-wordnet-jwi

Comment: can you please provide complete code. it will help me to get idea

Comment: I am asking for Synonym combinig code.

Comment: I am getting error : Error opening index file: ./index.sense

Comment: ok, thanks. I will try and if not working than call you back.

Comment: hello Java Nerd it gives me same error : Error opening index file: ./index.sense (No such file or directory) any solution.

Comment: hello Java Nerd, problem solved. Problem was with dictionary. I have not defined dictionary. so, it was unable to find dictionary.

Here is the accepted answer: So, here comes the answer i use Java JAWS for wordnet searching! The steps are:

```    1- Download WordNet Dictionary from 
```

Here 

```    2- Install WordNet
    3- Go to Installed Directory and copied the WordNet Directory (in my case C:\Program Files (x86) was the Directory for WordNet Folder)
    4- Pasted it into my Java Project (under MyProject&gt;WordNet)
    5- Making Path to the directory as:
       File f=new File("WordNet\\2.1\\dict");
       System.setProperty("wordnet.database.dir", f.toString());
    6- Got Synonyms as:

       public class TestJAWS{
              public static void main(String[] args){
                    String wordForm = "capacity";
                    //  Get the synsets containing the word form=capicity

                   File f=new File("WordNet\\2.1\\dict");
                   System.setProperty("wordnet.database.dir", f.toString());
                   //setting path for the WordNet Directory

                   WordNetDatabase database = WordNetDatabase.getFileInstance();
                   Synset[] synsets = database.getSynsets(wordForm);
                   //  Display the word forms and definitions for synsets retrieved

                   if (synsets.length &gt; 0){
                      ArrayList<String&gt; al = new ArrayList<String&gt;();
                      // add elements to al, including duplicates
                      HashSet hs = new HashSet();
                      for (int i = 0; i < synsets.length; i++){
                         String[] wordForms = synsets[i].getWordForms();
                           for (int j = 0; j < wordForms.length; j++)
                           {
                             al.add(wordForms[j]);
                           }


                      //removing duplicates
                       hs.addAll(al);
                       al.clear();
                       al.addAll(hs);

                      //showing all synsets
                      for (int i = 0; i < al.size(); i++) {
                            System.out.println(al.get(i));
                      }
                   }
              }
              }
              else
              {
               System.err.println("No synsets exist that contain the word form '" + wordForm + "'");
              }
       } 
```

The Thing is you must have jaws-bin.jar

Comment for this answer: how can i handle wordnetexception. I try inputting other keywords like light or ball it throws the wordnet exception. btw, you have extra bracket for your if condition
Comment for this answer: the exception is like "An error occurred parsing the synset data: : 00292635 30 v 05 light 0 illume 0 illumine 0 light_up 0 illuminate 3 012 @ 00281690 v 0000 + 14006632 n 0501 + 05025708 n 0502 + 14711674 n 0501 + 14006789 n 0101 + 04958550 n 0101 + 08663763 n 0101 + 05025269 n 0106 + 03670692 n 0101 + 11494354 n 0101 ~ 00293009 v 0000 ~ 00293130 v 0000 02 + 08 00 + 11 00 | make lighter or brighter; "This lamp lightens the room a bit"

Here is another answer: If you want to use JWI and want to fetch more than 1 synonym then change your code from this exact spot:

```IIndexWord idxWord = dict.getIndexWord(inputWord, POS.NOUN);
        try {
            int x = idxWord.getTagSenseCount();
            for (int i = 0; i < x; i++) {
                IWordID wordID = idxWord.getWordIDs().get(i);
                IWord word = dict.getWord(wordID);

                // Adding Related Words to List of Realted Words
                ISynset synset = word.getSynset();
                for (IWord w : synset.getWords()) {
                    System.out.println(w.getLemma());
                    // output.add(w.getLemma());
                }
            }
        } catch (Exception ex) {
            System.out.println("No synonym found!");
        }
```

It works perfectly fine.


Here is another answer: What you are getting is "capacity#1", which has the meaning of "capability to perform or produce", and it does indeed only have one synonym. (Play around with the PWN search page to get a feel for how WordNet organizes the words into synsets.)

It sounds like what you are after is the union of all synonyms in all the synsets? I think you either use ```getSenseEntryIterator()```, or simply put a loop around ```idxWord.getWordIDs().get(0);```, replacing the ```0``` with the loop counter, so you are not only ever getting the first item in the array.

Comment for this answer: @219CID Yes, I guess if you want every synonym for every possible part of speech, you'd add an outer loop, that will iterate through each POS tag.
Comment for this answer: could you call getSenseEntryIterator() for every POS for a given word? Or would that be repetitive?
Comment for this answer: Thank you, I had success doing this and was able to get these unique synonyms for capacity: CAPABILITY
CONTENT
CAPACITANCE
ELECTRICALCAPACITY
MENTALABILITY
Comment for this answer: I am wondering if you could take a look at this related JWI/WordNet 
 question: https://stackoverflow.com/questions/65403290/get-antonyms-for-a-word-in-java-wordnet-jwi

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 10, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/23/14', 'FavoriteCount': 5.0, 'PostTypeId': 1, 'Tags': '<java><wordnet><synonym><jwi>', 'Title': 'Searching WordNet for Synonym gives only one result', 'ViewCount': 7252, 'id': 22589700, 'user_AboutMe': '', 'user_AccountId': 4162075.0, 'user_DisplayName': 'Java Nerd', 'user_Reputation': 918.0, 'user_Views': 334.0, 'user_id': 3411946.0}}