Skip to content

Commit ca66853

Browse files
authored
Remove leading spaces from ASR results (k2-fsa#3711)
1 parent 1b8d95d commit ca66853

4 files changed

Lines changed: 22 additions & 1 deletion

File tree

sherpa-onnx/csrc/offline-recognizer.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ std::unique_ptr<OfflineStream> OfflineRecognizer::CreateStream() const {
172172

173173
void OfflineRecognizer::DecodeStreams(OfflineStream **ss, int32_t n) const {
174174
impl_->DecodeStreams(ss, n);
175+
176+
for (int32_t i = 0; i < n; ++i) {
177+
auto r = ss[i]->GetResult();
178+
r.text = RemoveLeadingSpaces(r.text);
179+
ss[i]->SetResult(r);
180+
}
175181
}
176182

177183
void OfflineRecognizer::SetConfig(const OfflineRecognizerConfig &config) {

sherpa-onnx/csrc/online-recognizer.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ void OnlineRecognizer::DecodeStreams(OnlineStream **ss, int32_t n) const {
252252
}
253253

254254
OnlineRecognizerResult OnlineRecognizer::GetResult(OnlineStream *s) const {
255-
return impl_->GetResult(s);
255+
auto r = impl_->GetResult(s);
256+
r.text = RemoveLeadingSpaces(r.text);
257+
return r;
256258
}
257259

258260
bool OnlineRecognizer::IsEndpoint(OnlineStream *s) const {

sherpa-onnx/csrc/text-utils.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,17 @@ std::string RemoveSpaceBetweenCjk(const std::string &text) {
11161116
return Utf32ToUtf8(ans);
11171117
}
11181118

1119+
std::string RemoveLeadingSpaces(const std::string &text) {
1120+
size_t start = text.find_first_not_of(' ');
1121+
if (start == std::string::npos) {
1122+
return {};
1123+
}
1124+
if (start == 0) {
1125+
return text;
1126+
}
1127+
return text.substr(start);
1128+
}
1129+
11191130
std::string GetWord(const std::vector<std::string> &words, int32_t start,
11201131
int32_t end) {
11211132
std::string ans;

sherpa-onnx/csrc/text-utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ bool ContainsCJK(const std::u32string &text);
190190

191191
std::string RemoveSpaceBetweenCjk(const std::string &text);
192192

193+
std::string RemoveLeadingSpaces(const std::string &text);
194+
193195
bool StringToBool(const std::string &s);
194196

195197
// end is inclusive

0 commit comments

Comments
 (0)