基于 ONNX 的实时语音转文本输入法,C++ 跨平台实现。 核心组件: - Qt 6 跨平台 GUI(实时识别 / 文件转写 / 配置页面) - ONNX Runtime 推理引擎(异步模型加载) - PortAudio 音频采集 - dr_libs 音频文件解码 - JSON 配置管理(线程安全,自动持久化) - 日志系统(控制台 + 文件输出) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
#include "tokenizer.h"
|
|
#include "utils/logger.h"
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
|
|
static const char* const kTag = "Tokenizer";
|
|
|
|
namespace impress {
|
|
|
|
Tokenizer::Tokenizer() = default;
|
|
|
|
bool Tokenizer::loadVocabulary(const QString& vocabPath) {
|
|
QFile file(vocabPath);
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
LOG_ERROR(kTag, QString("无法打开词表文件: %1").arg(vocabPath));
|
|
return false;
|
|
}
|
|
|
|
QTextStream stream(&file);
|
|
stream.setEncoding(QStringConverter::Utf8);
|
|
vocabulary_.clear();
|
|
|
|
while (!stream.atEnd()) {
|
|
QString line = stream.readLine().trimmed();
|
|
if (!line.isEmpty()) {
|
|
vocabulary_.push_back(line);
|
|
}
|
|
}
|
|
|
|
LOG_INFO(kTag, QString("词表已加载: %1 个词条").arg(vocabulary_.size()));
|
|
return true;
|
|
}
|
|
|
|
QString Tokenizer::decode(const std::vector<int>& tokens) const {
|
|
QString result;
|
|
for (int token : tokens) {
|
|
if (token >= 0 && token < static_cast<int>(vocabulary_.size())) {
|
|
result += vocabulary_[token];
|
|
} else {
|
|
result += QString("<unk:%1>").arg(token);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} // namespace impress
|