libime
tablebaseddictionary.h
1 /*
2  * SPDX-FileCopyrightText: 2015-2020 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #ifndef _FCITX_LIBIME_TABLE_TABLEBASEDDICTIONARY_H_
9 #define _FCITX_LIBIME_TABLE_TABLEBASEDDICTIONARY_H_
10 
11 #include <cstddef>
12 #include <cstdint>
13 #include <functional>
14 #include <istream>
15 #include <memory>
16 #include <ostream>
17 #include <string>
18 #include <string_view>
19 #include <unordered_set>
20 #include <vector>
21 #include <fcitx-utils/connectableobject.h>
22 #include <fcitx-utils/macros.h>
23 #include <fcitx-utils/signals.h>
24 #include <libime/core/dictionary.h>
25 #include <libime/core/segmentgraph.h>
26 #include <libime/table/libimetable_export.h>
27 
28 namespace libime {
29 class TableBasedDictionaryPrivate;
30 class TableOptions;
31 
32 enum class PhraseFlag {
33  None = 1,
34  Pinyin,
35  Prompt,
36  ConstructPhrase,
37  User,
38  Auto,
39  Invalid
40 };
41 
42 using TableMatchCallback = std::function<bool(
43  std::string_view, std::string_view, uint32_t, PhraseFlag)>;
44 
45 enum class TableFormat { Text, Binary };
46 enum class TableMatchMode { Exact, Prefix };
47 
48 class TableRule;
49 
50 class LIBIMETABLE_EXPORT TableBasedDictionary
51  : public Dictionary,
52  public fcitx::ConnectableObject {
53  friend class TableContextPrivate;
54 
55 public:
57  virtual ~TableBasedDictionary();
58 
59  TableBasedDictionary(const TableBasedDictionary &other) = delete;
60 
61  void load(const char *filename, TableFormat format = TableFormat::Binary);
62  void load(std::istream &in, TableFormat format = TableFormat::Binary);
63  void save(const char *filename, TableFormat format = TableFormat::Binary);
64  void save(std::ostream &out, TableFormat format = TableFormat::Binary);
65 
66  void loadUser(const char *filename,
67  TableFormat format = TableFormat::Binary);
68  void loadUser(std::istream &in, TableFormat format = TableFormat::Binary);
69  void saveUser(const char *filename,
70  TableFormat format = TableFormat::Binary);
71  void saveUser(std::ostream &out, TableFormat format = TableFormat::Binary);
72 
73  size_t loadExtra(const char *filename,
74  TableFormat format = TableFormat::Binary);
75  size_t loadExtra(std::istream &in,
76  TableFormat format = TableFormat::Binary);
77  void saveExtra(size_t index, const char *filename,
78  TableFormat format = TableFormat::Binary);
79  void saveExtra(size_t index, std::ostream &out,
80  TableFormat format = TableFormat::Binary);
81 
82  void removeAllExtra();
83 
84  bool hasRule() const noexcept;
85  bool hasCustomPrompt() const noexcept;
86  const TableRule *findRule(std::string_view name) const;
87  bool insert(std::string_view key, std::string_view value,
88  PhraseFlag flag = PhraseFlag::None,
89  bool verifyWithRule = false);
90  bool insert(std::string_view value, PhraseFlag flag = PhraseFlag::None);
91  bool generate(std::string_view value, std::string &key) const;
92  bool generateWithHint(std::string_view value,
93  const std::vector<std::string> &codeHint,
94  std::string &key) const;
95 
96  bool isInputCode(uint32_t c) const;
97  bool isAllInputCode(std::string_view code) const;
98  bool isEndKey(uint32_t c) const;
99 
100  bool hasPinyin() const;
101  uint32_t maxLength() const;
102  bool isValidLength(size_t length) const;
103 
104  void statistic() const;
105 
106  void setTableOptions(TableOptions option);
107  const TableOptions &tableOptions() const;
108 
109  bool matchWords(std::string_view code, TableMatchMode mode,
110  const TableMatchCallback &callback) const;
111 
112  bool hasMatchingWords(std::string_view code) const;
113  bool hasMatchingWords(std::string_view code, std::string_view next) const;
114 
115  bool hasOneMatchingWord(std::string_view code) const;
116 
117  PhraseFlag wordExists(std::string_view code, std::string_view word) const;
118  void removeWord(std::string_view code, std::string_view word);
119 
120  std::string reverseLookup(std::string_view word,
121  PhraseFlag flag = PhraseFlag::None) const;
122  std::string hint(std::string_view key) const;
123 
124  FCITX_DECLARE_SIGNAL(TableBasedDictionary, tableOptionsChanged, void());
125 
126 private:
127  void loadText(std::istream &in);
128  void loadBinary(std::istream &in);
129  void saveText(std::ostream &out);
130  void saveBinary(std::ostream &origOut);
131 
132  void
133  matchPrefixImpl(const SegmentGraph &graph,
134  const GraphMatchCallback &callback,
135  const std::unordered_set<const SegmentGraphNode *> &ignore,
136  void *helper) const override;
137 
138  std::unique_ptr<TableBasedDictionaryPrivate> d_ptr;
139  FCITX_DECLARE_PRIVATE(TableBasedDictionary);
140 };
141 } // namespace libime
142 
143 #endif // _FCITX_LIBIME_TABLE_TABLEBASEDDICTIONARY_H_