sherpa-display.h
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#include <stdlib.h>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <string>
namespace sherpa_onnx::cxx {
class SherpaDisplay {
public:
void UpdateText(const std::string &text) { current_text_ = text; }
void FinalizeCurrentSentence() {
if (!current_text_.empty() &&
(current_text_[0] != ' ' || current_text_.size() > 1)) {
sentences_.push_back({GetCurrentDateTime(), std::move(current_text_)});
}
}
void Display() const {
if (!sentences_.empty() || !current_text_.empty()) {
ClearScreen();
}
printf("=== Speech Recognition with Next-gen Kaldi ===\n");
printf("------------------------------\n");
if (!sentences_.empty()) {
int32_t i = 1;
for (const auto &p : sentences_) {
printf("[%s] %d. %s\n", p.first.c_str(), i, p.second.c_str());
i += 1;
}
printf("------------------------------\n");
}
if (!current_text_.empty()) {
printf("Recognizing: %s\n", current_text_.c_str());
}
}
private:
static void ClearScreen() {
#ifdef _MSC_VER
auto ret = system("cls");
#else
auto ret = system("clear");
#endif
(void)ret;
}
static std::string GetCurrentDateTime() {
std::ostringstream os;
auto t = std::time(nullptr);
auto tm = std::localtime(&t);
os << std::put_time(tm, "%Y-%m-%d %H:%M:%S");
return os.str();
}
private:
std::vector<std::pair<std::string, std::string>> sentences_;
std::string current_text_;
};
} // namespace sherpa_onnx::cxx