2013-02-13 57 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的長度。