5

我正在使用 java jwi API 来搜索 wordnet 以获取单词的同义词。问题是它只给我一个结果来找到它的同义词本身。请指导我。是否可以获得给定单词的所有可能同义词的列表?我的代码是:

  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) {
    }

}

输出只有:

capacity

本身!实际的同义词必须是:

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

那么我在代码中遗漏了什么,或者有人能给我任何想法,真正的问题是什么?

提前致谢

4

3 回答 3

4

所以,我使用 Java JAWS 进行 wordnet 搜索的答案来了!步骤是:

    1- Download WordNet Dictionary from 

这里

    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>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 > 0){
                      ArrayList<String> al = new ArrayList<String>();
                      // 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 + "'");
              }
       } 

问题是你必须有jaws-bin.jar

于 2015-01-12T07:01:05.217 回答
2

你得到的是“capability #1 ”,它的意思是“执行或生产的能力”,它确实只有一个同义词。(玩转 PWN 搜索页面,了解 WordNet 如何将单词组织成同义词集。)

听起来你所追求的是所有同义词集中所有同义词的联合?我认为您要么使用getSenseEntryIterator(),要么简单地放置一个循环idxWord.getWordIDs().get(0);,用0循环计数器替换 ,因此您不仅可以获得数组中的第一项。

于 2014-03-23T23:33:50.490 回答
1

如果你想使用 JWI 并且想获取超过 1 个同义词,那么从这个确切的位置更改你的代码:

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!");
        }

它工作得很好。

于 2018-04-08T12:30:42.727 回答