From e1fec4e8183a3cd451330e7e5619cb2e6ded4868 Mon Sep 17 00:00:00 2001 From: dabinat Date: Fri, 14 Feb 2020 19:19:14 -0800 Subject: [PATCH] Client - Change JSON output to return alternatives transcripts in an "alternatives" array --- native_client/client.cc | 52 +++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/native_client/client.cc b/native_client/client.cc index ffe3b518..413be288 100644 --- a/native_client/client.cc +++ b/native_client/client.cc @@ -47,6 +47,7 @@ struct meta_word { char* metadataToString(Metadata* metadata); std::vector WordsFromMetadata(Metadata* metadata); char* JSONOutput(Result* result); +std::string MetadataOutput(Metadata* metadata); ds_result LocalDsSTT(ModelState* aCtx, const short* aBuffer, size_t aBufferSize, @@ -341,35 +342,56 @@ char* JSONOutput(Result* result) { std::ostringstream out_string; - out_string << "[\n"; + out_string << "{\n"; for (int j=0; j < result->num_transcriptions; ++j) { Metadata *metadata = &result->transcriptions[j]; - std::vector words = WordsFromMetadata(metadata); - out_string << R"({"metadata":{"confidence":)" << metadata->confidence << R"(},"words":[)"; + if (j == 0) { + out_string << MetadataOutput(metadata); - for (int i = 0; i < words.size(); i++) { - meta_word w = words[i]; - out_string << R"({"word":")" << w.word << R"(","time":)" << w.start_time << R"(,"duration":)" << w.duration << "}"; - - if (i < words.size() - 1) { - out_string << ","; + if (result->num_transcriptions > 1) { + out_string << ",\n" << R"("alternatives")" << ":[\n"; } - } + } else { + out_string << "{" << MetadataOutput(metadata) << "}"; - out_string << "]}"; - - if (j < result->num_transcriptions - 1) { - out_string << ",\n"; + if (j < result->num_transcriptions - 1) { + out_string << ",\n"; + } else { + out_string << "\n]"; + } } } - out_string << "\n]\n"; + out_string << "\n}\n"; return strdup(out_string.str().c_str()); } +std::string +MetadataOutput(Metadata *metadata) +{ + std::ostringstream out_string; + + std::vector words = WordsFromMetadata(metadata); + + out_string << R"("metadata":{"confidence":)" << metadata->confidence << R"(},"words":[)"; + + for (int i = 0; i < words.size(); i++) { + meta_word w = words[i]; + out_string << R"({"word":")" << w.word << R"(","time":)" << w.start_time << R"(,"duration":)" << w.duration << "}"; + + if (i < words.size() - 1) { + out_string << ","; + } + } + + out_string << "]"; + + return out_string.str(); +} + int main(int argc, char **argv) {