|
18 | 18 |
|
19 | 19 | namespace sherpa_onnx {
|
20 | 20 |
|
21 |
| -std::string OnlineRecognizerResult::AsJsonString() const { |
22 |
| - std::ostringstream os; |
23 |
| - os << "{"; |
24 |
| - os << "\"is_final\":" << (is_final ? "true" : "false") << ", "; |
25 |
| - os << "\"segment\":" << segment << ", "; |
26 |
| - os << "\"start_time\":" << std::fixed << std::setprecision(2) << start_time |
27 |
| - << ", "; |
28 |
| - |
29 |
| - os << "\"text\"" |
30 |
| - << ": "; |
31 |
| - os << "\"" << text << "\"" |
32 |
| - << ", "; |
33 |
| - |
34 |
| - os << "\"" |
35 |
| - << "timestamps" |
36 |
| - << "\"" |
37 |
| - << ": "; |
38 |
| - os << "["; |
39 |
| - |
| 21 | +/// Helper for `OnlineRecognizerResult::AsJsonString()` |
| 22 | +template<typename T> |
| 23 | +std::string VecToString(const std::vector<T>& vec, int32_t precision = 6) { |
| 24 | + std::ostringstream oss; |
| 25 | + oss << std::fixed << std::setprecision(precision); |
| 26 | + oss << "[ "; |
40 | 27 | std::string sep = "";
|
41 |
| - for (auto t : timestamps) { |
42 |
| - os << sep << std::fixed << std::setprecision(2) << t; |
| 28 | + for (const auto& item : vec) { |
| 29 | + oss << sep << item; |
43 | 30 | sep = ", ";
|
44 | 31 | }
|
45 |
| - os << "], "; |
46 |
| - |
47 |
| - os << "\"" |
48 |
| - << "tokens" |
49 |
| - << "\"" |
50 |
| - << ":"; |
51 |
| - os << "["; |
52 |
| - |
53 |
| - sep = ""; |
54 |
| - auto oldFlags = os.flags(); |
55 |
| - for (const auto &t : tokens) { |
56 |
| - if (t.size() == 1 && static_cast<uint8_t>(t[0]) > 0x7f) { |
57 |
| - const uint8_t *p = reinterpret_cast<const uint8_t *>(t.c_str()); |
58 |
| - os << sep << "\"" |
59 |
| - << "<0x" << std::hex << std::uppercase << static_cast<uint32_t>(p[0]) |
60 |
| - << ">" |
61 |
| - << "\""; |
62 |
| - os.flags(oldFlags); |
63 |
| - } else { |
64 |
| - os << sep << "\"" << t << "\""; |
65 |
| - } |
| 32 | + oss << " ]"; |
| 33 | + return oss.str(); |
| 34 | +} |
| 35 | + |
| 36 | +/// Helper for `OnlineRecognizerResult::AsJsonString()` |
| 37 | +template<> // explicit specialization for T = std::string |
| 38 | +std::string VecToString<std::string>(const std::vector<std::string>& vec, |
| 39 | + int32_t) { // ignore 2nd arg |
| 40 | + std::ostringstream oss; |
| 41 | + oss << "[ "; |
| 42 | + std::string sep = ""; |
| 43 | + for (const auto& item : vec) { |
| 44 | + oss << sep << "\"" << item << "\""; |
66 | 45 | sep = ", ";
|
67 | 46 | }
|
68 |
| - os << "]"; |
69 |
| - os << "}"; |
| 47 | + oss << " ]"; |
| 48 | + return oss.str(); |
| 49 | +} |
70 | 50 |
|
| 51 | +std::string OnlineRecognizerResult::AsJsonString() const { |
| 52 | + std::ostringstream os; |
| 53 | + os << "{ "; |
| 54 | + os << "\"text\": " << "\"" << text << "\"" << ", "; |
| 55 | + os << "\"tokens\": " << VecToString(tokens) << ", "; |
| 56 | + os << "\"timestamps\": " << VecToString(timestamps, 2) << ", "; |
| 57 | + os << "\"ys_probs\": " << VecToString(ys_probs, 6) << ", "; |
| 58 | + os << "\"lm_probs\": " << VecToString(lm_probs, 6) << ", "; |
| 59 | + os << "\"context_scores\": " << VecToString(context_scores, 6) << ", "; |
| 60 | + os << "\"segment\": " << segment << ", "; |
| 61 | + os << "\"start_time\": " << std::fixed << std::setprecision(2) |
| 62 | + << start_time << ", "; |
| 63 | + os << "\"is_final\": " << (is_final ? "true" : "false"); |
| 64 | + os << "}"; |
71 | 65 | return os.str();
|
72 | 66 | }
|
73 | 67 |
|
|
0 commit comments