How to override .size() method to examine List<IWordID> java jwi?

886 Views Asked by At

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;
}
2

There are 2 best solutions below

0
On

I got it, with much help from @Titus

public void getHypernyms( String input_word ) throws IOException
{   
    System.out.println( "input_word : " + input_word);
    // get the synset of 'input_word'
    //IIndexWord idxWord = dict . getIndexWord (inut_word, POS . NOUN ) ;
    IIndexWord idxWord = dict.getIndexWord( input_word, POS.VERB );

    if(dict.getIndexWord( input_word, POS.VERB ) == null)
    {
        System.out.println( "got ya'!");
    }
    else
    {

        System.out.println( "idxWord: " + idxWord );
        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("}");
            }
        }


    }

}
9
On

I think that this is the line that causes the problem

 if( idxWord != null && idxWord.size() > 0)

I took a look at the docs and it seems that the IIndexWord doesn't have a size() method. To avoid null pointer exceptions and index out of bounds exceptions you can do the check like this:

if(idxWord != null && idxWord.getWordIDs() != null && idxWord.getWordIDs().size() > 0)