7 #include "libime/pinyin/pinyindecoder.h" 10 #include <string_view> 12 #include "libime/core/languagemodel.h" 13 #include "libime/core/lattice.h" 14 #include "libime/core/segmentgraph.h" 15 #include "pinyindecoder_p.h" 19 PinyinLatticeNode::PinyinLatticeNode(
20 std::string_view word, WordIndex idx, SegmentGraphPath path,
21 const State &state,
float cost,
22 std::unique_ptr<PinyinLatticeNodePrivate> data)
23 : LatticeNode(word, idx, std::move(path), state, cost),
24 d_ptr(std::move(data)) {}
26 PinyinLatticeNode::~PinyinLatticeNode() =
default;
28 const std::string &PinyinLatticeNode::encodedPinyin()
const {
29 static const std::string empty;
33 return d_ptr->encodedPinyin_;
36 bool PinyinLatticeNode::isCorrection()
const {
40 return d_ptr->isCorrection_;
43 bool PinyinLatticeNode::anyCorrectionOnPath()
const {
44 const auto *pivot =
this;
46 if (pivot->isCorrection()) {
49 pivot =
static_cast<PinyinLatticeNode *
>(pivot->prev());
54 LatticeNode *PinyinDecoder::createLatticeNodeImpl(
55 const SegmentGraphBase &graph,
const LanguageModelBase *model,
56 std::string_view word, WordIndex idx, SegmentGraphPath path,
57 const State &state,
float cost, std::unique_ptr<LatticeNodeData> data,
58 bool onlyPath)
const {
59 std::unique_ptr<PinyinLatticeNodePrivate> pinyinData(
60 static_cast<PinyinLatticeNodePrivate *>(data.release()));
61 if (model->isUnknown(idx, word)) {
64 if ((pinyinData && pinyinData->encodedPinyin_.size() == 2) &&
65 path.front() != &graph.start() && !onlyPath) {
70 return new PinyinLatticeNode(word, idx, std::move(path), state, cost,
71 std::move(pinyinData));