6 #ifndef _FCITX_LIBIME_CORE_LATTICE_H_ 7 #define _FCITX_LIBIME_CORE_LATTICE_H_ 10 #include <boost/type_traits/add_const.hpp> 18 #include <string_view> 19 #include <type_traits> 20 #include <unordered_set> 23 #include <boost/iterator/iterator_categories.hpp> 24 #include <boost/range/any_range.hpp> 25 #include <fcitx-utils/macros.h> 26 #include <fcitx-utils/stringutils.h> 27 #include <libime/core/languagemodel.h> 28 #include <libime/core/libimecore_export.h> 29 #include <libime/core/segmentgraph.h> 38 using Sentence = std::vector<const LatticeNode *>;
40 : sentence_(std::move(sentence)), score_(score) {}
43 const Sentence &sentence()
const {
return sentence_; }
44 size_t size()
const {
return sentence_.size(); }
46 float score()
const {
return score_; }
47 void adjustScore(
float adjust) { score_ += adjust; }
48 void setScore(
float score) { score_ = score; }
51 return score_ < rhs.score_;
55 return score_ > rhs.score_;
58 std::string toString()
const;
67 WordNode(std::string_view word, WordIndex idx) : word_(word), idx_(idx) {}
71 std::is_nothrow_move_constructible_v<std::string>);
73 std::is_nothrow_move_assignable_v<std::string>);
75 const std::string &word()
const {
return word_; }
76 WordIndex idx()
const {
return idx_; }
77 void setIdx(WordIndex idx) { idx_ = idx; }
91 LatticeNode(std::string_view word, WordIndex idx, SegmentGraphPath path,
92 const State &state,
float cost = 0)
93 :
WordNode(word, idx), path_(std::move(path)), cost_(cost),
95 assert(path_.size() >= 2);
97 float cost()
const {
return cost_; }
99 float score()
const {
return score_; }
100 void setScore(
float score) { score_ = score; }
104 const SegmentGraphPath &path()
const {
return path_; }
109 template <
typename T>
110 requires(std::is_base_of_v<LatticeNode, T>)
112 return static_cast<T &
>(*this);
115 template <
typename T>
116 requires(std::is_base_of_v<LatticeNode, T>)
117 const T &as()
const {
118 return static_cast<const T &
>(*this);
124 const auto *pivot =
this;
126 length += pivot->word().size();
127 pivot = pivot->prev();
131 result.resize(length);
134 const auto &word = pivot->word();
135 length -= word.size();
136 std::copy(word.begin(), word.end(), result.begin() + length);
137 pivot = pivot->prev();
144 SentenceResult::Sentence result;
145 const auto *pivot =
this;
147 while (pivot->prev()) {
149 result.emplace_back(pivot);
151 pivot = pivot->prev();
154 std::ranges::reverse(result);
155 return {std::move(result), score() + adjust};
158 State &state() {
return state_; }
161 SegmentGraphPath path_;
168 inline std::string SentenceResult::toString()
const {
169 return fcitx::stringutils::join(
171 std::views::transform([](
const auto &item) ->
const std::string & {
196 FCITX_DECLARE_VIRTUAL_DTOR_MOVE(
Lattice)
198 size_t sentenceSize()
const;
201 void discardNode(
const std::unordered_set<const SegmentGraphNode *> &node);
204 boost::any_range<
LatticeNode, boost::bidirectional_traversal_tag,
205 const LatticeNode &>;
210 std::unique_ptr<LatticePrivate> d_ptr;
211 FCITX_DECLARE_PRIVATE(
Lattice);
215 #endif // _FCITX_LIBIME_CORE_LATTICE_H_
std::string fullWord() const
Return the full word till the begining of the sentence.