Common Hypernym between two words using WordNet (JWI)
Does anyone know how a good way to retrieve the first common hypernym between two words? I can access the first level (immediate parent) from a given word, but I'm stuck on how to retrieve all hypernyms ("going up") from this word until it matches another word. The idea is to identify where/when/which two words can be considered "the same" through WordNet according with their root (if not found it should continue until the end of the words in wordnet). I found some topics here but for Python and Perl, nothing specific for this problem in JAVA
I'm using JWI (2.4.0) to access SynsetID, WordID and other information from WordNet. If there is a simpler API that does the job is also welcome. Here below is the method that provides the hypernym I mentioned.
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("}");
}
}
Providing a dictionary and the word "dog" we have as a result (as you can see I'm just usingn the first meaning to execute this method):
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}
For those who might be interested. After some time I figured it out.
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;
}
链接地址: http://www.djcxy.com/p/62418.html