Spaces:
Sleeping
Sleeping
main : fix typo in JSON output (#648)
Browse files* typo in JSON output
* fix double quotes in JSON output
- examples/main/main.cpp +38 -3
examples/main/main.cpp
CHANGED
|
@@ -371,6 +371,39 @@ bool output_csv(struct whisper_context * ctx, const char * fname) {
|
|
| 371 |
return true;
|
| 372 |
}
|
| 373 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
bool output_json(struct whisper_context * ctx, const char * fname, const whisper_params & params) {
|
| 375 |
std::ofstream fout(fname);
|
| 376 |
int indent = 0;
|
|
@@ -414,7 +447,9 @@ bool output_json(struct whisper_context * ctx, const char * fname, const whisper
|
|
| 414 |
|
| 415 |
auto value_s = [&](const char *name, const char *val, bool end = false) {
|
| 416 |
start_value(name);
|
| 417 |
-
|
|
|
|
|
|
|
| 418 |
};
|
| 419 |
|
| 420 |
auto end_value = [&](bool end = false) {
|
|
@@ -455,7 +490,7 @@ bool output_json(struct whisper_context * ctx, const char * fname, const whisper
|
|
| 455 |
value_i("ctx", whisper_model_n_text_ctx(ctx));
|
| 456 |
value_i("state", whisper_model_n_text_state(ctx));
|
| 457 |
value_i("head", whisper_model_n_text_head(ctx));
|
| 458 |
-
value_i("
|
| 459 |
end_obj();
|
| 460 |
value_i("mels", whisper_model_n_mels(ctx));
|
| 461 |
value_i("f16", whisper_model_f16(ctx), true);
|
|
@@ -477,7 +512,7 @@ bool output_json(struct whisper_context * ctx, const char * fname, const whisper
|
|
| 477 |
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
| 478 |
|
| 479 |
start_obj();
|
| 480 |
-
start_obj("
|
| 481 |
value_s("from", to_timestamp(t0, true).c_str());
|
| 482 |
value_s("to", to_timestamp(t1, true).c_str(), true);
|
| 483 |
end_obj();
|
|
|
|
| 371 |
return true;
|
| 372 |
}
|
| 373 |
|
| 374 |
+
char *escape_double_quotes(const char *str) {
|
| 375 |
+
if (str == NULL) {
|
| 376 |
+
return NULL;
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
size_t escaped_length = strlen(str) + 1;
|
| 380 |
+
|
| 381 |
+
for (size_t i = 0; str[i] != '\0'; i++) {
|
| 382 |
+
if (str[i] == '"') {
|
| 383 |
+
escaped_length++;
|
| 384 |
+
}
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
char *escaped = (char *)calloc(escaped_length, 1); // pre-zeroed
|
| 388 |
+
if (escaped == NULL) {
|
| 389 |
+
return NULL;
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
size_t pos = 0;
|
| 393 |
+
for (size_t i = 0; str[i] != '\0'; i++) {
|
| 394 |
+
if (str[i] == '"') {
|
| 395 |
+
escaped[pos++] = '\\';
|
| 396 |
+
escaped[pos++] = '"';
|
| 397 |
+
} else {
|
| 398 |
+
escaped[pos++] = str[i];
|
| 399 |
+
}
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
// no need to set zero due to calloc() being used prior
|
| 403 |
+
|
| 404 |
+
return escaped;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
bool output_json(struct whisper_context * ctx, const char * fname, const whisper_params & params) {
|
| 408 |
std::ofstream fout(fname);
|
| 409 |
int indent = 0;
|
|
|
|
| 447 |
|
| 448 |
auto value_s = [&](const char *name, const char *val, bool end = false) {
|
| 449 |
start_value(name);
|
| 450 |
+
char * val_escaped = escape_double_quotes(val);
|
| 451 |
+
fout << "\"" << val_escaped << (end ? "\"\n" : "\",\n");
|
| 452 |
+
free(val_escaped);
|
| 453 |
};
|
| 454 |
|
| 455 |
auto end_value = [&](bool end = false) {
|
|
|
|
| 490 |
value_i("ctx", whisper_model_n_text_ctx(ctx));
|
| 491 |
value_i("state", whisper_model_n_text_state(ctx));
|
| 492 |
value_i("head", whisper_model_n_text_head(ctx));
|
| 493 |
+
value_i("layer", whisper_model_n_text_layer(ctx), true);
|
| 494 |
end_obj();
|
| 495 |
value_i("mels", whisper_model_n_mels(ctx));
|
| 496 |
value_i("f16", whisper_model_f16(ctx), true);
|
|
|
|
| 512 |
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
| 513 |
|
| 514 |
start_obj();
|
| 515 |
+
start_obj("timestamps");
|
| 516 |
value_s("from", to_timestamp(t0, true).c_str());
|
| 517 |
value_s("to", to_timestamp(t1, true).c_str(), true);
|
| 518 |
end_obj();
|