由于某种原因,
我的程序有一些问题。第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);
进行检查:
if(ws.length > 0){
POS p = ws[0].getPOS();
}
set.getIndexWordArray ()
返回空数组。你没有发布这个方法的代码,所以我不知道为什么。
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
// .......
}