使用webkit语音识别调用Watson API无响应问题求助
Let’s walk through the possible issues and fixes for your problem where transcribed speech containing a question mark isn’t being processed correctly by Watson Assistant, while keyboard input works fine.
1. First, Verify the Transcript Content and API Call
The most basic check is to confirm exactly what’s being sent to Watson. Add logging to see the raw transcript and ensure the API call is triggering as expected:
if(finalTranscripts.includes('?')){ // Log the exact transcript to check for unexpected formatting console.log('Transcript being sent to Watson:', JSON.stringify(finalTranscripts)); Api.sendRequest(finalTranscripts, this); }
Look for anomalies like extra spaces before the question mark (e.g., "Hello ?" instead of "Hello?"), hidden special characters, or line breaks that might break Watson’s parsing.
2. Fix the this Context in the API Call
In the speechRecognizer.onresult callback, this refers to the speech recognition instance—not the same context used by the original inputKeyDown function (which was the input element). Watson’s API might expect a reference to the chat input context here.
Capture the correct context upfront to match the original behavior:
function startConverting (){ // Capture the input element context to pass to the API const inputElement = document.getElementById('textInput'); if('webkitSpeechRecognition' in window){ var speechRecognizer = new webkitSpeechRecognition(); // ... rest of your speech recognition setup ... speechRecognizer.onresult = function(event){ // ... transcript processing logic ... if(finalTranscripts.includes('?')){ // Use the captured input element instead of `this` Api.sendRequest(finalTranscripts, inputElement); } }; } }
3. Ensure You’re Only Sending Final Transcripts
Speech recognition’s interimResults can cause partial transcripts to be processed before the final result is confirmed. Plus, your original code accumulated finalTranscripts across recognition sessions, leading to outdated text being sent. Reset and check for final results properly:
speechRecognizer.onresult = function(event){ var interimTranscripts = ''; var finalTranscripts = ''; // Reset to avoid old transcript buildup for(var i = event.resultIndex; i < event.results.length; i++){ var transcript = event.results[i][0].transcript; transcript = transcript.replace("\n", "<br>"); if(event.results[i].isFinal){ finalTranscripts += transcript; // Only trigger the API call once we have a complete, final transcript if(finalTranscripts.includes('?')){ console.log('Final transcript being sent:', finalTranscripts); Api.sendRequest(finalTranscripts, inputElement); } }else{ interimTranscripts += transcript; } } r.innerHTML = finalTranscripts + '<span style="color:#999">' + interimTranscripts + '</span>'; };
4. Add Error Handling to Catch Hidden Issues
Your current code doesn’t log errors from either speech recognition or Watson’s API. Add logging to diagnose failures:
// Log speech recognition errors speechRecognizer.onerror = function (event) { console.error('Speech Recognition Error:', event.error, event.message); }; // If Api.sendRequest uses Promises, add a catch handler if(finalTranscripts.includes('?')){ Api.sendRequest(finalTranscripts, inputElement) .catch(error => console.error('Watson API Request Failed:', error)); }
If Api.sendRequest uses callbacks instead of Promises, check its documentation for an error callback parameter (e.g., Api.sendRequest(transcript, context, onSuccess, onError)).
5. Match Keyboard Input Formatting
Compare speech transcripts to manual keyboard input. Speech recognition might add extra spaces before question marks (e.g., "what's your name ?" vs. keyboard input "What's your name?"). Clean the transcript to align with Watson’s expected format:
const cleanedTranscript = finalTranscripts.trim().replace(/\s+\?/, '?'); Api.sendRequest(cleanedTranscript, inputElement);
This removes unnecessary spaces before question marks to ensure consistency with working keyboard inputs.
内容的提问来源于stack exchange,提问作者Daniel Dalan

