有谁知道如何检索两个单词之间的第一个常见的上位词?我可以从给定的单词访问第一级(直接父级),但我仍然坚持如何从该单词中检索所有上位词("上升")直到它与另一个单词匹配。这个想法是确定何时/何时/可以考虑哪两个单词"相同"通过WordNet根据他们的根(如果没有找到它应该继续,直到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("}");
}
}
提供字典和单词&#34; dog&#34;我们有结果(你可以看到我只是使用第一个意思来执行这个方法):
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}
答案 0 :(得分:1)
对于那些可能感兴趣的人。过了一段时间我才发现它。
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;
}