1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 #ifndef _FCITX_LIBIME_CORE_HISTORYBIGRAM_H_
7 #define _FCITX_LIBIME_CORE_HISTORYBIGRAM_H_
8 
9 #include "libimecore_export.h"
10 #include <fcitx-utils/macros.h>
11 #include <libime/core/lattice.h>
12 #include <memory>
13 #include <string>
14 #include <string_view>
15 #include <vector>
16 
17 namespace libime {
18 
19 class HistoryBigramPrivate;
20 class HistoryBigram;
21 
22 class LIBIMECORE_EXPORT HistoryBigram {
23 public:
24     HistoryBigram();
25 
26     FCITX_DECLARE_VIRTUAL_DTOR_MOVE(HistoryBigram);
27 
28     void load(std::istream &in);
29     void save(std::ostream &out);
30     void dump(std::ostream &out);
31     void clear();
32 
33     /// Set unknown probability penatly.
34     /// \param unknown is a log probability.
35     void setUnknownPenalty(float unknown);
36     float unknownPenalty() const;
37 
38     void setUseOnlyUnigram(bool useOnlyUnigram);
39     bool useOnlyUnigram() const;
40 
41     void forget(std::string_view word);
42 
43     bool isUnknown(std::string_view v) const;
score(const WordNode * prev,const WordNode * cur)44     float score(const WordNode *prev, const WordNode *cur) const {
45         return score(prev ? prev->word() : "", cur ? cur->word() : "");
46     }
47     float score(std::string_view prev, std::string_view cur) const;
48     void add(const SentenceResult &sentence);
49     void add(const std::vector<std::string> &sentence);
50 
51     /// Fill the prediction based on current sentence.
52     void fillPredict(std::unordered_set<std::string> &words,
53                      const std::vector<std::string> &sentence,
54                      size_t maxSize) const;
55 
56 private:
57     std::unique_ptr<HistoryBigramPrivate> d_ptr;
58     FCITX_DECLARE_PRIVATE(HistoryBigram);
59 };
60 } // namespace libime
61 
62 #endif // _FCITX_LIBIME_CORE_HISTORYBIGRAM_H_
63