Dart API for adding punctuations to text (#1182)

This commit is contained in:
Fangjun Kuang
2024-07-29 12:41:52 +08:00
committed by GitHub
parent cd1fedaa49
commit 646f99c870
30 changed files with 327 additions and 22 deletions

View File

@@ -16,6 +16,7 @@ https://pub.dev/packages/sherpa_onnx
| [./vad](./vad)| Example for voice activity detection|
| [./vad-with-non-streaming-asr](./vad-with-non-streaming-asr)| Example for voice activity detection with non-streaming speech recognition. You can use it to generate subtitles.|
| [./audio-tagging](./audio-tagging)| Example for audio tagging.|
| [./add-punctuations](./add-punctuations)| Example for adding punctuations to text.|
## How to create an example in this folder

View File

@@ -0,0 +1,3 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

View File

@@ -0,0 +1,8 @@
# Introduction
This example shows how to use the Dart API from sherpa-onnx to add punctuations to text.
| File | Description|
|------|------------|
|[./bin/punctuations.dart](./bin/punctuations.dart)| Use a [CT Transformer model](https://modelscope.cn/models/iic/punc_ct-transformer_zh-cn-common-vocab272727-pytorch/summary) to add punctuations to text. See [./run-ct-transformer.sh](./run-ct-transformer.sh)|

View File

@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.
include: package:lints/recommended.yaml
# Uncomment the following section to specify additional rules.
# linter:
# rules:
# - camel_case_types
# analyzer:
# exclude:
# - path/to/excluded/files/**
# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints
# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options

View File

@@ -0,0 +1 @@
../../vad/bin/init.dart

View File

@@ -0,0 +1,46 @@
// Copyright (c) 2024 Xiaomi Corporation
import 'dart:io';
import 'package:args/args.dart';
import 'package:sherpa_onnx/sherpa_onnx.dart' as sherpa_onnx;
import './init.dart';
void main(List<String> arguments) async {
await initSherpaOnnx();
final parser = ArgParser()..addOption('model', help: 'Path to model.onnx');
final res = parser.parse(arguments);
if (res['model'] == null) {
print(parser.usage);
exit(1);
}
final modelFile = res['model'] as String;
final modelConfig = sherpa_onnx.OfflinePunctuationModelConfig(
ctTransformer: modelFile,
numThreads: 1,
provider: 'cpu',
debug: false,
);
final config = sherpa_onnx.OfflinePunctuationConfig(model: modelConfig);
final punct = sherpa_onnx.OfflinePunctuation(config: config);
final texts = [
'这是一个测试你好吗How are you我很好thank you are you ok谢谢你',
'我们都是木头人不会说话不会动',
'The African blogosphere is rapidly expanding bringing more voices online in the form of commentaries opinions analyses rants and poetry',
];
for (final t in texts) {
final textWithPunct = punct.addPunct(t);
print('----------');
print('Before: $t');
print('After: $textWithPunct');
}
print('----------');
punct.free();
}

View File

@@ -0,0 +1,17 @@
name: add_punctuations
description: >
This example demonstrates how to use the Dart API to add punctuations to text.
version: 1.0.0
environment:
sdk: ^3.4.0
dependencies:
sherpa_onnx: ^1.10.20
path: ^1.9.0
args: ^2.5.0
dev_dependencies:
lints: ^3.0.0

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -ex
dart pub get
if [[ ! -f ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx ]]; then
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
tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
fi
dart run \
./bin/punctuations.dart \
--model ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx

View File

@@ -9,7 +9,7 @@ environment:
sdk: ^3.4.0
dependencies:
sherpa_onnx: ^1.10.19
sherpa_onnx: ^1.10.20
path: ^1.9.0
args: ^2.5.0

View File

@@ -9,7 +9,7 @@ environment:
sdk: ^3.4.0
dependencies:
sherpa_onnx: ^1.10.19
sherpa_onnx: ^1.10.20
# sherpa_onnx:
# path: ../../flutter/sherpa_onnx
path: ^1.9.0

View File

@@ -10,7 +10,7 @@ environment:
# Add regular dependencies here.
dependencies:
sherpa_onnx: ^1.10.19
sherpa_onnx: ^1.10.20
path: ^1.9.0
args: ^2.5.0

View File

@@ -11,7 +11,7 @@ environment:
# Add regular dependencies here.
dependencies:
sherpa_onnx: ^1.10.19
sherpa_onnx: ^1.10.20
path: ^1.9.0
args: ^2.5.0

View File

@@ -8,7 +8,7 @@ environment:
# Add regular dependencies here.
dependencies:
sherpa_onnx: ^1.10.19
sherpa_onnx: ^1.10.20
path: ^1.9.0
args: ^2.5.0

View File

@@ -10,7 +10,7 @@ environment:
sdk: ^3.4.0
dependencies:
sherpa_onnx: ^1.10.19
sherpa_onnx: ^1.10.20
path: ^1.9.0
args: ^2.5.0

View File

@@ -9,7 +9,7 @@ environment:
sdk: ^3.4.0
dependencies:
sherpa_onnx: ^1.10.19
sherpa_onnx: ^1.10.20
path: ^1.9.0
args: ^2.5.0