首页 > 解决方案 > 在java中使用wordnet查找给定形容词的同义词和反义词集

问题描述

我正在使用 java 中的 nlp 项目,我想使用 wordnet 获取给定形容词的同义词和反义词集。到目前为止,我已经这样做了,但我没有得到想要的输出,它只是同义词和反义词集的列表。

    net.didion.jwnl.dictionary.Dictionary dict;
    File file = new File("properties.xml");
    JWNL.initialize(new FileInputStream(file));
    dict = net.didion.jwnl.dictionary.Dictionary.getInstance();

    IndexWord indexWord = dict.getIndexWord(POS.ADJECTIVE, "bad");

    Synset[] senses = indexWord.getSenses();


    for(int i=0;i<senses.length;i++){
        Synset set= senses[i];
        Word[] words=set.getWords();
        for(int k=0;k<words.length;k++){
            System.out.println(i+","+k+":"+words[k].getLemma());
        }

    }

输出

信息:安装字典 net.didion.jwnl.dictionary.FileBackedDictionary@70177ecd

好,充分,好,好,可贵,好,可敬,可敬,有益,好,好,好,公正,正直,熟练,专家,好,熟练,熟练,熟练,熟练,好,亲爱的,好,近,可靠,好,安全,安全,好,正确,成熟,好,好(p),有效,好,in_effect(p),in_force(p),好,好,严重,好,健全,好,有益,好, 诚实, 好, 未受破坏, 未受破坏, 好,

标签: javanlpwordnet

解决方案


你需要过滤你的答案,

Synset syn;
       String word = null;
       ArrayList syns = new ArrayList<>();
         for(int i=0;i<sensesVerbe.length;i++){
            // syn=sensesVerbe[i];          
            //System.out.println(sensesVerbe[i]); 
           
            String v= sensesVerbe[i].toString();
            //String v= sensesVerbe[i].toString();
            if(v.contains("Words:")){
                          //System.out.println("yeeh");
                          //System.out.println("Index of NN = "+ line.indexOf("N "));
                          int chaine = v.indexOf("Words:");
                          //System.out.println("index Words: ="+ chaine+"");
                          
                          int chaine1 = v.indexOf("--");
                          //System.out.println("index -- ="+ chaine1+"");
                          
                          word = v.substring(chaine+6, chaine1);
                          //System.out.println("the sub of line where index is ("+ chaine +","+ chaine1 +") is :" + word);
                         
                          //System.out.println(word); 
                          String mots[] =word.split(",");
                          //System.out.println("mots séparés :"); 
                          for (int j = 0; j < mots.length; j++) {
                                mots[j]=mots[j].replaceAll(" ", "");
                                //System.out.println(mots[j]);
                                if(!syns.contains(mots[j].toLowerCase())){
                                   syns.add(mots[j].toLowerCase());
                                }
                            }                                                    
               }
         }


推荐阅读