-1

我的程序有一些问题。第 93 行有错误。如何解决这个问题?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at first_JWNL.main(first_JWNL.java:93)

代码

 for (int i=0;i<sentences.length;i++)
             {  
                 System.out.println(i);
              System.out.println(sentences[i]);
              int wcount = sentences[i].split("\\s+").length;
        String[] word1 = sentences[i].split(" ");


        for (int j=0;j<wcount;j++){  
            System.out.println(j);

         System.out.println(word1[j]);
         String sen="one";

         IndexWordSet set = wordnet.lookupAllIndexWords(word1[j]);
         IndexWord[] ws = set.getIndexWordArray(); 
         **POS p = ws[0].getPOS();**///////Line no 93

         Set<String> synonyms = new HashSet<String>();
         IndexWord indexWord = wordnet.lookupIndexWord(p, word1[j]);
         Synset[] synSets = indexWord.getSenses();
         for (Synset synset : synSets)
         {  Word[] words = synset.getWords();

            for (Word word : words)
            {  synonyms.add(word.getLemma());
            }
         }
         System.out.println(synonyms);
4

3 回答 3

0
IndexWordSet set = wordnet.lookupAllIndexWords(word1[j]);

在数组中找不到任何索引词,并返回一个空集。因此

IndexWord[] ws = set.getIndexWordArray();

返回一个零长度数组和表达式

ws[0]

导致异常。

您必须考虑到可能没有匹配项。

例如,在您的代码中,执行以下操作:

if (wordnet.size() == 0 ) {
    System.out.println("Could not find any words!!!!!");
} else {
    IndexWordSet set = wordnet.lookupAllIndexWords(word1[j]);
    IndexWord[] ws = set.getIndexWordArray(); 
    POS p = ws[0].getPOS();

    // .......
    // ETC
    // ....... 

}
于 2013-02-12T04:29:46.913 回答
0

做检查:

if(ws.length > 0){
  POS p = ws[0].getPOS();
}
于 2013-02-12T04:24:58.053 回答
0

set.getIndexWordArray ()出于某种原因返回空数组。您没有发布此方法的代码,所以我不知道为什么。

于 2013-02-12T04:25:21.440 回答