我的 Android 应用程序有问题,它是一个使用语音识别和 Google TTS 的应用程序。它就像一个 SIRI 客户端。正如您在此处看到的,当用户说出数组中给定的单词时:
String[] g = { "hallo", "heey", "hoi", "hey", "he", "hee", "hay" };
for (String strings : g) {
if (mostLikelyThingHeard.contains(strings)) {
String[] array = { "leuk dat je er bent", "heeyy" };
String randomStr = array[new Random()
.nextInt(array.length)];
tts.speak(randomStr, TextToSpeech.QUEUE_FLUSH, null);
return;
}
}
String[] f = { "haha", "hah", "ha" };
for (String strings : f) {
if (mostLikelyThingHeard.contains(strings)) {
String[] array = { "haha leuk grapje", "hiehaho" };
String randomStr = array[new Random()
.nextInt(array.length)];
tts.speak(randomStr, TextToSpeech.QUEUE_FLUSH, null);
return;
}
}
一切正常,但当用户说“hallo”时,它首先检测到前两个字符“ha”。字符串数组“f”中提到了这一点。所以这很烦人,整个词都没有被检测到,而只是其中的一部分。
当我像这样交换两个字符串数组时:
String[] f = { "haha", "hah", "ha" };
for (String strings : f) {
if (mostLikelyThingHeard.contains(strings)) {
String[] array = { "haha leuk grapje", "hiehaho" };
String randomStr = array[new Random()
.nextInt(array.length)];
tts.speak(randomStr, TextToSpeech.QUEUE_FLUSH, null);
return;
}
}
String[] g = { "hallo", "heey", "hoi", "hey", "he", "hee",
"hay" };
for (String strings : g) {
if (mostLikelyThingHeard.contains(strings)) {
String[] array = { "leuk dat je er bent", "heeyy" };
String randomStr = array[new Random()
.nextInt(array.length)];
tts.speak(randomStr, TextToSpeech.QUEUE_FLUSH, null);
return;
}
}
然后它首先检测到“hallo”而不是“ha”
但是如果我制作超过 100 个数组,这会很烦人,那么我怎样才能让 java 使用数组中最匹配的词而不是仅仅一部分呢?
我知道这很难理解,但如果你们不理解,请在此处查看我的来源: http://github.com/gi097/PWS
编辑:
当我改变
contains
到
equals
我解决了这个问题,但现在我遇到了一个新问题:
如果我创建一个像这样的数组:
"I am"
当用户说:“我是 Giovanni”时,我们遇到了“我是”由于等号而不再被检测到的问题......
编辑2
我认为可以通过将 mostLikelythingHeard“我是 Giovanni”拆分为“我”"is"“Giovanni”来解决这个问题,但是如何解决?
最佳答案
代替
mostLikelyThingHeard.contains(strings)
使用:
Arrays.sort(g);
int index = Arrays.binarySearch(g, mostLikelyThingHeard);
if (index >= 0) {
System.out.println("I heard it properly and its ", g[index]);
}
包含对 String 子序列的检查,如前所述 here .因此,如果您的字符串是“hallo”,则 "Hallo".contains("ha")
在 hallo 内搜索 ha,是的,ha 在 hallo 内,因此您的问题。
关于java - 数组只检测输入单词的一部分而不是整个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27378568/