正在显示
13 个修改的文件
包含
301 行增加
和
11 行删除
| @@ -11,9 +11,22 @@ log() { | @@ -11,9 +11,22 @@ log() { | ||
| 11 | echo "SLID_EXE is $SLID_EXE" | 11 | echo "SLID_EXE is $SLID_EXE" |
| 12 | echo "SID_EXE is $SID_EXE" | 12 | echo "SID_EXE is $SID_EXE" |
| 13 | echo "AT_EXE is $AT_EXE" | 13 | echo "AT_EXE is $AT_EXE" |
| 14 | +echo "PUNCT_EXE is $PUNCT_EXE" | ||
| 14 | echo "PATH: $PATH" | 15 | echo "PATH: $PATH" |
| 15 | 16 | ||
| 16 | log "------------------------------------------------------------" | 17 | log "------------------------------------------------------------" |
| 18 | +log "Test adding punctuations " | ||
| 19 | +log "------------------------------------------------------------" | ||
| 20 | + | ||
| 21 | +curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 | ||
| 22 | +ls -lh | ||
| 23 | +tar xf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 | ||
| 24 | +ls -lh sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12 | ||
| 25 | +rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 | ||
| 26 | +$PUNCT_EXE | ||
| 27 | +rm -rf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12 | ||
| 28 | + | ||
| 29 | +log "------------------------------------------------------------" | ||
| 17 | log "Test audio tagging " | 30 | log "Test audio tagging " |
| 18 | log "------------------------------------------------------------" | 31 | log "------------------------------------------------------------" |
| 19 | 32 |
| @@ -126,7 +126,7 @@ jobs: | @@ -126,7 +126,7 @@ jobs: | ||
| 126 | - uses: actions/upload-artifact@v4 | 126 | - uses: actions/upload-artifact@v4 |
| 127 | with: | 127 | with: |
| 128 | name: release-${{ matrix.build_type }}-with-shared-lib-${{ matrix.shared_lib }}-with-tts-${{ matrix.with_tts }} | 128 | name: release-${{ matrix.build_type }}-with-shared-lib-${{ matrix.shared_lib }}-with-tts-${{ matrix.with_tts }} |
| 129 | - path: build/bin/* | 129 | + path: install/* |
| 130 | 130 | ||
| 131 | - name: Test offline punctuation | 131 | - name: Test offline punctuation |
| 132 | shell: bash | 132 | shell: bash |
| @@ -143,6 +143,7 @@ jobs: | @@ -143,6 +143,7 @@ jobs: | ||
| 143 | export SLID_EXE=spoken-language-identification-c-api | 143 | export SLID_EXE=spoken-language-identification-c-api |
| 144 | export SID_EXE=speaker-identification-c-api | 144 | export SID_EXE=speaker-identification-c-api |
| 145 | export AT_EXE=audio-tagging-c-api | 145 | export AT_EXE=audio-tagging-c-api |
| 146 | + export PUNCT_EXE=add-punctuation-c-api | ||
| 146 | 147 | ||
| 147 | .github/scripts/test-c-api.sh | 148 | .github/scripts/test-c-api.sh |
| 148 | 149 |
| @@ -122,6 +122,7 @@ jobs: | @@ -122,6 +122,7 @@ jobs: | ||
| 122 | export SLID_EXE=spoken-language-identification-c-api | 122 | export SLID_EXE=spoken-language-identification-c-api |
| 123 | export SID_EXE=speaker-identification-c-api | 123 | export SID_EXE=speaker-identification-c-api |
| 124 | export AT_EXE=audio-tagging-c-api | 124 | export AT_EXE=audio-tagging-c-api |
| 125 | + export PUNCT_EXE=add-punctuation-c-api | ||
| 125 | 126 | ||
| 126 | .github/scripts/test-c-api.sh | 127 | .github/scripts/test-c-api.sh |
| 127 | 128 |
| @@ -89,6 +89,7 @@ jobs: | @@ -89,6 +89,7 @@ jobs: | ||
| 89 | export SLID_EXE=spoken-language-identification-c-api.exe | 89 | export SLID_EXE=spoken-language-identification-c-api.exe |
| 90 | export SID_EXE=speaker-identification-c-api.exe | 90 | export SID_EXE=speaker-identification-c-api.exe |
| 91 | export AT_EXE=audio-tagging-c-api.exe | 91 | export AT_EXE=audio-tagging-c-api.exe |
| 92 | + export PUNCT_EXE=add-punctuation-c-api.exe | ||
| 92 | 93 | ||
| 93 | .github/scripts/test-c-api.sh | 94 | .github/scripts/test-c-api.sh |
| 94 | 95 |
| @@ -89,6 +89,7 @@ jobs: | @@ -89,6 +89,7 @@ jobs: | ||
| 89 | export SLID_EXE=spoken-language-identification-c-api.exe | 89 | export SLID_EXE=spoken-language-identification-c-api.exe |
| 90 | export SID_EXE=speaker-identification-c-api.exe | 90 | export SID_EXE=speaker-identification-c-api.exe |
| 91 | export AT_EXE=audio-tagging-c-api.exe | 91 | export AT_EXE=audio-tagging-c-api.exe |
| 92 | + export PUNCT_EXE=add-punctuation-c-api.exe | ||
| 92 | 93 | ||
| 93 | .github/scripts/test-c-api.sh | 94 | .github/scripts/test-c-api.sh |
| 94 | 95 |
| @@ -21,6 +21,9 @@ target_link_libraries(streaming-hlg-decode-file-c-api sherpa-onnx-c-api) | @@ -21,6 +21,9 @@ target_link_libraries(streaming-hlg-decode-file-c-api sherpa-onnx-c-api) | ||
| 21 | add_executable(audio-tagging-c-api audio-tagging-c-api.c) | 21 | add_executable(audio-tagging-c-api audio-tagging-c-api.c) |
| 22 | target_link_libraries(audio-tagging-c-api sherpa-onnx-c-api) | 22 | target_link_libraries(audio-tagging-c-api sherpa-onnx-c-api) |
| 23 | 23 | ||
| 24 | +add_executable(add-punctuation-c-api add-punctuation-c-api.c) | ||
| 25 | +target_link_libraries(add-punctuation-c-api sherpa-onnx-c-api) | ||
| 26 | + | ||
| 24 | if(SHERPA_ONNX_HAS_ALSA) | 27 | if(SHERPA_ONNX_HAS_ALSA) |
| 25 | add_subdirectory(./asr-microphone-example) | 28 | add_subdirectory(./asr-microphone-example) |
| 26 | elseif((UNIX AND NOT APPLE) OR LINUX) | 29 | elseif((UNIX AND NOT APPLE) OR LINUX) |
c-api-examples/add-punctuation-c-api.c
0 → 100644
| 1 | +// c-api-examples/add-punctuation-c-api.c | ||
| 2 | +// | ||
| 3 | +// Copyright (c) 2024 Xiaomi Corporation | ||
| 4 | + | ||
| 5 | +// We assume you have pre-downloaded the model files for testing | ||
| 6 | +// from https://github.com/k2-fsa/sherpa-onnx/releases/tag/punctuation-models | ||
| 7 | +// | ||
| 8 | +// An example is given below: | ||
| 9 | +// | ||
| 10 | +// clang-format off | ||
| 11 | +// | ||
| 12 | +// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 | ||
| 13 | +// tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 | ||
| 14 | +// rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 | ||
| 15 | +// | ||
| 16 | +// clang-format on | ||
| 17 | + | ||
| 18 | +#include <stdio.h> | ||
| 19 | +#include <stdlib.h> | ||
| 20 | +#include <string.h> | ||
| 21 | + | ||
| 22 | +#include "sherpa-onnx/c-api/c-api.h" | ||
| 23 | + | ||
| 24 | +int32_t main() { | ||
| 25 | + SherpaOnnxOfflinePunctuationConfig config; | ||
| 26 | + memset(&config, 0, sizeof(config)); | ||
| 27 | + | ||
| 28 | + // clang-format off | ||
| 29 | + config.model.ct_transformer = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx"; | ||
| 30 | + // clang-format on | ||
| 31 | + config.model.num_threads = 1; | ||
| 32 | + config.model.debug = 1; | ||
| 33 | + config.model.provider = "cpu"; | ||
| 34 | + | ||
| 35 | + const SherpaOnnxOfflinePunctuation *punct = | ||
| 36 | + SherpaOnnxCreateOfflinePunctuation(&config); | ||
| 37 | + if (!punct) { | ||
| 38 | + fprintf(stderr, | ||
| 39 | + "Failed to create OfflinePunctuation. Please check your config"); | ||
| 40 | + return -1; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + const char *texts[] = { | ||
| 44 | + "这是一个测试你好吗How are you我很好thank you are you ok谢谢你", | ||
| 45 | + "我们都是木头人不会说话不会动", | ||
| 46 | + "The African blogosphere is rapidly expanding bringing more voices " | ||
| 47 | + "online in the form of commentaries opinions analyses rants and poetry", | ||
| 48 | + }; | ||
| 49 | + | ||
| 50 | + int32_t n = sizeof(texts) / sizeof(const char *); | ||
| 51 | + fprintf(stderr, "n: %d\n", n); | ||
| 52 | + | ||
| 53 | + fprintf(stderr, "--------------------\n"); | ||
| 54 | + for (int32_t i = 0; i != n; ++i) { | ||
| 55 | + const char *text_with_punct = | ||
| 56 | + SherpaOfflinePunctuationAddPunct(punct, texts[i]); | ||
| 57 | + | ||
| 58 | + fprintf(stderr, "Input text: %s\n", texts[i]); | ||
| 59 | + fprintf(stderr, "Output text: %s\n", text_with_punct); | ||
| 60 | + SherpaOfflinePunctuationFreeText(text_with_punct); | ||
| 61 | + fprintf(stderr, "--------------------\n"); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + SherpaOnnxDestroyOfflinePunctuation(punct); | ||
| 65 | + | ||
| 66 | + return 0; | ||
| 67 | +}; |
| 1 | +#!/usr/bin/env python3 | ||
| 2 | + | ||
| 3 | +# Real-time speech recognition from a microphone with sherpa-onnx Python API | ||
| 4 | +# with endpoint detection. | ||
| 5 | +# This script uses a streaming paraformer | ||
| 6 | +# | ||
| 7 | +# Please refer to | ||
| 8 | +# https://k2-fsa.github.io/sherpa/onnx/pretrained_models/online-paraformer/paraformer-models.html# | ||
| 9 | +# to download pre-trained models | ||
| 10 | + | ||
| 11 | +import sys | ||
| 12 | +from pathlib import Path | ||
| 13 | + | ||
| 14 | +try: | ||
| 15 | + import sounddevice as sd | ||
| 16 | +except ImportError: | ||
| 17 | + print("Please install sounddevice first. You can use") | ||
| 18 | + print() | ||
| 19 | + print(" pip install sounddevice") | ||
| 20 | + print() | ||
| 21 | + print("to install it") | ||
| 22 | + sys.exit(-1) | ||
| 23 | + | ||
| 24 | +import sherpa_onnx | ||
| 25 | + | ||
| 26 | + | ||
| 27 | +def assert_file_exists(filename: str): | ||
| 28 | + assert Path(filename).is_file(), ( | ||
| 29 | + f"{filename} does not exist!\n" | ||
| 30 | + "Please refer to " | ||
| 31 | + "https://k2-fsa.github.io/sherpa/onnx/pretrained_models/online-paraformer/paraformer-models.html to download it" | ||
| 32 | + ) | ||
| 33 | + | ||
| 34 | + | ||
| 35 | +def create_recognizer(): | ||
| 36 | + encoder = "./sherpa-onnx-streaming-paraformer-bilingual-zh-en/encoder.int8.onnx" | ||
| 37 | + decoder = "./sherpa-onnx-streaming-paraformer-bilingual-zh-en/decoder.int8.onnx" | ||
| 38 | + tokens = "./sherpa-onnx-streaming-paraformer-bilingual-zh-en/tokens.txt" | ||
| 39 | + assert_file_exists(encoder) | ||
| 40 | + assert_file_exists(decoder) | ||
| 41 | + assert_file_exists(tokens) | ||
| 42 | + recognizer = sherpa_onnx.OnlineRecognizer.from_paraformer( | ||
| 43 | + tokens=tokens, | ||
| 44 | + encoder=encoder, | ||
| 45 | + decoder=decoder, | ||
| 46 | + num_threads=1, | ||
| 47 | + sample_rate=16000, | ||
| 48 | + feature_dim=80, | ||
| 49 | + enable_endpoint_detection=True, | ||
| 50 | + rule1_min_trailing_silence=2.4, | ||
| 51 | + rule2_min_trailing_silence=1.2, | ||
| 52 | + rule3_min_utterance_length=300, # it essentially disables this rule | ||
| 53 | + ) | ||
| 54 | + return recognizer | ||
| 55 | + | ||
| 56 | + | ||
| 57 | +def main(): | ||
| 58 | + devices = sd.query_devices() | ||
| 59 | + if len(devices) == 0: | ||
| 60 | + print("No microphone devices found") | ||
| 61 | + sys.exit(0) | ||
| 62 | + | ||
| 63 | + print(devices) | ||
| 64 | + default_input_device_idx = sd.default.device[0] | ||
| 65 | + print(f'Use default device: {devices[default_input_device_idx]["name"]}') | ||
| 66 | + | ||
| 67 | + recognizer = create_recognizer() | ||
| 68 | + print("Started! Please speak") | ||
| 69 | + | ||
| 70 | + # The model is using 16 kHz, we use 48 kHz here to demonstrate that | ||
| 71 | + # sherpa-onnx will do resampling inside. | ||
| 72 | + sample_rate = 48000 | ||
| 73 | + samples_per_read = int(0.1 * sample_rate) # 0.1 second = 100 ms | ||
| 74 | + | ||
| 75 | + stream = recognizer.create_stream() | ||
| 76 | + | ||
| 77 | + last_result = "" | ||
| 78 | + segment_id = 0 | ||
| 79 | + with sd.InputStream(channels=1, dtype="float32", samplerate=sample_rate) as s: | ||
| 80 | + while True: | ||
| 81 | + samples, _ = s.read(samples_per_read) # a blocking read | ||
| 82 | + samples = samples.reshape(-1) | ||
| 83 | + stream.accept_waveform(sample_rate, samples) | ||
| 84 | + while recognizer.is_ready(stream): | ||
| 85 | + recognizer.decode_stream(stream) | ||
| 86 | + | ||
| 87 | + is_endpoint = recognizer.is_endpoint(stream) | ||
| 88 | + | ||
| 89 | + result = recognizer.get_result(stream) | ||
| 90 | + | ||
| 91 | + if result and (last_result != result): | ||
| 92 | + last_result = result | ||
| 93 | + print("\r{}:{}".format(segment_id, result), end="", flush=True) | ||
| 94 | + if is_endpoint: | ||
| 95 | + if result: | ||
| 96 | + print("\r{}:{}".format(segment_id, result), flush=True) | ||
| 97 | + segment_id += 1 | ||
| 98 | + recognizer.reset(stream) | ||
| 99 | + | ||
| 100 | + | ||
| 101 | +if __name__ == "__main__": | ||
| 102 | + try: | ||
| 103 | + main() | ||
| 104 | + except KeyboardInterrupt: | ||
| 105 | + print("\nCaught Ctrl + C. Exiting") |
| @@ -15,6 +15,7 @@ | @@ -15,6 +15,7 @@ | ||
| 15 | #include "sherpa-onnx/csrc/display.h" | 15 | #include "sherpa-onnx/csrc/display.h" |
| 16 | #include "sherpa-onnx/csrc/keyword-spotter.h" | 16 | #include "sherpa-onnx/csrc/keyword-spotter.h" |
| 17 | #include "sherpa-onnx/csrc/macros.h" | 17 | #include "sherpa-onnx/csrc/macros.h" |
| 18 | +#include "sherpa-onnx/csrc/offline-punctuation.h" | ||
| 18 | #include "sherpa-onnx/csrc/offline-recognizer.h" | 19 | #include "sherpa-onnx/csrc/offline-recognizer.h" |
| 19 | #include "sherpa-onnx/csrc/online-recognizer.h" | 20 | #include "sherpa-onnx/csrc/online-recognizer.h" |
| 20 | #include "sherpa-onnx/csrc/speaker-embedding-extractor.h" | 21 | #include "sherpa-onnx/csrc/speaker-embedding-extractor.h" |
| @@ -1299,3 +1300,48 @@ void SherpaOnnxAudioTaggingFreeResults( | @@ -1299,3 +1300,48 @@ void SherpaOnnxAudioTaggingFreeResults( | ||
| 1299 | 1300 | ||
| 1300 | delete[] events; | 1301 | delete[] events; |
| 1301 | } | 1302 | } |
| 1303 | + | ||
| 1304 | +struct SherpaOnnxOfflinePunctuation { | ||
| 1305 | + std::unique_ptr<sherpa_onnx::OfflinePunctuation> impl; | ||
| 1306 | +}; | ||
| 1307 | + | ||
| 1308 | +const SherpaOnnxOfflinePunctuation *SherpaOnnxCreateOfflinePunctuation( | ||
| 1309 | + const SherpaOnnxOfflinePunctuationConfig *config) { | ||
| 1310 | + sherpa_onnx::OfflinePunctuationConfig c; | ||
| 1311 | + c.model.ct_transformer = SHERPA_ONNX_OR(config->model.ct_transformer, ""); | ||
| 1312 | + c.model.num_threads = SHERPA_ONNX_OR(config->model.num_threads, 1); | ||
| 1313 | + c.model.debug = config->model.debug; | ||
| 1314 | + c.model.provider = SHERPA_ONNX_OR(config->model.provider, "cpu"); | ||
| 1315 | + | ||
| 1316 | + if (c.model.debug) { | ||
| 1317 | + SHERPA_ONNX_LOGE("%s\n", c.ToString().c_str()); | ||
| 1318 | + } | ||
| 1319 | + | ||
| 1320 | + if (!c.Validate()) { | ||
| 1321 | + SHERPA_ONNX_LOGE("Errors in config"); | ||
| 1322 | + return nullptr; | ||
| 1323 | + } | ||
| 1324 | + | ||
| 1325 | + SherpaOnnxOfflinePunctuation *punct = new SherpaOnnxOfflinePunctuation; | ||
| 1326 | + punct->impl = std::make_unique<sherpa_onnx::OfflinePunctuation>(c); | ||
| 1327 | + | ||
| 1328 | + return punct; | ||
| 1329 | +} | ||
| 1330 | + | ||
| 1331 | +void SherpaOnnxDestroyOfflinePunctuation( | ||
| 1332 | + const SherpaOnnxOfflinePunctuation *punct) { | ||
| 1333 | + delete punct; | ||
| 1334 | +} | ||
| 1335 | + | ||
| 1336 | +const char *SherpaOfflinePunctuationAddPunct( | ||
| 1337 | + const SherpaOnnxOfflinePunctuation *punct, const char *text) { | ||
| 1338 | + std::string text_with_punct = punct->impl->AddPunctuation(text); | ||
| 1339 | + | ||
| 1340 | + char *ans = new char[text_with_punct.size() + 1]; | ||
| 1341 | + std::copy(text_with_punct.begin(), text_with_punct.end(), ans); | ||
| 1342 | + ans[text_with_punct.size()] = 0; | ||
| 1343 | + | ||
| 1344 | + return ans; | ||
| 1345 | +} | ||
| 1346 | + | ||
| 1347 | +void SherpaOfflinePunctuationFreeText(const char *text) { delete[] text; } |
| @@ -1149,6 +1149,41 @@ SherpaOnnxAudioTaggingCompute(const SherpaOnnxAudioTagging *tagger, | @@ -1149,6 +1149,41 @@ SherpaOnnxAudioTaggingCompute(const SherpaOnnxAudioTagging *tagger, | ||
| 1149 | SHERPA_ONNX_API void SherpaOnnxAudioTaggingFreeResults( | 1149 | SHERPA_ONNX_API void SherpaOnnxAudioTaggingFreeResults( |
| 1150 | const SherpaOnnxAudioEvent *const *p); | 1150 | const SherpaOnnxAudioEvent *const *p); |
| 1151 | 1151 | ||
| 1152 | +// ============================================================ | ||
| 1153 | +// For punctuation | ||
| 1154 | +// ============================================================ | ||
| 1155 | + | ||
| 1156 | +SHERPA_ONNX_API typedef struct SherpaOnnxOfflinePunctuationModelConfig { | ||
| 1157 | + const char *ct_transformer; | ||
| 1158 | + int32_t num_threads; | ||
| 1159 | + int32_t debug; // true to print debug information of the model | ||
| 1160 | + const char *provider; | ||
| 1161 | +} SherpaOnnxOfflinePunctuationModelConfig; | ||
| 1162 | + | ||
| 1163 | +SHERPA_ONNX_API typedef struct SherpaOnnxOfflinePunctuationConfig { | ||
| 1164 | + SherpaOnnxOfflinePunctuationModelConfig model; | ||
| 1165 | +} SherpaOnnxOfflinePunctuationConfig; | ||
| 1166 | + | ||
| 1167 | +SHERPA_ONNX_API typedef struct SherpaOnnxOfflinePunctuation | ||
| 1168 | + SherpaOnnxOfflinePunctuation; | ||
| 1169 | + | ||
| 1170 | +// The user has to invoke SherpaOnnxDestroyOfflinePunctuation() | ||
| 1171 | +// to free the returned pointer to avoid memory leak | ||
| 1172 | +SHERPA_ONNX_API const SherpaOnnxOfflinePunctuation * | ||
| 1173 | +SherpaOnnxCreateOfflinePunctuation( | ||
| 1174 | + const SherpaOnnxOfflinePunctuationConfig *config); | ||
| 1175 | + | ||
| 1176 | +SHERPA_ONNX_API void SherpaOnnxDestroyOfflinePunctuation( | ||
| 1177 | + const SherpaOnnxOfflinePunctuation *punct); | ||
| 1178 | + | ||
| 1179 | +// Add punctuations to the input text. | ||
| 1180 | +// The user has to invoke SherpaOfflinePunctuationFreeText() | ||
| 1181 | +// to free the returned pointer to avoid memory leak | ||
| 1182 | +SHERPA_ONNX_API const char *SherpaOfflinePunctuationAddPunct( | ||
| 1183 | + const SherpaOnnxOfflinePunctuation *punct, const char *text); | ||
| 1184 | + | ||
| 1185 | +SHERPA_ONNX_API void SherpaOfflinePunctuationFreeText(const char *text); | ||
| 1186 | + | ||
| 1152 | #if defined(__GNUC__) | 1187 | #if defined(__GNUC__) |
| 1153 | #pragma GCC diagnostic pop | 1188 | #pragma GCC diagnostic pop |
| 1154 | #endif | 1189 | #endif |
| @@ -134,25 +134,40 @@ class OfflinePunctuationCtTransformerImpl : public OfflinePunctuationImpl { | @@ -134,25 +134,40 @@ class OfflinePunctuationCtTransformerImpl : public OfflinePunctuationImpl { | ||
| 134 | } | 134 | } |
| 135 | } // for (int32_t i = 0; i != num_segments; ++i) | 135 | } // for (int32_t i = 0; i != num_segments; ++i) |
| 136 | 136 | ||
| 137 | - std::string ans; | 137 | + if (punctuations.empty()) { |
| 138 | + return text + meta_data.id2punct[meta_data.dot_id]; | ||
| 139 | + } | ||
| 140 | + std::vector<std::string> words_punct; | ||
| 138 | 141 | ||
| 139 | for (int32_t i = 0; i != static_cast<int32_t>(punctuations.size()); ++i) { | 142 | for (int32_t i = 0; i != static_cast<int32_t>(punctuations.size()); ++i) { |
| 140 | - if (i > tokens.size()) { | 143 | + if (i >= tokens.size()) { |
| 141 | break; | 144 | break; |
| 142 | } | 145 | } |
| 143 | - const std::string &w = tokens[i]; | ||
| 144 | - if (i > 0 && !(ans.back() & 0x80) && !(w[0] & 0x80)) { | ||
| 145 | - ans.push_back(' '); | 146 | + std::string &w = tokens[i]; |
| 147 | + if (i > 0 && !(words_punct.back()[0] & 0x80) && !(w[0] & 0x80)) { | ||
| 148 | + words_punct.push_back(" "); | ||
| 146 | } | 149 | } |
| 147 | - ans.append(w); | 150 | + words_punct.push_back(std::move(w)); |
| 151 | + | ||
| 148 | if (punctuations[i] != meta_data.underline_id) { | 152 | if (punctuations[i] != meta_data.underline_id) { |
| 149 | - ans.append(meta_data.id2punct[punctuations[i]]); | 153 | + words_punct.push_back(meta_data.id2punct[punctuations[i]]); |
| 154 | + } | ||
| 150 | } | 155 | } |
| 156 | + | ||
| 157 | + if (words_punct.back() == meta_data.id2punct[meta_data.comma_id] || | ||
| 158 | + words_punct.back() == meta_data.id2punct[meta_data.pause_id]) { | ||
| 159 | + words_punct.back() = meta_data.id2punct[meta_data.dot_id]; | ||
| 151 | } | 160 | } |
| 152 | - if (ans.back() != meta_data.dot_id && ans.back() != meta_data.quest_id) { | ||
| 153 | - ans.push_back(meta_data.dot_id); | 161 | + |
| 162 | + if (words_punct.back() != meta_data.id2punct[meta_data.dot_id] && | ||
| 163 | + words_punct.back() != meta_data.id2punct[meta_data.quest_id]) { | ||
| 164 | + words_punct.push_back(meta_data.id2punct[meta_data.dot_id]); | ||
| 154 | } | 165 | } |
| 155 | 166 | ||
| 167 | + std::string ans; | ||
| 168 | + for (const auto &w : words_punct) { | ||
| 169 | + ans.append(w); | ||
| 170 | + } | ||
| 156 | return ans; | 171 | return ans; |
| 157 | } | 172 | } |
| 158 | 173 |
| @@ -4,6 +4,8 @@ | @@ -4,6 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/python/csrc/offline-punctuation.h" | 5 | #include "sherpa-onnx/python/csrc/offline-punctuation.h" |
| 6 | 6 | ||
| 7 | +#include <string> | ||
| 8 | + | ||
| 7 | #include "sherpa-onnx/csrc/offline-punctuation.h" | 9 | #include "sherpa-onnx/csrc/offline-punctuation.h" |
| 8 | 10 | ||
| 9 | namespace sherpa_onnx { | 11 | namespace sherpa_onnx { |
-
请 注册 或 登录 后发表评论