OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/speech/speech_recognition_request.h" | 5 #include "chrome/browser/speech/speech_recognition_request.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/common/net/url_request_context_getter.h" | 11 #include "chrome/common/net/url_request_context_getter.h" |
12 #include "net/base/load_flags.h" | 12 #include "net/base/load_flags.h" |
13 #include "net/url_request/url_request_status.h" | 13 #include "net/url_request/url_request_status.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 const char* const kHypothesesString = "hypotheses"; | 17 const char* const kHypothesesString = "hypotheses"; |
18 const char* const kUtteranceString = "utterance"; | 18 const char* const kUtteranceString = "utterance"; |
| 19 const char* const kConfidenceString = "confidence"; |
19 | 20 |
20 bool ParseServerResponse(const std::string& response_body, string16* value) { | 21 bool ParseServerResponse(const std::string& response_body, |
21 DCHECK(value); | 22 speech_input::SpeechInputResultArray* result) { |
22 | |
23 if (response_body.empty()) { | 23 if (response_body.empty()) { |
24 LOG(WARNING) << "ParseServerResponse: Response was empty."; | 24 LOG(WARNING) << "ParseServerResponse: Response was empty."; |
25 return false; | 25 return false; |
26 } | 26 } |
27 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body; | 27 DVLOG(1) << "ParseServerResponse: Parsing response " << response_body; |
28 | 28 |
29 // Parse the response, ignoring comments. | 29 // Parse the response, ignoring comments. |
30 std::string error_msg; | 30 std::string error_msg; |
31 scoped_ptr<Value> response_value(base::JSONReader::ReadAndReturnError( | 31 scoped_ptr<Value> response_value(base::JSONReader::ReadAndReturnError( |
32 response_body, false, NULL, &error_msg)); | 32 response_body, false, NULL, &error_msg)); |
(...skipping 21 matching lines...) Expand all Loading... |
54 VLOG(1) << "ParseServerResponse: Unexpected hypotheses type " | 54 VLOG(1) << "ParseServerResponse: Unexpected hypotheses type " |
55 << hypotheses_value->GetType(); | 55 << hypotheses_value->GetType(); |
56 return false; | 56 return false; |
57 } | 57 } |
58 const ListValue* hypotheses_list = static_cast<ListValue*>(hypotheses_value); | 58 const ListValue* hypotheses_list = static_cast<ListValue*>(hypotheses_value); |
59 if (hypotheses_list->GetSize() == 0) { | 59 if (hypotheses_list->GetSize() == 0) { |
60 VLOG(1) << "ParseServerResponse: hypotheses list is empty."; | 60 VLOG(1) << "ParseServerResponse: hypotheses list is empty."; |
61 return false; | 61 return false; |
62 } | 62 } |
63 | 63 |
64 Value* first_hypotheses = NULL; | 64 size_t index = 0; |
65 if (!hypotheses_list->Get(0, &first_hypotheses)) { | 65 for (; index < hypotheses_list->GetSize(); ++index) { |
66 LOG(WARNING) << "ParseServerResponse: Unable to read hypotheses value."; | 66 Value* hypothesis = NULL; |
67 return false; | 67 if (!hypotheses_list->Get(index, &hypothesis)) { |
| 68 LOG(WARNING) << "ParseServerResponse: Unable to read hypothesis value."; |
| 69 break; |
| 70 } |
| 71 DCHECK(hypothesis); |
| 72 if (!hypothesis->IsType(Value::TYPE_DICTIONARY)) { |
| 73 LOG(WARNING) << "ParseServerResponse: Unexpected value type " |
| 74 << hypothesis->GetType(); |
| 75 break; |
| 76 } |
| 77 |
| 78 const DictionaryValue* hypothesis_value = |
| 79 static_cast<DictionaryValue*>(hypothesis); |
| 80 string16 utterance; |
| 81 if (!hypothesis_value->GetString(kUtteranceString, &utterance)) { |
| 82 LOG(WARNING) << "ParseServerResponse: Missing utterance value."; |
| 83 break; |
| 84 } |
| 85 |
| 86 // It is not an error if the 'confidence' field is missing. |
| 87 double confidence = 0.0; |
| 88 hypothesis_value->GetReal(kConfidenceString, &confidence); |
| 89 |
| 90 result->push_back(speech_input::SpeechInputResultItem(utterance, |
| 91 confidence)); |
68 } | 92 } |
69 DCHECK(first_hypotheses); | 93 |
70 if (!first_hypotheses->IsType(Value::TYPE_DICTIONARY)) { | 94 if (index < hypotheses_list->GetSize()) { |
71 LOG(WARNING) << "ParseServerResponse: Unexpected value type " | 95 result->clear(); |
72 << first_hypotheses->GetType(); | |
73 return false; | |
74 } | |
75 const DictionaryValue* first_hypotheses_value = | |
76 static_cast<DictionaryValue*>(first_hypotheses); | |
77 if (!first_hypotheses_value->GetString(kUtteranceString, value)) { | |
78 LOG(WARNING) << "ParseServerResponse: Missing utterance value."; | |
79 return false; | 96 return false; |
80 } | 97 } |
81 | 98 |
82 return true; | 99 return true; |
83 } | 100 } |
84 | 101 |
85 } // namespace | 102 } // namespace |
86 | 103 |
87 namespace speech_input { | 104 namespace speech_input { |
88 | 105 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 const URLFetcher* source, | 139 const URLFetcher* source, |
123 const GURL& url, | 140 const GURL& url, |
124 const URLRequestStatus& status, | 141 const URLRequestStatus& status, |
125 int response_code, | 142 int response_code, |
126 const ResponseCookies& cookies, | 143 const ResponseCookies& cookies, |
127 const std::string& data) { | 144 const std::string& data) { |
128 DCHECK_EQ(url_fetcher_.get(), source); | 145 DCHECK_EQ(url_fetcher_.get(), source); |
129 DCHECK(url_.possibly_invalid_spec() == url.possibly_invalid_spec()); | 146 DCHECK(url_.possibly_invalid_spec() == url.possibly_invalid_spec()); |
130 | 147 |
131 bool error = !status.is_success() || response_code != 200; | 148 bool error = !status.is_success() || response_code != 200; |
132 string16 value; | 149 SpeechInputResultArray result; |
133 if (!error) | 150 if (!error) |
134 error = !ParseServerResponse(data, &value); | 151 error = !ParseServerResponse(data, &result); |
135 url_fetcher_.reset(); | 152 url_fetcher_.reset(); |
136 | 153 |
137 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; | 154 DVLOG(1) << "SpeechRecognitionRequest: Invoking delegate with result."; |
138 delegate_->SetRecognitionResult(error, value); | 155 delegate_->SetRecognitionResult(error, result); |
139 } | 156 } |
140 | 157 |
141 } // namespace speech_input | 158 } // namespace speech_input |
OLD | NEW |