如果给出超过3个字的话,Android文本函数forceclose
即时消息来自C++的java新手,并且快速学习将chatbot转换为java android应用程序的不同实践。这个功能在这里强制关闭,如果它超过3个不同的单词,我不知道为什么。如果有人能帮我打败这个谜语,我会很高兴!如果给出超过3个字的话,Android文本函数forceclose
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
}
的logcat:
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;
logcat中所指向的行:
if(!context.get(trigger).isEmpty()) {
Collections.copy(ary, context.get(trigger));}
我还没有准确定位的问题,但我相信这种替代方法更快,应该躲开它。 (说实话,你的代码崩溃的第四个单词这只是奇怪,我相信还有更多的吧...反正?)
在Java 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++;
}
此外,如果trigger
不context
然后空存在,返回,不是一个空洞的列表,所以你也许可以做一个简单的变化:
if(context.get(trigger) != null && !context.get(trigger).isEmpty()) {
Collections.copy(ary, context.get(trigger));
}
但这应该抛出NullPointerException不是一个出界的。我还是不知道为什么第四个词是致命的。希望这可以帮助。
谢谢队友!最后得到一个可能的解决方案让我非常开心!我在巴士atm,所以我会回来的结果,当我回家! :-) – user1419305
是的!工作!现在这条线路正在令人厌烦。 sentence.add(words.get(rand.nextInt(words.size()))); – user1419305
因为这是一个不同的问题,你应该问一个新的问题,并包括logcat错误,因为该行看起来不错(假设'单词'确实包含至少一个单词)... – Sam
当你得到一个'ArrayIndexOutOfBoundsException'时,只需在检查变量/循环的时候遍历代码(调试)。 – keyser
这是循环'for(int i = 0; i
Sam
是的,这是获取内容的唯一场所! – user1419305