libime
pinyindecoder.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 
7 #include "libime/pinyin/pinyindecoder.h"
8 #include <memory>
9 #include <string>
10 #include <string_view>
11 #include <utility>
12 #include "libime/core/languagemodel.h"
13 #include "libime/core/lattice.h"
14 #include "libime/core/segmentgraph.h"
15 #include "pinyindecoder_p.h"
16 
17 namespace libime {
18 
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)) {}
25 
26 PinyinLatticeNode::~PinyinLatticeNode() = default;
27 
28 const std::string &PinyinLatticeNode::encodedPinyin() const {
29  static const std::string empty;
30  if (!d_ptr) {
31  return empty;
32  }
33  return d_ptr->encodedPinyin_;
34 }
35 
36 bool PinyinLatticeNode::isCorrection() const {
37  if (!d_ptr) {
38  return false;
39  }
40  return d_ptr->isCorrection_;
41 }
42 
43 bool PinyinLatticeNode::anyCorrectionOnPath() const {
44  const auto *pivot = this;
45  while (pivot) {
46  if (pivot->isCorrection()) {
47  return true;
48  }
49  pivot = static_cast<PinyinLatticeNode *>(pivot->prev());
50  }
51  return false;
52 }
53 
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)) {
62  // we don't really care about a lot of unknown single character
63  // which is not used for candidates
64  if ((pinyinData && pinyinData->encodedPinyin_.size() == 2) &&
65  path.front() != &graph.start() && !onlyPath) {
66  return nullptr;
67  }
68  }
69 
70  return new PinyinLatticeNode(word, idx, std::move(path), state, cost,
71  std::move(pinyinData));
72 }
73 } // namespace libime