【问题标题】:Array detects only a part from an input word instead of the entire word数组仅检测输入单词的一部分而不是整个单词
【发布时间】:2015-02-07 07:45:05
【问题描述】:

我的 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;
    }
}

一切正常,但是当用户说“你好”时,它首先检测到前两个字符“哈”。字符串数组'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"

当用户说:“我是乔瓦尼”时,我们遇到的问题是“我是”不再被检测到,因为 equals....

EDIT2

我认为可以通过将最有可能听到的“我是乔瓦尼”拆分为“我”“我”“乔瓦尼”来解决这个问题,但如何解决?

【问题讨论】:

    标签: java android arrays string


    【解决方案1】:

    代替

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

    包含here 中提到的字符串子序列检查。因此,如果您的字符串是“hallo”,则 "Hallo".contains("ha") 在 halo 内搜索 ha,是的 ha 在 halo 内,因此是您的问题。

    【讨论】:

    • 我不知道这是否可行,如果我有例如 100 个数组,它会检测到整个单词而不是部分字符吗?
    • 是的,它会比较整个单词。因此,如果您的字符串是 halo 并且您的数组中有 ha 和 hallo,它会找到 halo 而不是 ha。试试看。
    • 好的,我在家的时候试试:D
    • 我通过将 contains 更改为 equals 来管理它。但是现在另一个问题是每个句子都必须完全相同
    • 句子必须相同是什么意思?
    猜你喜欢
    • 2012-10-13
    • 2017-11-24
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多