OnlineAddPunctuation.java
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2025 Xiaomi Corporation
// This file shows how to use a punctuation model to add punctuations to text.
//
// The model supports ONLY English.
import com.k2fsa.sherpa.onnx.*;
public class OnlineAddPunctuation {
public static void main(String[] args) {
// please download the model from
// https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-online-punct-en-2024-08-06.tar.bz2
String model = "./sherpa-onnx-online-punct-en-2024-08-06/model.int8.onnx";
String bpeVocab = "./sherpa-onnx-online-punct-en-2024-08-06/bpe.vocab";
OnlinePunctuationModelConfig modelConfig =
OnlinePunctuationModelConfig.builder()
.setCnnBilstm(model)
.setBpeVocab(bpeVocab)
.setNumThreads(1)
.setDebug(true)
.build();
OnlinePunctuationConfig config =
OnlinePunctuationConfig.builder().setModel(modelConfig).build();
OnlinePunctuation punct = new OnlinePunctuation(config);
String[] sentences =
new String[] {
"how are you doing fantastic thank you how about you",
"The African blogosphere is rapidly expanding bringing more voices online in the form of"
+ " commentaries opinions analyses rants and poetry",
};
System.out.println("---");
for (String text : sentences) {
String out = punct.addPunctuation(text);
System.out.printf("Input: %s\n", text);
System.out.printf("Output: %s\n", out);
System.out.println("---");
}
}
}