I am using the java wordnet interface JWI to try to generate the hypernyms of words, generalizing them from specific entities to higher-order concept/types.
There part of my code where I want to make sure that a word registers with wordnet, since sometimes I have input like isa
, which corresponds to is a
but wordnet doesn't recognize that and my program crashes if, when, it sees this. This is the how I'm trying this right now.
public void getHypernyms( String inut_word ) throws IOException
{
// get the synset of 'input_word'
//IIndexWord idxWord = dict . getIndexWord (inut_word, POS . NOUN ) ;
IIndexWord idxWord = dict.getIndexWord( inut_word, POS.VERB );
if( idxWord != null && idxWord.size() > 0)
IWordID wordID = idxWord.getWordIDs().get (0); // 1st meaning
IWord word = dict.getWord( wordID );
ISynset synset = word.getSynset();
// get the hypernyms
List < ISynsetID > hypernyms = synset.getRelatedSynsets( Pointer.HYPERNYM );
if( hypernyms.size() > 0)
{
// print out each hypernyms id and synonyms
List < IWord > words;
for( ISynsetID sid : hypernyms )
{
words = dict.getSynset( sid ).getWords ();
System.out.print( sid + " {");
for( Iterator <IWord> i = words.iterator(); i.hasNext(); )
{
System.out.print( i.next().getLemma() );
if( i.hasNext() )
System.out.print(", ");
}
System.out.println("}");
}
}
else
{
System.out.println( inut_word );
}
}
But eclipse warns me that method size() is not defined for type IIndexWord
.
I think this means I need to @override
size, isn't that right? But I've never actually done that before, how to do that?
java.util.List.size specifically.
I was trying to implement this method like this, similar one, that works like a charm.
public String getStem(String word)
{
WordnetStemmer stem = new WordnetStemmer( dict );
List<String> stemmed_words = stem.findStems(word, POS.VERB);
if( stemmed_words != null && stemmed_words.size() > 0)
return stemmed_words.get(0);
else
return word;
}
I got it, with much help from @Titus