diff --git a/sherpa-onnx/csrc/offline-recognizer-ctc-impl.h b/sherpa-onnx/csrc/offline-recognizer-ctc-impl.h index 2eb908b53..2f4961350 100644 --- a/sherpa-onnx/csrc/offline-recognizer-ctc-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-ctc-impl.h @@ -5,7 +5,9 @@ #ifndef SHERPA_ONNX_CSRC_OFFLINE_RECOGNIZER_CTC_IMPL_H_ #define SHERPA_ONNX_CSRC_OFFLINE_RECOGNIZER_CTC_IMPL_H_ +#include #include +#include #include #include #include @@ -42,6 +44,15 @@ static OfflineRecognitionResult Convert(const OfflineCtcDecoderResult &src, } auto sym = sym_table[src.tokens[i]]; text.append(sym); + + if (sym.size() == 1 && sym[0] != ' ') { + // for byte bpe models + std::ostringstream os; + os << "<0x" << std::hex << std::uppercase + << (static_cast(sym[0]) & 0xff) << ">"; + sym = os.str(); + } + r.tokens.push_back(std::move(sym)); } r.text = std::move(text); diff --git a/sherpa-onnx/csrc/offline-recognizer-transducer-impl.h b/sherpa-onnx/csrc/offline-recognizer-transducer-impl.h index 3f4e2b05e..bf30eb30f 100644 --- a/sherpa-onnx/csrc/offline-recognizer-transducer-impl.h +++ b/sherpa-onnx/csrc/offline-recognizer-transducer-impl.h @@ -6,8 +6,10 @@ #define SHERPA_ONNX_CSRC_OFFLINE_RECOGNIZER_TRANSDUCER_IMPL_H_ #include +#include #include #include // NOLINT +#include #include #include #include @@ -44,6 +46,14 @@ static OfflineRecognitionResult Convert( auto sym = sym_table[i]; text.append(sym); + if (sym.size() == 1 && sym[0] != ' ') { + // for byte bpe models + std::ostringstream os; + os << "<0x" << std::hex << std::uppercase + << (static_cast(sym[0]) & 0xff) << ">"; + sym = os.str(); + } + r.tokens.push_back(std::move(sym)); } r.text = std::move(text); diff --git a/sherpa-onnx/csrc/online-recognizer-ctc-impl.h b/sherpa-onnx/csrc/online-recognizer-ctc-impl.h index f59dbd84b..fa81d6590 100644 --- a/sherpa-onnx/csrc/online-recognizer-ctc-impl.h +++ b/sherpa-onnx/csrc/online-recognizer-ctc-impl.h @@ -6,7 +6,9 @@ #define SHERPA_ONNX_CSRC_ONLINE_RECOGNIZER_CTC_IMPL_H_ #include +#include #include +#include #include #include #include @@ -35,6 +37,15 @@ static OnlineRecognizerResult Convert(const OnlineCtcDecoderResult &src, auto sym = sym_table[i]; r.text.append(sym); + + if (sym.size() == 1 && sym[0] != ' ') { + // for byte bpe models + std::ostringstream os; + os << "<0x" << std::hex << std::uppercase + << (static_cast(sym[0]) & 0xff) << ">"; + sym = os.str(); + } + r.tokens.push_back(std::move(sym)); } diff --git a/sherpa-onnx/csrc/online-recognizer-transducer-impl.h b/sherpa-onnx/csrc/online-recognizer-transducer-impl.h index a9ba0a95e..3ac757ea7 100644 --- a/sherpa-onnx/csrc/online-recognizer-transducer-impl.h +++ b/sherpa-onnx/csrc/online-recognizer-transducer-impl.h @@ -6,8 +6,10 @@ #define SHERPA_ONNX_CSRC_ONLINE_RECOGNIZER_TRANSDUCER_IMPL_H_ #include +#include #include #include // NOLINT +#include #include #include #include @@ -47,6 +49,15 @@ static OnlineRecognizerResult Convert(const OnlineTransducerDecoderResult &src, auto sym = sym_table[i]; r.text.append(sym); + + if (sym.size() == 1 && sym[0] != ' ') { + // for byte bpe models + std::ostringstream os; + os << "<0x" << std::hex << std::uppercase + << (static_cast(sym[0]) & 0xff) << ">"; + sym = os.str(); + } + r.tokens.push_back(std::move(sym)); } diff --git a/sherpa-onnx/python/csrc/offline-stream.cc b/sherpa-onnx/python/csrc/offline-stream.cc index e9eb9c57a..bd1d0650f 100644 --- a/sherpa-onnx/python/csrc/offline-stream.cc +++ b/sherpa-onnx/python/csrc/offline-stream.cc @@ -23,8 +23,12 @@ Process audio samples. static void PybindOfflineRecognitionResult(py::module *m) { // NOLINT using PyClass = OfflineRecognitionResult; py::class_(*m, "OfflineRecognitionResult") - .def_property_readonly("text", - [](const PyClass &self) { return self.text; }) + .def_property_readonly( + "text", + [](const PyClass &self) -> py::str { + return py::str(PyUnicode_DecodeUTF8(self.text.c_str(), + self.text.size(), "ignore")); + }) .def_property_readonly("tokens", [](const PyClass &self) { return self.tokens; }) .def_property_readonly( diff --git a/sherpa-onnx/python/csrc/online-recognizer.cc b/sherpa-onnx/python/csrc/online-recognizer.cc index 5a2dba095..d77be3867 100644 --- a/sherpa-onnx/python/csrc/online-recognizer.cc +++ b/sherpa-onnx/python/csrc/online-recognizer.cc @@ -15,7 +15,11 @@ static void PybindOnlineRecognizerResult(py::module *m) { using PyClass = OnlineRecognizerResult; py::class_(*m, "OnlineRecognizerResult") .def_property_readonly( - "text", [](PyClass &self) -> std::string { return self.text; }) + "text", + [](PyClass &self) -> py::str { + return py::str(PyUnicode_DecodeUTF8(self.text.c_str(), + self.text.size(), "ignore")); + }) .def_property_readonly( "tokens", [](PyClass &self) -> std::vector { return self.tokens; })