librime 1.2
Rime Input Method Engine, the core library
table.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-07-01 GONG Chen <chen.sst@gmail.com>
6//
7
8#ifndef RIME_TABLE_H_
9#define RIME_TABLE_H_
10
11#include <cstring>
12#include <rime/common.h>
16
17#define RIME_TABLE_UNION(U, V, A, a, B, b) \
18 struct U { \
19 V value; \
20 const A& a() const { \
21 return *reinterpret_cast<const A*>(this); \
22 } \
23 const B& b() const { \
24 return *reinterpret_cast<const B*>(this); \
25 } \
26 A& a() { \
27 return *reinterpret_cast<A*>(this); \
28 } \
29 B& b() { \
30 return *reinterpret_cast<B*>(this); \
31 } \
32 }
33
34namespace rime {
35
36namespace table {
37
38// union StringType {
39// String str;
40// StringId str_id;
41// };
42RIME_TABLE_UNION(StringType, int32_t, String, str, StringId, str_id);
43
45
47
48using Weight = float;
49
50struct Entry {
51 StringType text;
53};
54
55struct LongEntry {
58};
59
60struct PhraseIndex;
61
65};
66
68
73};
74
76
78
79// union PhraseIndex {
80// TrunkIndex trunk;
81// TailIndex tail;
82// };
83RIME_TABLE_UNION(PhraseIndex, Array<char>, TrunkIndex, trunk, TailIndex, tail);
84
86
87struct Metadata {
88 static const int kFormatMaxLength = 32;
91 uint32_t num_syllables;
92 uint32_t num_entries;
95 // v2
96 int32_t reserved_1;
97 int32_t reserved_2;
100};
101
102} // namespace table
103
105 public:
106 TableAccessor() = default;
108 const List<table::Entry>* entries,
109 double credibility = 0.0,
110 double quality_len = 0.0);
112 const Array<table::Entry>* entries,
113 double credibility = 0.0,
114 double quality_len = 0.0);
116 const table::TailIndex* code_map,
117 double credibility = 0.0,
118 double quality_len = 0.0);
119
120 RIME_DLL bool Next();
121
122 RIME_DLL bool exhausted() const;
123 RIME_DLL size_t remaining() const;
124 RIME_DLL const table::Entry* entry() const;
125 RIME_DLL const table::Code* extra_code() const;
126 const Code& index_code() const { return index_code_; }
127 Code code() const;
128 double credibility() const { return credibility_; }
129 double quality_len() const { return quality_len_; }
130
131 private:
132 Code index_code_;
133 const table::Entry* entries_ = nullptr;
134 const table::LongEntry* long_entries_ = nullptr;
135 size_t size_ = 0;
136 size_t cursor_ = 0;
137 double credibility_ = 0.0;
138 double quality_len_ = 0.0;
139};
140
141using TableQueryResult = map<int, vector<TableAccessor>>;
142
143struct SyllableGraph;
144
146 public:
147 TableQuery(table::Index* index) : lv1_index_(index) { Reset(); }
148
149 TableAccessor Access(SyllableId syllable_id,
150 double credibility = 0.0,
151 double quality_len = 0.0) const;
152
153 // down to next level
154 bool Advance(SyllableId syllable_id,
155 double credibility = 0.0,
156 double quality_len = 0.0,
157 size_t last_pos = 0);
158
159 // up one level
160 bool Backdate();
161
162 // back to root
163 void Reset();
164
165 size_t level() const { return level_; }
166
167 double credibility_sum() const {
168 return credibility_.empty() ? 0 : credibility_.back();
169 }
170 double quality_len_sum() const {
171 return quality_len_.empty() ? 0 : quality_len_.back();
172 }
173 size_t last_pos() const { return last_pos_.empty() ? 0 : last_pos_.back(); }
174
175 protected:
176 size_t level_ = 0;
178 vector<double> credibility_;
179 vector<double> quality_len_;
180 vector<size_t> last_pos_;
181
182 private:
183 bool Walk(SyllableId syllable_id);
184
185 table::HeadIndex* lv1_index_ = nullptr;
186 table::TrunkIndex* lv2_index_ = nullptr;
187 table::TrunkIndex* lv3_index_ = nullptr;
188 table::TailIndex* lv4_index_ = nullptr;
189};
190
191class Table : public MappedFile {
192 public:
194 virtual ~Table();
195
196 RIME_DLL bool Load();
197 RIME_DLL bool Save();
198 RIME_DLL bool Build(const Syllabary& syllabary,
199 const Vocabulary& vocabulary,
200 size_t num_entries,
201 uint32_t dict_file_checksum = 0);
202
203 bool GetSyllabary(Syllabary* syllabary);
204 RIME_DLL string GetSyllableById(int syllable_id);
205 RIME_DLL TableAccessor QueryWords(int syllable_id);
207 RIME_DLL bool Query(const SyllableGraph& syll_graph,
208 size_t start_pos,
209 TableQueryResult* result);
210 RIME_DLL string GetEntryText(const table::Entry& entry);
211
212 uint32_t dict_file_checksum() const;
213 table::Metadata* metadata() const { return metadata_; }
214
215 private:
216 table::Index* BuildIndex(const Vocabulary& vocabulary, size_t num_syllables);
217 table::HeadIndex* BuildHeadIndex(const Vocabulary& vocabulary,
218 size_t num_syllables);
219 table::TrunkIndex* BuildTrunkIndex(const Code& prefix,
220 const Vocabulary& vocabulary);
221 table::TailIndex* BuildTailIndex(const Code& prefix,
222 const Vocabulary& vocabulary);
223 bool BuildPhraseIndex(Code code,
224 const Vocabulary& vocabulary,
225 map<string, int>* index_data);
226 Array<table::Entry>* BuildEntryArray(const ShortDictEntryList& entries);
227 bool BuildEntryList(const ShortDictEntryList& src, List<table::Entry>* dest);
228 bool BuildEntry(const ShortDictEntry& dict_entry, table::Entry* entry);
229
230 string GetString(const table::StringType& x);
231 bool AddString(const string& src, table::StringType* dest, double weight);
232 bool OnBuildStart();
233 bool OnBuildFinish();
234 bool OnLoad();
235
236 protected:
240
243};
244
245} // namespace rime
246
247#endif // RIME_TABLE_H_
#define RIME_DLL
Definition rime_api.h:33
Definition algebra.cc:12
map< int, vector< TableAccessor > > TableQueryResult
Definition table.h:141
std::uint32_t StringId
Definition string_table.h:19
set< string > Syllabary
Definition vocabulary.h:17
std::unique_ptr< T > the
Definition common.h:58
int32_t SyllableId
Definition syllabifier.h:21
Definition table.h:36
List< SyllableId > Code
Definition table.h:46
HeadIndex Index
Definition table.h:85
Array< LongEntry > TailIndex
Definition table.h:77
Array< TrunkIndexNode > TrunkIndex
Definition table.h:75
Array< HeadIndexNode > HeadIndex
Definition table.h:67
float Weight
Definition table.h:48
Array< StringType > Syllabary
Definition table.h:44
RIME_TABLE_UNION(StringType, int32_t, String, str, StringId, str_id)
Definition syllabifier.h:39
Definition common.h:84
Definition mapped_file.h:22
Definition mapped_file.h:53
Definition mapped_file.h:61
Definition mapped_file.h:71
const path & file_path() const
Definition mapped_file.h:121
MappedFile(const path &file_path)
Definition mapped_file.cc:46
Definition table.h:50
StringType text
Definition table.h:51
Weight weight
Definition table.h:52
Definition table.h:55
Entry entry
Definition table.h:57
Code extra_code
Definition table.h:56
Definition table.h:62
List< Entry > entries
Definition table.h:63
OffsetPtr< PhraseIndex > next_level
Definition table.h:64
Definition table.h:69
List< Entry > entries
Definition table.h:71
SyllableId key
Definition table.h:70
OffsetPtr< PhraseIndex > next_level
Definition table.h:72
Definition table.h:87
int32_t reserved_1
Definition table.h:96
static const int kFormatMaxLength
Definition table.h:88
uint32_t dict_file_checksum
Definition table.h:90
uint32_t num_entries
Definition table.h:92
OffsetPtr< char > string_table
Definition table.h:98
uint32_t string_table_size
Definition table.h:99
OffsetPtr< Syllabary > syllabary
Definition table.h:93
uint32_t num_syllables
Definition table.h:91
int32_t reserved_2
Definition table.h:97
OffsetPtr< Index > index
Definition table.h:94
char format[kFormatMaxLength]
Definition table.h:89
Definition table.h:104
const Code & index_code() const
Definition table.h:126
RIME_DLL const table::Code * extra_code() const
Definition table.cc:77
RIME_DLL const table::Entry * entry() const
Definition table.cc:68
RIME_DLL size_t remaining() const
Definition table.cc:61
RIME_DLL bool exhausted() const
Definition table.cc:54
double credibility() const
Definition table.h:128
TableAccessor()=default
RIME_DLL bool Next()
Definition table.cc:95
double quality_len() const
Definition table.h:129
Code code() const
Definition table.cc:83
vector< double > quality_len_
Definition table.h:179
size_t level_
Definition table.h:176
double quality_len_sum() const
Definition table.h:170
Code index_code_
Definition table.h:177
double credibility_sum() const
Definition table.h:167
bool Advance(SyllableId syllable_id, double credibility=0.0, double quality_len=0.0, size_t last_pos=0)
Definition table.cc:102
bool Backdate()
Definition table.cc:117
void Reset()
Definition table.cc:130
vector< double > credibility_
Definition table.h:178
size_t last_pos() const
Definition table.h:173
TableAccessor Access(SyllableId syllable_id, double credibility=0.0, double quality_len=0.0) const
Definition table.cc:190
vector< size_t > last_pos_
Definition table.h:180
size_t level() const
Definition table.h:165
TableQuery(table::Index *index)
Definition table.h:147
RIME_DLL TableAccessor QueryPhrases(const Code &code)
Definition table.cc:555
bool GetSyllabary(Syllabary *syllabary)
Definition table.cc:535
RIME_DLL bool Build(const Syllabary &syllabary, const Vocabulary &vocabulary, size_t num_entries, uint32_t dict_file_checksum=0)
Definition table.cc:330
the< StringTable > string_table_
Definition table.h:241
RIME_DLL string GetSyllableById(int syllable_id)
Definition table.cc:543
table::Syllabary * syllabary_
Definition table.h:238
table::Metadata * metadata_
Definition table.h:237
RIME_DLL TableAccessor QueryWords(int syllable_id)
Definition table.cc:550
RIME_DLL bool Save()
Definition table.cc:315
RIME_DLL Table(const path &file_path)
Definition table.cc:265
RIME_DLL string GetEntryText(const table::Entry &entry)
Definition table.cc:632
table::Metadata * metadata() const
Definition table.h:213
uint32_t dict_file_checksum() const
Definition table.cc:326
RIME_DLL bool Query(const SyllableGraph &syll_graph, size_t start_pos, TableQueryResult *result)
Definition table.cc:571
RIME_DLL bool Load()
Definition table.cc:269
table::Index * index_
Definition table.h:239
the< StringTableBuilder > string_table_builder_
Definition table.h:242
Definition vocabulary.h:21
Definition vocabulary.h:37
Definition vocabulary.h:70
Definition vocabulary.h:100