-
Notifications
You must be signed in to change notification settings - Fork 414
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
Implement Tokens in Swift and Kotlin #227
Changes from 1 commit
240c04e
313ffbd
171c9f2
a0e0793
3b39860
92b0432
d1dfa87
25b708c
24aa98d
3d7c6f5
28d43bc
2b9c69e
48038a7
0a8d370
ccc3ba4
c313653
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -154,18 +154,23 @@ SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult( | |||||||||
memset(reinterpret_cast<void *>(const_cast<char *>(r->tokens)), 0, | ||||||||||
totalLength); | ||||||||||
r->timestamps = new float[r->count]; | ||||||||||
char **tokensTemp = new char*[r->count]; | ||||||||||
int pos = 0; | ||||||||||
for (int32_t i = 0; i < r->count; ++i) { | ||||||||||
tokensTemp[i] = const_cast<char*>(r->tokens) + pos; | ||||||||||
memcpy(reinterpret_cast<void *>(const_cast<char *>(r->tokens + pos)), | ||||||||||
result.tokens[i].c_str(), result.tokens[i].size()); | ||||||||||
// +1 to move past the null character | ||||||||||
pos += result.tokens[i].size() + 1; | ||||||||||
r->timestamps[i] = result.timestamps[i]; | ||||||||||
} | ||||||||||
|
||||||||||
r->tokensArr = const_cast<const char *const *>(tokensTemp); | ||||||||||
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. I'm done |
||||||||||
} else { | ||||||||||
r->count = 0; | ||||||||||
r->timestamps = nullptr; | ||||||||||
r->tokens = nullptr; | ||||||||||
r->tokensArr = nullptr; | ||||||||||
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. Please also add code to free it to avoid memory leak. 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. @csukuangfj sorry, what do you want to free? This line is just to initialize null for pointer incase there is no token from 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. Please change sherpa-onnx/sherpa-onnx/c-api/c-api.cc Lines 179 to 182 in 48038a7
to void DestroyOnlineRecognizerResult(const SherpaOnnxOnlineRecognizerResult *r) {
delete[] r->text;
delete[] r->json;
delete[] r->tokensArr;
delete r;
} By the way, please change the variable names:
|
||||||||||
} | ||||||||||
|
||||||||||
return r; | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,9 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOnlineRecognizerResult { | |
// which are seperated by \0 | ||
const char *tokens; | ||
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. I suggest that we define
where And the new In this way, it simplifies users' life as they only need to iterate
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. @csukuangfj I added |
||
|
||
// a pointer array contains the address of the first item in tokens | ||
const char *const *tokensArr; | ||
|
||
// Pointer to continuous memory which holds timestamps which | ||
// are seperated by \0 | ||
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. Please update the comment. It is not separated by \0 for timestamps. 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. fixed |
||
float *timestamps; | ||
|
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.