使用 WordNet (JWI) 的两个单词之间的常用上位词 - JAVA

Common Hypernym between two words using WordNet (JWI) - JAVA

有谁知道检索两个词之间第一个常用上位词的好方法吗?我可以从给定的单词访问第一级(直接父级),但我一直在研究如何从这个单词中检索所有上位词 ("going up"),直到它与另一个单词匹配。思路是通过WordNet根据词根识别where/when/which两个可以认为是"the same"的词(如果没有找到应该继续到wordnet中词的末尾)。我在这里找到了一些主题,但对于 Python 和 Perl,在 JAVA

中没有针对此问题的具体内容

我正在使用 JWI (2.4.0) 从 WordNet 访问 SynsetID、WordID 和其他信息。如果有更简单的 API 也欢迎。下面是提供我提到的上位词的方法。

 public void getHypernyms(IDictionary dict_param, String lemma_param) throws IOException {
    dict_param.open();
    // get the synset
    IIndexWord idxWord = dict_param.getIndexWord(lemma_param, POS.NOUN);

    // 1st meaning
    IWordID wordIDb = idxWord.getWordIDs().get(0);
    IWord word = dict_param.getWord(wordIDb);

    ISynset synset = word.getSynset();
    System.out.println("Synset = " + synset);

    // get the hypernyms by pointing a list of <types> in the words
    List<ISynsetID> hypernyms = synset.getRelatedSynsets(Pointer.HYPERNYM);

    // print out each h y p e r n y m s id and synonyms
    List<IWord> words, wordsb;

    for (ISynsetID sid : hypernyms) {

        words = dict_param.getSynset(sid).getWords();
        System.out.println("Lemma: " + word.getLemma());
        System.out.print("Hypernonyms = " + sid + " {");

        for (Iterator<IWord> i = words.iterator(); i.hasNext();) {
            System.out.print(i.next().getLemma());

            if (i.hasNext()) {
                System.out.print(", ");
            }

        }
        System.out.println("}");

    }

}

提供字典和单词 "dog" 结果(如您所见,我只是使用第一个含义来执行此方法):

Synset = SYNSET{SID-02084071-N : Words[W-02084071-N-1-dog, W-02084071-N-2 domestic_dog, W-02084071-N-3-Canis_familiaris]}

Lemma: dog Hypernonyms = SID-02083346-N {canine, canid} 
Lemma: dog Hypernonyms = SID-01317541-N {domestic_animal, domesticated_animal}

对于那些可能感兴趣的人。过了一段时间我弄明白了。

    public List<ISynsetID> getListHypernym(ISynsetID sid_pa) throws IOException {
    IDictionary dict = openDictionary();
    dict.open(); //Open the dictionary to start looking for LEMMA
    List<ISynsetID> hypernym_list = new ArrayList<>();

    boolean end = false;

    while (!end) {
        hypernym_list.add(sid_pa);
        List<ISynsetID> hypernym_tmp = dict.getSynset(sid_pa).getRelatedSynsets(Pointer.HYPERNYM);
        if (hypernym_tmp.isEmpty()) {
            end = true;
        } else {
            sid_pa = hypernym_tmp.get(0);//we will stick with the first hypernym
        }

    }

    //for(int i =0; i< hypernym_list.size();i++){
    //    System.out.println(hypernym_list.get(i));
    //}
    return hyp;
}