我正在使用java wordnet interface JWI来尝试生成单词的上位词,将它们从特定的实体概括为更高阶的概念/类型。
在我的代码中,有一部分我想要确保一个单词注册到wordnet,因为有时我有像isa这样的输入,它对应于is a,但wordnet无法识别它,如果我的程序看到这一点,它就会崩溃。这就是我现在尝试的方式。
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 );
}
}但是eclipse警告我method size() is not defined for type IIndexWord。
我想这意味着我需要@override大小,对吗?但是我以前从来没有这样做过,怎么做呢?
具体地说是java.util.List.size。
我试着像这样实现这个方法,类似的方法,效果很好。
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;
}发布于 2015-01-30 14:57:10
我得到了它,在@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("}");
}
}
}
}发布于 2015-01-30 14:24:00
我认为这就是导致问题的原因。
if( idxWord != null && idxWord.size() > 0)我看了一下docs,发现IIndexWord似乎没有size()方法。为了避免空指针异常和索引越界异常,您可以执行如下检查:
if(idxWord != null && idxWord.getWordIDs() != null && idxWord.getWordIDs().size() > 0)https://stackoverflow.com/questions/28230179
复制相似问题