libime
pinyincontext.h
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_PINYIN_PINYINCONTEXT_H_
7 #define _FCITX_LIBIME_PINYIN_PINYINCONTEXT_H_
8 
9 #include <cstddef>
10 #include <memory>
11 #include <span>
12 #include <string>
13 #include <string_view>
14 #include <unordered_set>
15 #include <utility>
16 #include <vector>
17 #include <fcitx-utils/macros.h>
18 #include <libime/core/inputbuffer.h>
19 #include <libime/core/languagemodel.h>
20 #include <libime/core/lattice.h>
21 #include <libime/pinyin/libimepinyin_export.h>
22 #include "libime/core/historybigram.h"
23 
24 namespace libime {
25 class PinyinIME;
26 class PinyinContextPrivate;
27 enum class PinyinPreeditMode;
28 
29 class LIBIMEPINYIN_EXPORT PinyinContext : public InputBuffer {
30 public:
32  virtual ~PinyinContext();
33 
34  void setUseShuangpin(bool sp);
35  bool useShuangpin() const;
36 
37  void erase(size_t from, size_t to) override;
38  void setCursor(size_t pos) override;
39 
40  int maxSentenceLength() const;
41  void setMaxSentenceLength(int length);
42 
43  const std::vector<SentenceResult> &candidates() const;
44 
45  /**
46  * Return the set of candidates, useful for deduplication.
47  *
48  * @see PinyinContext::candidates
49  * @since 1.0.18
50  */
51  const std::unordered_set<std::string> &candidateSet() const;
52 
53  const std::vector<SentenceResult> &candidatesToCursor() const;
54 
55  /**
56  * Return the set of candidates to current cursor.
57  *
58  * @see PinyinContext::candidatesToCursor
59  * @since 1.0.18
60  */
61  const std::unordered_set<std::string> &candidatesToCursorSet() const;
62  void select(size_t idx);
63  void selectCandidatesToCursor(size_t idx);
64  void cancel();
65  bool cancelTill(size_t pos);
66 
67  /**
68  * Create a custom selection
69  *
70  * This allows Engine to do make a custom selection that is not pinyin.
71  *
72  * @param inputLength the length of characters to match in the input
73  * @param segment segment
74  * @param encodedPinyin whether this segment has a pinyin
75  * @since 1.1.7
76  */
77  void selectCustom(size_t inputLength, std::string_view segment,
78  std::string_view encodedPinyin = "");
79 
80  /// Whether the input is fully selected.
81  bool selected() const;
82 
83  /// The sentence for this context, can be used as preedit.
84  std::string sentence() const {
85  const auto &c = candidates();
86  if (!c.empty()) {
87  return selectedSentence() + c[0].toString();
88  }
89  return selectedSentence();
90  }
91 
92  std::string preedit(PinyinPreeditMode mode) const;
93 
94  /// Mixed preedit (selected hanzi + pinyin).
95  std::pair<std::string, size_t>
96  preeditWithCursor(PinyinPreeditMode mode) const;
97 
98  std::string preedit() const;
99 
100  /// Mixed preedit (selected hanzi + pinyin).
101  std::pair<std::string, size_t> preeditWithCursor() const;
102 
103  /// Selected hanzi.
104  std::string selectedSentence() const;
105 
106  /// Selected pinyin length.
107  size_t selectedLength() const;
108 
109  /// Selected hanzi segments.
110  std::vector<std::string> selectedWords() const;
111 
112  /// Selected hanzi with encoded pinyin
113  std::vector<HistoryBigram::WordWithCode> selectedWordsWithPinyin() const;
114 
115  /// Get the full pinyin string of the selected part.
116  std::string selectedFullPinyin() const;
117 
118  /// Get the full pinyin string of certain candidate.
119  std::string candidateFullPinyin(size_t i) const;
120 
121  /// Get the full pinyin string of certain candidate.
122  std::string candidateFullPinyin(const SentenceResult &candidate) const;
123 
124  /// Add the selected part to history if selected() == true.
125  void learn();
126 
127  /// Return the position of last pinyin. E.g. 你h|ao, return the offset
128  /// before h.
129  int pinyinBeforeCursor() const;
130 
131  /// Return the position of last pinyin. E.g. 你h|ao, return the offset after
132  /// h.
133  int pinyinAfterCursor() const;
134 
135  PinyinIME *ime() const;
136 
137  /// Opaque language model state.
138  State state() const;
139 
140  /**
141  * Set context words for better prediction.
142  * @param contextWords The context words.
143  * @since 1.1.13
144  */
145  void setContextWords(const std::vector<std::string> &contextWords);
146 
147  /**
148  * Clear context words.
149  * @since 1.1.13
150  */
151  void clearContextWords();
152 
153  /**
154  * Append context words for better prediction.
155  * @param contextWords The context words.
156  * @since 1.1.13
157  */
158  void appendContextWords(const std::vector<std::string> &contextWords);
159 
160  /**
161  * Get context words for better prediction.
162  * @return current context words
163  * @since 1.1.13
164  */
165  std::vector<std::string> contextWords() const;
166 
167  /**
168  * Set context words with pinyin for better prediction.
169  * @param contextWordsWithPinyin The context words with encoded pinyin.
170  * @since 1.1.14
171  */
172  void setContextWordsWithPinyin(
173  const std::vector<HistoryBigram::WordWithCode> &contextWordsWithPinyin);
174 
175  /**
176  * Append context words with pinyin for better prediction.
177  * @param contextWordsWithPinyin The context words with pinyin.
178  * @since 1.1.14
179  */
180  void appendContextWordsWithPinyin(
181  const std::vector<HistoryBigram::WordWithCode> &contextWordsWithPinyin);
182 
183  /**
184  * Get context words with pinyin for better prediction.
185  * @return current context words with pinyin
186  * @since 1.1.14
187  */
188  std::vector<HistoryBigram::WordWithCode> contextWordsWithPinyin() const;
189 
190 protected:
191  bool typeImpl(const char *s, size_t length) override;
192 
193 private:
194  void update();
195  bool learnWord();
196  std::unique_ptr<PinyinContextPrivate> d_ptr;
197  FCITX_DECLARE_PRIVATE(PinyinContext);
198 };
199 } // namespace libime
200 
201 #endif // _FCITX_LIBIME_PINYIN_PINYINCONTEXT_H_
std::string sentence() const
The sentence for this context, can be used as preedit.
Definition: pinyincontext.h:84
Provides shared data for PinyinContext.
Definition: pinyinime.h:28