2012-05-26 2 views
0

이 자바에 새롭고, C++에서 온 것이고, 다른 chatbot을 java android app으로 변환하는 방법을 빨리 배울 수 있습니다. 이 함수는 3 가지 이상의 단어를 가져 오면 강제 종료됩니다. 왜 그런지 모르겠습니다. 누군가가이 수수께끼를 이길 수 있으면 나는 감격 할 것이다!Android 텍스트 함수 forcecloses가 3 단어 이상이 주어지면

public void AI(String text) { 
// initiate new lists to be on the safe side 
List<String> indexer = new ArrayList<String>(); 
List<String> sentence = new ArrayList<String>(); 
List<String> explode = new ArrayList<String>(); 
List<String> ary = new ArrayList<String>(); 

explode = Arrays.asList(text.split(" ")); 

// initiate randint and trigger variable 
Random rand = new Random(); 
int randint = rand.nextInt(explode.size()); 

// initiate the trigger variable 
String trigger = explode.get(randint); 


    // check if word exists in database and add if it not. 
for(int i = 0; i < explode.size(); i++) { 
    String word = explode.get(i); 

if(common.get(word)==null) { 
         words.add(word); 
         common.put(word, 1); 
         context.put(word, explode); 
         pos.put(word, i); 
         length.put(word, explode.size()); 

         }else{ 
     // increase the weight of common if the word repeats 
         common.put(word, common.get(word)+1); 
            } 
            } 
    //check if context with index set as trigger is empty, if not, copy the arraylist to the ary variable 
    if(!context.get(trigger).isEmpty()) { 
      Collections.copy(ary, context.get(trigger));} 


    // fill the arraylist sentence with words to be used, some in context, some random from the database. 
for(int i2 = 0; i2 < length.get(trigger); i2++) { 

        randint = rand.nextInt(length.get(trigger)); 

if(randint < length.get(trigger)/2) { 
             // 
             if(ary.get(i2)!=null) { 
                    sentence.add(ary.get(i2)); 
                   } 
            }else{ 
         sentence.add(words.get(rand.nextInt(words.size()))); 
             } 

}  

    // use to the pos-hashmap to check at which index the word was detected at, if not  place it at the deliberate index. 
for(int i3 = 0; i3 < sentence.size(); i3++) { 
              if(sentence.get(i3)!=null) {  
              indexer.add(pos.get(sentence.get(i3)), sentence.get(i3)); 
                     } 
              } 

// compose the final string that is to be passed to the speak function 
for(int i4 = 0; i4 < indexer.size(); i4++) { 
say = say + indexer.get(i4)+" ";  
    } 

// pass the string to the speak function 
mTts.speak(say, TextToSpeech.QUEUE_FLUSH, null);  


// removing final string to be ready for next iteration 
    say = ""; 



// end of AI stuff   
}  

로그 캣 :

05-26 17:40:08.266: E/AndroidRuntime(490): Uncaught handler: thread main exiting due to uncaught exception 
05-26 17:40:08.276: E/AndroidRuntime(490): java.lang.ArrayIndexOutOfBoundsException 
05-26 17:40:08.276: E/AndroidRuntime(490): at java.util.Collections.copy(Collections.java:1593) 
05-26 17:40:08.276: E/AndroidRuntime(490): at sarah.namespace.SarahActivity.AI(SarahActivity.java:163) 

변수 :

// arraylists 
public List<String> explode = new ArrayList<String>(); 
    public List<String> words = new ArrayList<String>(); 
    public List<String> sentence = new ArrayList<String>(); 
    public List<String> indexer = new ArrayList<String>(); 
// hashmaps 
    public Map<String, Integer> common = new HashMap<String, Integer>(); 
public Map<String, List<String>> context = new HashMap<String, List<String>>(); 
public Map<String, Integer> pos = new HashMap<String, Integer>(); 
public Map<String, Integer> length = new HashMap<String, Integer>(); 

// strings 
public String say; 

로그 캣가 가리키는 라인 : 나는 문제를 정확히 지적하지 않은

if(!context.get(trigger).isEmpty()) { 
      Collections.copy(ary, context.get(trigger));} 
+0

'ArrayIndexOutOfBoundsException'을 얻으면 변수/루프를 검사하면서 코드 (디버그)를 단계별로 실행하십시오. – keyser

+0

this loop'for (int i = 0; i Sam

+0

예, 컨텍스트에서 콘텐츠를 가져 오는 유일한 장소입니다! – user1419305

답변

0

,하지만 난 믿는다 이 대체 방법은 더 빠르며 그것을 피하십시오. (솔직히, 당신의 코드는 네 번째 단어에 충돌 그건 그냥 이상한 내가 더이 생각 ... 어쨌든?.) 자바 for-each loops에서

때문에, 빠르고 간단 : 또한

// check if word exists in database and add if it not. 
int i = 0; 
for(String word : explode) { 
    if(common.get(word) == null) { 
     if(word.equals(trigger)) { 
      ary = new ArrayList<String>(explode); 
     } 

    ... 
    i++; 
} 

경우 나는 아직도 ...

if(context.get(trigger) != null && !context.get(trigger).isEmpty()) { 
    Collections.copy(ary, context.get(trigger)); 
} 

그러나 이것은 NullPointerException이되지에게 경계의 밖으로 던져해야합니다 간단한 변경을 할 수있을 수 있도록 trigger가 아닌 빈 목록 반환 다음 context 널 (null)에 존재하지 않는 왜 그 일을 몰라. e 네 번째 단어는 치명적이다. 희망이 도움이됩니다.

+0

고마워요! 마침내 가능한 해결책을 얻으면 정말 행복했습니다! 나는 버스 ATM에있다. 그래서 내가 집으로 돌아올 때 결과를보고 할 것이다! :-) – user1419305

+0

예! 그게 효과가! 이제이 선은 엉망이되고 있습니다. sentence.add (words.get (rand.nextInt (words.size()))); – user1419305

+0

다른 문제이므로 새로운 단어를 물어보고 logcat 오류를 포함시켜야합니다. ('단어'에 적어도 하나 이상의 단어가 포함되어 있다고 가정하면 ...) – Sam

관련 문제