-
Notifications
You must be signed in to change notification settings - Fork 570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add context biasing for mobile #568
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -76,11 +76,24 @@ class SherpaOnnx { | |||||||
|
||||||||
bool IsReady() const { return recognizer_.IsReady(stream_.get()); } | ||||||||
|
||||||||
void Reset(bool recreate) { | ||||||||
if (recreate) { | ||||||||
// If keywords is an empty string, it just recreates the decoding stream | ||||||||
// If keywords is not empty, it will create a new decoding stream with | ||||||||
// the given keywords appended to the default keywords. | ||||||||
void Reset(bool recreate, const std::string &keywords = {}) { | ||||||||
if (keywords.empty()) { | ||||||||
if (recreate) { | ||||||||
stream_ = recognizer_.CreateStream(); | ||||||||
} else { | ||||||||
} else { | ||||||||
recognizer_.Reset(stream_.get()); | ||||||||
} | ||||||||
} else { | ||||||||
auto stream = recognizer_.CreateStream(keywords); | ||||||||
// Set new keywords failed, the stream_ will not be updated. | ||||||||
if (stream != nullptr) { | ||||||||
stream_ = std::move(stream); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also use |
||||||||
} else { | ||||||||
SHERPA_ONNX_LOGE("Failed to set keywords: %s", keywords.c_str()); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
@@ -1509,9 +1522,12 @@ JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_SherpaOnnxOffline_delete( | |||||||
|
||||||||
SHERPA_ONNX_EXTERN_C | ||||||||
JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_SherpaOnnx_reset( | ||||||||
JNIEnv *env, jobject /*obj*/, jlong ptr, jboolean recreate) { | ||||||||
JNIEnv *env, jobject /*obj*/, jlong ptr, jboolean recreate, jstring keywords) { | ||||||||
auto model = reinterpret_cast<sherpa_onnx::SherpaOnnx *>(ptr); | ||||||||
model->Reset(recreate); | ||||||||
const char *p_keywords = env->GetStringUTFChars(keywords, nullptr); | ||||||||
std::string keywords_str = p_keywords; | ||||||||
model->Reset(recreate, keywords_str); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You can pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Does it cause compiler errors? If yes, please post the error logs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're right, it works normally |
||||||||
env->ReleaseStringUTFChars(keywords, p_keywords); | ||||||||
} | ||||||||
|
||||||||
SHERPA_ONNX_EXTERN_C | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -188,7 +188,7 @@ class SherpaOnnxOnlineRecongitionResult { | |||||
class SherpaOnnxRecognizer { | ||||||
/// A pointer to the underlying counterpart in C | ||||||
let recognizer: OpaquePointer! | ||||||
let stream: OpaquePointer! | ||||||
var stream: OpaquePointer! | ||||||
|
||||||
/// Constructor taking a model config | ||||||
init( | ||||||
|
@@ -237,8 +237,23 @@ class SherpaOnnxRecognizer { | |||||
|
||||||
/// Reset the recognizer, which clears the neural network model state | ||||||
/// and the state for decoding. | ||||||
func reset() { | ||||||
Reset(recognizer, stream) | ||||||
/// If hotWords is an empty string, it just recreates the decoding stream | ||||||
/// If hotWords is not empty, it will create a new decoding stream with | ||||||
/// the given hotWords appended to the default hotWords. | ||||||
func reset(hotWords: String? = nil) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||
guard let words = hotWords, !words.isEmpty else { | ||||||
Reset(recognizer, stream) | ||||||
return | ||||||
} | ||||||
|
||||||
words.withCString { cString in | ||||||
let newStream = CreateOnlineStreamWithHotwords(recognizer, cString) | ||||||
// lock while release and replace stream | ||||||
objc_sync_enter(self) | ||||||
DestroyOnlineStream(stream) | ||||||
stream = newStream | ||||||
objc_sync_enter(self) | ||||||
} | ||||||
} | ||||||
|
||||||
/// Signal that no more audio samples would be available. | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you indent the statements?
You can run
to check your code style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
All code style checks passed