libime
pinyinprediction.h
1 /*
2  * SPDX-FileCopyrightText: 2023-2023 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 #ifndef _FCITX_LIBIME_PINYIN_PREDICTION_H_
7 #define _FCITX_LIBIME_PINYIN_PREDICTION_H_
8 
9 #include <cstddef>
10 #include <memory>
11 #include <string>
12 #include <string_view>
13 #include <utility>
14 #include <vector>
15 #include <fcitx-utils/macros.h>
16 #include <libime/core/languagemodel.h>
17 #include <libime/core/prediction.h>
18 #include <libime/pinyin/libimepinyin_export.h>
19 #include <libime/pinyin/pinyindictionary.h>
20 #include "libime/core/historybigram.h"
21 
22 namespace libime {
23 
24 class PinyinPredictionPrivate;
25 
26 enum class PinyinPredictionSource { Model, Dictionary };
27 
28 /**
29  * This is a prediction class that allows to predict against both model and
30  * pinyin dictionary.
31  */
32 class LIBIMEPINYIN_EXPORT PinyinPrediction : public Prediction {
33 public:
35  virtual ~PinyinPrediction();
36 
37  /**
38  * Set the pinyin dictionary used for prediction.
39  */
40  void setPinyinDictionary(const PinyinDictionary *dict);
41 
42  /**
43  * Predict from model and pinyin dictionary for the last sentnce being type.
44  */
45  std::vector<std::pair<std::string, PinyinPredictionSource>>
46  predict(const State &state, const std::vector<std::string> &sentence,
47  std::string_view lastEncodedPinyin, size_t maxSize = 0);
48 
49  /**
50  * Predict from model and pinyin dictionary for the last sentnce being type.
51  *
52  * @since 1.1.14
53  */
54  std::vector<std::pair<std::string, PinyinPredictionSource>>
55  predict(const State &state,
56  const std::vector<libime::HistoryBigram::WordWithCode> &sentence,
57  size_t maxSize = 0);
58 
59  /**
60  * Same as the Prediction::predict with the same signature.
61  */
62  std::vector<std::string>
63  predict(const std::vector<std::string> &sentence = {}, size_t maxSize = 0);
64 
65 private:
66  std::unique_ptr<PinyinPredictionPrivate> d_ptr;
67  FCITX_DECLARE_PRIVATE(PinyinPrediction);
68 };
69 
70 } // namespace libime
71 
72 #endif // _LIBIM_LIBIME_CORE_PREDICTION_H_
PinyinDictionary is a set of dictionaries for Pinyin.
This is a prediction class that allows to predict against both model and pinyin dictionary.