Add Java and Kotlin API for punctuation models (#818)
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OfflinePunctuation {
|
||||
static {
|
||||
System.loadLibrary("sherpa-onnx-jni");
|
||||
}
|
||||
|
||||
private long ptr = 0; // this is the asr engine ptrss
|
||||
|
||||
public OfflinePunctuation(OfflinePunctuationConfig config) {
|
||||
ptr = newFromFile(config);
|
||||
}
|
||||
|
||||
public String addPunctuation(String text) {
|
||||
return addPunctuation(ptr, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
}
|
||||
|
||||
// You'd better call it manually if it is not used anymore
|
||||
public void release() {
|
||||
if (this.ptr == 0) {
|
||||
return;
|
||||
}
|
||||
delete(this.ptr);
|
||||
this.ptr = 0;
|
||||
}
|
||||
|
||||
private native void delete(long ptr);
|
||||
|
||||
private native long newFromFile(OfflinePunctuationConfig config);
|
||||
|
||||
private native String addPunctuation(long ptr, String text);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OfflinePunctuationConfig {
|
||||
private final OfflinePunctuationModelConfig model;
|
||||
|
||||
private OfflinePunctuationConfig(Builder builder) {
|
||||
this.model = builder.model;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public OfflinePunctuationModelConfig getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
public static class Builder {
|
||||
private OfflinePunctuationModelConfig model = OfflinePunctuationModelConfig.builder().build();
|
||||
|
||||
public OfflinePunctuationConfig build() {
|
||||
return new OfflinePunctuationConfig(this);
|
||||
}
|
||||
|
||||
public Builder setModel(OfflinePunctuationModelConfig model) {
|
||||
this.model = model;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OfflinePunctuationModelConfig {
|
||||
private final String ctTransformer;
|
||||
private final int numThreads;
|
||||
private final boolean debug;
|
||||
private final String provider;
|
||||
|
||||
private OfflinePunctuationModelConfig(Builder builder) {
|
||||
this.ctTransformer = builder.ctTransformer;
|
||||
this.numThreads = builder.numThreads;
|
||||
this.debug = builder.debug;
|
||||
this.provider = builder.provider;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getCtTransformer() {
|
||||
return ctTransformer;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String ctTransformer = "";
|
||||
private int numThreads = 1;
|
||||
private boolean debug = true;
|
||||
private String provider = "cpu";
|
||||
|
||||
public OfflinePunctuationModelConfig build() {
|
||||
return new OfflinePunctuationModelConfig(this);
|
||||
}
|
||||
|
||||
public Builder setCtTransformer(String ctTransformer) {
|
||||
this.ctTransformer = ctTransformer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setNumThreads(int numThreads) {
|
||||
this.numThreads = numThreads;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDebug(boolean debug) {
|
||||
this.debug = debug;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setProvider(String provider) {
|
||||
this.provider = provider;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user