Skip to content

Commit 27101c0

Browse files
texasichtexasich
andauthored
cli : merge tokens split across UTF-8 boundaries in JSON output (#3751)
* cli : merge tokens split across UTF-8 boundaries in JSON output When a multi-byte UTF-8 codepoint (most commonly a CJK character, 3 bytes) is split across multiple whisper tokens, the -ojf/--output-json-full writer emitted each token's partial bytes as its own JSON string, producing invalid UTF-8 that chokes downstream parsers. Merge adjacent tokens in output_json whenever the accumulated text still ends on an incomplete UTF-8 sequence. The merged entry keeps the first token's id/p/t_dtw and extends t1 to the last absorbed token, which matches how segment text is assembled elsewhere. Refs #1798 * fix: address review — add braces for consistency, use full issue URL - Add braces to if/else chain for codebase consistency - Use full URL for issue #1798 reference Review: @danbev --------- Co-authored-by: texasich <texasich@users.noreply.github.com> Co-authored-by: texasich <texasich@gmail.com>
1 parent e0fd1f6 commit 27101c0

1 file changed

Lines changed: 71 additions & 9 deletions

File tree

examples/cli/cli.cpp

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,39 @@ static void replace_all(std::string & s, const std::string & search, const std::
3131
}
3232
}
3333

34+
// Returns the number of trailing continuation bytes still needed for `s` to end
35+
// on a complete UTF-8 codepoint. Returns 0 if the tail of `s` is already a
36+
// complete codepoint (or if the tail looks malformed and we should stop merging).
37+
// Used to merge whisper tokens whose bytes split a multi-byte UTF-8 character
38+
// (e.g. CJK), so the JSON output stays valid UTF-8. See https://github.com/ggml-org/whisper.cpp/issues/1798.
39+
static int utf8_trailing_bytes_needed(const std::string & s) {
40+
const int n = (int) s.size();
41+
int i = n - 1;
42+
// walk back past continuation bytes (10xxxxxx)
43+
while (i >= 0 && ((unsigned char) s[i] & 0xC0) == 0x80) {
44+
--i;
45+
}
46+
if (i < 0) {
47+
// all continuation bytes, or empty — nothing we can do
48+
return 0;
49+
}
50+
const unsigned char c = (unsigned char) s[i];
51+
int expected;
52+
if ((c & 0x80) == 0x00) {
53+
expected = 1; // ASCII
54+
} else if ((c & 0xE0) == 0xC0) {
55+
expected = 2;
56+
} else if ((c & 0xF0) == 0xE0) {
57+
expected = 3;
58+
} else if ((c & 0xF8) == 0xF0) {
59+
expected = 4;
60+
} else {
61+
return 0; // malformed lead, give up
62+
}
63+
const int have = n - i;
64+
return have >= expected ? 0 : (expected - have);
65+
}
66+
3467
// command-line parameters
3568
struct whisper_params {
3669
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
@@ -738,18 +771,47 @@ static void output_json(
738771
if (full) {
739772
start_arr("tokens");
740773
const int n = whisper_full_n_tokens(ctx, i);
741-
for (int j = 0; j < n; ++j) {
742-
auto token = whisper_full_get_token_data(ctx, i, j);
774+
775+
// Merge adjacent tokens whose bytes together form a
776+
// single UTF-8 codepoint. Multi-byte characters (CJK
777+
// in particular) can end up split across whisper
778+
// tokens, which used to produce invalid UTF-8 in the
779+
// JSON string. Refs issue #1798.
780+
struct merged_token {
781+
std::string text;
782+
whisper_token_data data;
783+
int64_t t1;
784+
};
785+
std::vector<merged_token> merged;
786+
merged.reserve(n);
787+
for (int j = 0; j < n; ) {
788+
auto tok = whisper_full_get_token_data(ctx, i, j);
789+
merged_token m{ whisper_token_to_str(ctx, tok.id), tok, tok.t1 };
790+
++j;
791+
while (j < n && utf8_trailing_bytes_needed(m.text) > 0) {
792+
auto tok_next = whisper_full_get_token_data(ctx, i, j);
793+
m.text += whisper_token_to_str(ctx, tok_next.id);
794+
if (tok_next.t1 > -1) {
795+
m.t1 = tok_next.t1;
796+
}
797+
++j;
798+
}
799+
merged.push_back(std::move(m));
800+
}
801+
802+
const int nm = (int) merged.size();
803+
for (int j = 0; j < nm; ++j) {
804+
const auto & mt = merged[j];
743805
start_obj(nullptr);
744-
value_s("text", whisper_token_to_str(ctx, token.id), false);
745-
if(token.t0 > -1 && token.t1 > -1) {
806+
value_s("text", mt.text.c_str(), false);
807+
if (mt.data.t0 > -1 && mt.t1 > -1) {
746808
// If we have per-token timestamps, write them out
747-
times_o(token.t0, token.t1, false);
809+
times_o(mt.data.t0, mt.t1, false);
748810
}
749-
value_i("id", token.id, false);
750-
value_f("p", token.p, false);
751-
value_f("t_dtw", token.t_dtw, true);
752-
end_obj(j == (n - 1));
811+
value_i("id", mt.data.id, false);
812+
value_f("p", mt.data.p, false);
813+
value_f("t_dtw", mt.data.t_dtw, true);
814+
end_obj(j == (nm - 1));
753815
}
754816
end_arr(!params.diarize && !params.tinydiarize);
755817
}

0 commit comments

Comments
 (0)