Add C API for punctuation (#768)

This commit is contained in:
Fangjun Kuang
2024-04-14 19:02:34 +08:00
committed by GitHub
parent b0265b258d
commit 13730ecbd8
13 changed files with 302 additions and 12 deletions

View File

@@ -15,6 +15,7 @@
#include "sherpa-onnx/csrc/display.h"
#include "sherpa-onnx/csrc/keyword-spotter.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/offline-punctuation.h"
#include "sherpa-onnx/csrc/offline-recognizer.h"
#include "sherpa-onnx/csrc/online-recognizer.h"
#include "sherpa-onnx/csrc/speaker-embedding-extractor.h"
@@ -1299,3 +1300,48 @@ void SherpaOnnxAudioTaggingFreeResults(
delete[] events;
}
struct SherpaOnnxOfflinePunctuation {
std::unique_ptr<sherpa_onnx::OfflinePunctuation> impl;
};
const SherpaOnnxOfflinePunctuation *SherpaOnnxCreateOfflinePunctuation(
const SherpaOnnxOfflinePunctuationConfig *config) {
sherpa_onnx::OfflinePunctuationConfig c;
c.model.ct_transformer = SHERPA_ONNX_OR(config->model.ct_transformer, "");
c.model.num_threads = SHERPA_ONNX_OR(config->model.num_threads, 1);
c.model.debug = config->model.debug;
c.model.provider = SHERPA_ONNX_OR(config->model.provider, "cpu");
if (c.model.debug) {
SHERPA_ONNX_LOGE("%s\n", c.ToString().c_str());
}
if (!c.Validate()) {
SHERPA_ONNX_LOGE("Errors in config");
return nullptr;
}
SherpaOnnxOfflinePunctuation *punct = new SherpaOnnxOfflinePunctuation;
punct->impl = std::make_unique<sherpa_onnx::OfflinePunctuation>(c);
return punct;
}
void SherpaOnnxDestroyOfflinePunctuation(
const SherpaOnnxOfflinePunctuation *punct) {
delete punct;
}
const char *SherpaOfflinePunctuationAddPunct(
const SherpaOnnxOfflinePunctuation *punct, const char *text) {
std::string text_with_punct = punct->impl->AddPunctuation(text);
char *ans = new char[text_with_punct.size() + 1];
std::copy(text_with_punct.begin(), text_with_punct.end(), ans);
ans[text_with_punct.size()] = 0;
return ans;
}
void SherpaOfflinePunctuationFreeText(const char *text) { delete[] text; }

View File

@@ -1149,6 +1149,41 @@ SherpaOnnxAudioTaggingCompute(const SherpaOnnxAudioTagging *tagger,
SHERPA_ONNX_API void SherpaOnnxAudioTaggingFreeResults(
const SherpaOnnxAudioEvent *const *p);
// ============================================================
// For punctuation
// ============================================================
SHERPA_ONNX_API typedef struct SherpaOnnxOfflinePunctuationModelConfig {
const char *ct_transformer;
int32_t num_threads;
int32_t debug; // true to print debug information of the model
const char *provider;
} SherpaOnnxOfflinePunctuationModelConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflinePunctuationConfig {
SherpaOnnxOfflinePunctuationModelConfig model;
} SherpaOnnxOfflinePunctuationConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflinePunctuation
SherpaOnnxOfflinePunctuation;
// The user has to invoke SherpaOnnxDestroyOfflinePunctuation()
// to free the returned pointer to avoid memory leak
SHERPA_ONNX_API const SherpaOnnxOfflinePunctuation *
SherpaOnnxCreateOfflinePunctuation(
const SherpaOnnxOfflinePunctuationConfig *config);
SHERPA_ONNX_API void SherpaOnnxDestroyOfflinePunctuation(
const SherpaOnnxOfflinePunctuation *punct);
// Add punctuations to the input text.
// The user has to invoke SherpaOfflinePunctuationFreeText()
// to free the returned pointer to avoid memory leak
SHERPA_ONNX_API const char *SherpaOfflinePunctuationAddPunct(
const SherpaOnnxOfflinePunctuation *punct, const char *text);
SHERPA_ONNX_API void SherpaOfflinePunctuationFreeText(const char *text);
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

View File

@@ -134,25 +134,40 @@ class OfflinePunctuationCtTransformerImpl : public OfflinePunctuationImpl {
}
} // for (int32_t i = 0; i != num_segments; ++i)
std::string ans;
if (punctuations.empty()) {
return text + meta_data.id2punct[meta_data.dot_id];
}
std::vector<std::string> words_punct;
for (int32_t i = 0; i != static_cast<int32_t>(punctuations.size()); ++i) {
if (i > tokens.size()) {
if (i >= tokens.size()) {
break;
}
const std::string &w = tokens[i];
if (i > 0 && !(ans.back() & 0x80) && !(w[0] & 0x80)) {
ans.push_back(' ');
std::string &w = tokens[i];
if (i > 0 && !(words_punct.back()[0] & 0x80) && !(w[0] & 0x80)) {
words_punct.push_back(" ");
}
ans.append(w);
words_punct.push_back(std::move(w));
if (punctuations[i] != meta_data.underline_id) {
ans.append(meta_data.id2punct[punctuations[i]]);
words_punct.push_back(meta_data.id2punct[punctuations[i]]);
}
}
if (ans.back() != meta_data.dot_id && ans.back() != meta_data.quest_id) {
ans.push_back(meta_data.dot_id);
}
if (words_punct.back() == meta_data.id2punct[meta_data.comma_id] ||
words_punct.back() == meta_data.id2punct[meta_data.pause_id]) {
words_punct.back() = meta_data.id2punct[meta_data.dot_id];
}
if (words_punct.back() != meta_data.id2punct[meta_data.dot_id] &&
words_punct.back() != meta_data.id2punct[meta_data.quest_id]) {
words_punct.push_back(meta_data.id2punct[meta_data.dot_id]);
}
std::string ans;
for (const auto &w : words_punct) {
ans.append(w);
}
return ans;
}

View File

@@ -4,6 +4,8 @@
#include "sherpa-onnx/python/csrc/offline-punctuation.h"
#include <string>
#include "sherpa-onnx/csrc/offline-punctuation.h"
namespace sherpa_onnx {