lkuza2 / java-speech-api

The J.A.R.V.I.S. Speech API is designed to be simple and efficient, using the speech engines created by Google to provide functionality for parts of the API. Essentially, it is an API written in Java, including a recognizer, synthesizer, and a microphone capture utility. The project uses Google services for the synthesizer and recognizer. While this requires an Internet connection, it provides a complete, modern, and fully functional speech API in Java.
GNU General Public License v3.0
535 stars 301 forks source link

RecognizerChunked can not parse result correctly #46

Closed wugh closed 9 years ago

wugh commented 10 years ago

RecognizerChunked's parseResponse function paser fellow response

{"result":[{"alternative":[{"transcript":"This is a apple","confidence":0.97342438},{"transcript":"This is a apple"},{"transcript":"This is a apple"}],"final":true}],"result_index":0}

Making 0.97342438 as element of otherPossibleResponses ArrayList, result as

Google thinks you said: This is a apple
with 97.342438% confidence.
Google also thinks that you might have said:[0.97342438, This is a apple, This is a apple]

I would like to adapt java-json to parse raw response

    /**
     * Parses the response into a Google Response
     * @param rawResponse The raw String you want to parse
     * @param gr The GoogleResponse you want to parse into ti.
     */
    private void parseResponse(String rawResponse, GoogleResponse gr){
        if(rawResponse == null || !rawResponse.contains("\"result\"")){ return; }

        JSONObject object = new JSONObject(rawResponse);
        JSONObject result = object.getJSONArray("result").getJSONObject(0);
        JSONArray ASRHypotheses = result.getJSONArray("alternative");
        String response = "";
        String confidence = "";
        if (ASRHypotheses.length() == 1) {
            //  Google is 100% confident in it's translation
            response = ASRHypotheses.getJSONObject(0).getString("transcript");
            if (ASRHypotheses.getJSONObject(0).has("confidence")) {
                confidence = String.valueOf(ASRHypotheses.getJSONObject(0).getDouble("confidence"));
            }
            else {
                confidence = String.valueOf(1d);
            }
        }
        else {
            String otherResponse;
            for (int i = 0; i < ASRHypotheses.length(); i++) {
                if (i == 0) {
                    response = ASRHypotheses.getJSONObject(i).getString("transcript");
                    confidence = String.valueOf(ASRHypotheses.getJSONObject(i).getDouble("confidence"));
                } else {
                    System.out.println(ASRHypotheses.getJSONObject(i));
                    otherResponse = ASRHypotheses.getJSONObject(i).getString("transcript");
                    gr.getOtherPossibleResponses().add(otherResponse);
                }
            }
        }
        gr.setResponse(response);
        gr.setConfidence(confidence);
    }
Skylion007 commented 10 years ago

Interesting, I'd rather try to limit the number of third party dependencies though. I will working into fixing this bug with manual re-parsing later. Thanks for reporting it.