2

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 Answers2

1

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("}");
            }
        }


    }

}
0

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)
Titus
  • 22,031
  • 1
  • 23
  • 33
  • eclipse doesn't like that. can't use && with booleans he says. –  Jan 30 '15 at 06:26
  • the doc says this (The list will neither be null or empty, or contain null.) with regard to [ getWordIDs() ] maybe we can just have a check for that? –  Jan 30 '15 at 06:27
  • @Kordelia Yes you are right, the problem was `idxWord.size()` you can remove that and everything should work. – Titus Jan 30 '15 at 06:29
  • it didn't work actually. you don't think overriding size would be helpful? i don't know why i thought that was a good idea. –  Jan 30 '15 at 06:32
  • hmm i just changed some things around. i can tell you in a minute. but maybe it would be prudent to look at just [ dict.getIndexWord( input_word, POS.VERB ) ] –  Jan 30 '15 at 06:44
  • Exception in thread "main" java.lang.NullPointerException at inference_learner.WordNet.getHypernyms(WordNet.java:54) at inference_learner.Sentence.(Sentence.java:25) at inference_learner.RegEx.match_regex_patterns(RegEx.java:33) at inference_learner.ReadFile.readFile(ReadFile.java:30) at inference_learner.Main.main(Main.java:38) –  Jan 30 '15 at 06:46
  • i got it, posted it as an answer. –  Jan 30 '15 at 06:57
  • @Kordelia I'm glad that you did, it seems that the `getIndexWord(...)` method was returning `null`. Is good that you've posted the answer, maybe someone with the same problem won't loose so much time on it because of that. – Titus Jan 30 '15 at 07:11
  • yeah, this site is SO AWESOME! haha –  Jan 30 '15 at 07:19