2013-02-13 97 views
1

嗨,朋友,我正在做我的最后一年的项目语义相似性之间的句子。 所以我使用word-net 2.1数据库来检索含义。每条线我都不得分裂。在每一个字我得到意义和存储到数组中。但它只能得到第一句话的意思。扭曲射击java.lang.ArrayIndexOutOfBoundsException

String[] sentences = result.split("[\\.\\!\\?]"); 
for (int i=0;i<sentences.length;i++) 
      { 
       System.out.println(i); 
       System.out.println(sentences[i]); 
       int wcount1 = sentences[i].split("\\s+").length; 
       System.out.println(wcount1);int wcount1=wordCount(w2); 
      System.out.println(wcount1); 
     String[] word1 = sentences[i].split(" "); 
     for (int j=0;j<wcount1;j++){ 
      System.out.println(j); 

     System.out.println(word1[j]); 
    } 
      } 

     IndexWordSet set = wordnet.lookupAllIndexWords(word1[j]); 
     System.out.println(set); 
     IndexWord[] ws = set.getIndexWordArray(); 

     **POS p = ws[0].getPOS();///line no 103** 

     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); 

OUTPUT: 只有sentences[o](第一句词的只有鞋的含义......一切换句话说不是循环...) 它表明这个错误..

**java.lang.ArrayIndexOutOfBoundsException: 0 
at first_JWNL.main(first_JWNL.java:102)** 
+1

你的问题似乎不完整。你能修改吗?另外,你会得到哪些错误? – kufudo 2013-02-13 06:09:55

回答

0

当你声明变量wcount1,你赋值为:sentences[i].split("\\s+")..。然而,当您分配变量word1时,它将被分配sentences[i].split(" ")

是否有可能,因为您使用了两个正则表达式,第二个分割(它被分配给word1变量)没有正确分割?因此,当您访问该值(System.out.println(word1[j]);)时,它会抛出ArrayIndexOutOfBoundsException。由于wcount1的值可能大于word1的长度。