`vendor/MetasequoiaImeEngine` submodule 改为 `engine/` 目录,导入的是 product-lock.json 原本钉住的提交 c810d201,逐文件核对过 blob,因此这是一次 搬迁而不是引擎升级。引擎自己的嵌套 submodule(googlepinyinime-rev、utfcpp、 miniaudio)按各自 gitlink 一并展开,来源与提交见 engine/UPSTREAM.md。 Windows 专门化裁掉了不属于本产品的部分:whisper.cpp 与本地推理 provider、 macOS 示例宿主、整个 dictionary/ 建库流水线、上游 CI 与 release-please 配置。 词库仍按 product-lock.json 在构建时取发布产物。 引擎不再进 product-lock.json:没有 gitlink 就没有可比对的东西,锁里写一个上游 commit 等于宣称这棵树仍等于上游,而第一个 Windows 专门化改动落地时这句话就假了。 verify-contracts / verify-checkout 随之移除,verify-published 保留。 CI 与工具链:引擎源码并入根 scripts/format.sh(删掉引擎自己的那份,避免两套排除 列表分叉);引擎 voice.yml 里的 silero_vad.onnx 摘要校验移到本仓 lock job; engine-bump-triage 工作流与其测试删除;gitsubmodule 的 dependabot 频率由 daily 改为 monthly。 README、README.en、AGENTS.md、CONTRIBUTING、docs/product-release.md 及各子目录 文档同步说明新的位置与边界。
93 lines
2.7 KiB
C++
93 lines
2.7 KiB
C++
#include "pinyin_decoder.h"
|
|
#include "data_path.h"
|
|
#include "../googlepinyinime-rev/src/include/pinyinime.h"
|
|
#include "../googlepinyinime-rev/src/include/lpicache.h"
|
|
#include <mutex>
|
|
#include <utf8.h>
|
|
|
|
namespace metasequoia
|
|
{
|
|
namespace
|
|
{
|
|
struct DecoderService
|
|
{
|
|
std::mutex mutex;
|
|
std::filesystem::path model;
|
|
std::filesystem::path user_dictionary;
|
|
bool ready = false;
|
|
std::size_t clients = 0;
|
|
~DecoderService()
|
|
{
|
|
ime_pinyin::im_close_decoder();
|
|
}
|
|
};
|
|
|
|
DecoderService &service()
|
|
{
|
|
static DecoderService instance;
|
|
return instance;
|
|
}
|
|
} // namespace
|
|
|
|
PinyinDecoder::PinyinDecoder(std::filesystem::path model, std::filesystem::path user_dictionary)
|
|
: model_(std::move(model)), user_dictionary_(std::move(user_dictionary))
|
|
{
|
|
auto &decoder = service();
|
|
std::lock_guard lock(decoder.mutex);
|
|
++decoder.clients;
|
|
}
|
|
|
|
PinyinDecoder::~PinyinDecoder()
|
|
{
|
|
auto &decoder = service();
|
|
std::lock_guard lock(decoder.mutex);
|
|
if (--decoder.clients == 0)
|
|
{
|
|
ime_pinyin::im_close_decoder();
|
|
decoder.ready = false;
|
|
decoder.model.clear();
|
|
decoder.user_dictionary.clear();
|
|
}
|
|
}
|
|
|
|
std::string PinyinDecoder::sentence(const std::string &pinyin) const
|
|
{
|
|
if (pinyin.empty() || pinyin.size() > 128 || model_.empty() || user_dictionary_.empty())
|
|
return {};
|
|
auto &decoder = service();
|
|
std::lock_guard lock(decoder.mutex);
|
|
if (decoder.model != model_ || decoder.user_dictionary != user_dictionary_)
|
|
{
|
|
ime_pinyin::im_close_decoder();
|
|
decoder.ready = false;
|
|
// The upstream lemma cache is also global and contains model-specific IDs.
|
|
auto &cache = ime_pinyin::LpiCache::get_instance();
|
|
for (ime_pinyin::uint16 id = 0; id < ime_pinyin::kFullSplIdStart; ++id)
|
|
cache.put_cache(id, nullptr, 0);
|
|
decoder.model = model_;
|
|
decoder.user_dictionary = user_dictionary_;
|
|
decoder.ready =
|
|
ime_pinyin::im_open_decoder(path_to_utf8(model_).c_str(), path_to_utf8(user_dictionary_).c_str());
|
|
if (!decoder.ready)
|
|
return {};
|
|
ime_pinyin::im_set_max_lens(128, 64);
|
|
}
|
|
if (!decoder.ready)
|
|
return {};
|
|
ime_pinyin::im_reset_search();
|
|
const auto count = ime_pinyin::im_search(pinyin.data(), pinyin.size());
|
|
for (std::size_t i = 0; i < count; ++i)
|
|
{
|
|
ime_pinyin::char16 buffer[256] = {};
|
|
if (!ime_pinyin::im_get_candidate(i, buffer, 255))
|
|
continue;
|
|
std::size_t length = 0;
|
|
while (length < 255 && buffer[length] != 0)
|
|
++length;
|
|
if (length)
|
|
return utf8::utf16to8(std::u16string(reinterpret_cast<const char16_t *>(buffer), length));
|
|
}
|
|
return {};
|
|
}
|
|
} // namespace metasequoia
|