0

如果有这个代码,它就像魅力一样工作,但在第一个结果之后 in_array 函数只给我“假”。我找不到问题。每次我重新开始识别,它只适用于一个短语......

var myArray = [
    'Kontakt', 
    'gehe zu Kontakt', 
    'navigiere zu Kontakt', 
    'navigiere zu Kontakte'
];

function in_array(needle, haystack, argStrict) 
{
    var key = '',
    strict = !! argStrict;
    if (strict) 
    {
        for (key in haystack) 
        {
            if (haystack[key] === needle) 
                return true;
        }
    } 
    else 
    {
        for (key in haystack) 
        {
            if (haystack[key] == needle)
                return true;
        }
    }

    return false;
};

recognition.onresult = function (event) 
{
    var pos = textArea.getCursorPosition() - interimResult.length;
    textArea.val(textArea.val().replace(interimResult, ''));
    interimResult = '';
    textArea.setCursorPosition(pos);
    for (var i = event.resultIndex; i < event.results.length; ++i) 
    {
        if (event.results[i].isFinal) 
        {
            insertAtCaret(textAreaID, event.results[i][0].transcript);
            speak = event.results[i][0].transcript;
            index = in_array(speak,myArray,false);
            alert(speak);
            alert(in_array(speak,myArray,false));
            if (index) 
            {
                alert(event.results[i][0].transcript);
            }
            else 
            {
                alert('Keine Übereinstimmung');
            }
        } 
        else 
        {
            isFinished = false;
            insertAtCaret(textAreaID, event.results[i][0].transcript + '\u200B');
            interimResult += event.results[i][0].transcript + '\u200B';
        }
    }
};

用于测试 jsfiddle:http: //jsfiddle.net/p3Fxc/

4

0 回答 0