【问题标题】:Searching WordNet for Synonym gives only one result在 WordNet 中搜索同义词只会得到一个结果
【发布时间】:2014-03-23 10:34:55
【问题描述】:

我正在使用 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)

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

提前致谢

【问题讨论】:

  • 能否提供完整的代码。这将帮助我获得想法
  • 我要的是同义词组合代码。
  • 我收到错误:打开索引文件时出错:./index.sense
  • lyle.smu.edu/~tspell/jaws/TestJAWS.java 这里的代码可以为你工作,它需要jaws.jar 与它集成
  • 你可以联系我juni1289@hotmail.com 我从事数据挖掘项目已经3年了!

标签: java wordnet synonym jwi


【解决方案1】:

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

    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>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

【讨论】:

  • 如何处理 wordnetexception。我尝试输入其他关键字,如 light 或 ball 它会引发 wordnet 异常。顺便说一句,你的 if 条件有额外的括号
  • 异常类似于“解析同义词集数据时发生错误:: 00292635 30 v 05 light 0 ilume 0 illumine 0 light_up 0Illumination 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 + 03670692 n 0101 + 11494354 n 0101 + 11494354 n 0101〜00293009 V 0000 02 + 08 00 + 11 00 |使更轻或更亮;“这灯照亮了房间一点”
【解决方案2】:

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

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

【讨论】:

  • 您可以为给定单词的每个 POS 调用 getSenseEntryIterator() 吗?还是会重复?
  • @219CID 是的,我想如果你想要每个可能的词性的每个同义词,你会添加一个外部循环,它将遍历每个 POS 标签。
  • 谢谢,我成功地做到了这一点,并且能够获得这些独特的容量同义词:CAPABILITY CONTENT CAPACITANCE ELECTRICALCAPACITY MENTALABILITY
  • 我想知道你是否可以看看这个相关的 JWI/WordNet 问题:stackoverflow.com/questions/65403290/…
【解决方案3】:

如果您想使用 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!");
        }

效果很好。

【讨论】:

    猜你喜欢
    • 2021-08-06
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多