77#define _FCITX_LIBIME_CORE_HISTORYBIGRAM_H_
88
99#include < cstddef>
10+ #include < cstdint>
11+ #include < functional>
1012#include < istream>
1113#include < memory>
1214#include < ostream>
1315#include < string>
1416#include < string_view>
1517#include < unordered_set>
18+ #include < utility>
1619#include < vector>
1720#include < fcitx-utils/macros.h>
1821#include < libime/core/lattice.h>
@@ -22,8 +25,13 @@ namespace libime {
2225
2326class HistoryBigramPrivate ;
2427
28+ using ValidationCodeExtractor = std::function<std::string(const WordNode *)>;
29+
2530class LIBIMECORE_EXPORT HistoryBigram {
2631public:
32+ using WordWithCode = std::pair<std::string, std::string>;
33+ using WordWithCodeView = std::pair<std::string_view, std::string_view>;
34+
2735 HistoryBigram ();
2836
2937 FCITX_DECLARE_VIRTUAL_DTOR_MOVE (HistoryBigram);
@@ -43,14 +51,20 @@ class LIBIMECORE_EXPORT HistoryBigram {
4351 bool useOnlyUnigram () const ;
4452
4553 void forget (std::string_view word);
54+ void forget (std::string_view word, std::string_view code);
4655
4756 bool isUnknown (std::string_view v) const ;
48- float score (const WordNode *prev, const WordNode *cur) const {
49- return score (prev ? prev->word () : " " , cur ? cur->word () : " " );
50- }
57+ float score (const WordNode *prev, const WordNode *cur) const ;
5158 float score (std::string_view prev, std::string_view cur) const ;
59+ float scoreWithCode (WordWithCodeView prev, WordWithCodeView cur) const ;
60+ float scoreWithCode (const WordNode *prev, const WordNode *cur,
61+ const ValidationCodeExtractor &extractor) const ;
5262 void add (const SentenceResult &sentence);
5363 void add (const std::vector<std::string> &sentence);
64+ void addWithCode (const SentenceResult &sentence,
65+ const ValidationCodeExtractor &validationCodeExtractor);
66+ void
67+ addWithCode (const std::vector<WordWithCode> &sentenceWithValidationCode);
5468
5569 // / Fill the prediction based on current sentence.
5670 void fillPredict (std::unordered_set<std::string> &words,
@@ -59,6 +73,35 @@ class LIBIMECORE_EXPORT HistoryBigram {
5973
6074 bool containsBigram (std::string_view prev, std::string_view cur) const ;
6175
76+ /* *
77+ * Query the weighted frequency of the unigram.
78+ *
79+ * @since 1.1.14
80+ */
81+ float unigramFrequency (WordWithCodeView word) const ;
82+
83+ /* *
84+ * Query the weighted frequency of the bigram.
85+ *
86+ * @since 1.1.14
87+ */
88+ float bigramFrequency (WordWithCodeView prev, WordWithCodeView cur) const ;
89+
90+ /* *
91+ * Query the raw frequency of the unigram.
92+ *
93+ * @since 1.1.14
94+ */
95+ int32_t rawUnigramFrequency (WordWithCodeView word) const ;
96+
97+ /* *
98+ * Query the raw frequency of the bigram.
99+ *
100+ * @since 1.1.14
101+ */
102+ int32_t rawBigramFrequency (WordWithCodeView prev,
103+ WordWithCodeView cur) const ;
104+
62105private:
63106 std::unique_ptr<HistoryBigramPrivate> d_ptr;
64107 FCITX_DECLARE_PRIVATE (HistoryBigram);
0 commit comments