1 //
2 // Copyright RIME Developers
3 // Distributed under the BSD License
4 //
5 // 2011-07-10 GONG Chen <chen.sst@gmail.com>
6 //
7 
8 #ifndef RIME_VOCABULARY_H_
9 #define RIME_VOCABULARY_H_
10 
11 #include <stdint.h>
12 #include <rime_api.h>
13 #include <rime/common.h>
14 
15 namespace rime {
16 
17 using Syllabary = set<string>;
18 
19 using SyllableId = int32_t;
20 
21 class Code : public vector<SyllableId> {
22  public:
23   static const size_t kIndexCodeMaxLength = 3;
24 
25   bool operator< (const Code& other) const;
26   bool operator== (const Code& other) const;
27 
28   void CreateIndex(Code* index_code);
29 
30   string ToString() const;
31 };
32 
33 struct DictEntry {
34   string text;
35   string comment;
36   string preedit;
37   double weight = 0.0;
38   int commit_count = 0;
39   Code code;  // multi-syllable code from prism
40   string custom_code;  // user defined code
41   int remaining_code_length = 0;
42 
43   DictEntry() = default;
44   bool operator< (const DictEntry& other) const;
45 };
46 
47 class DictEntryList : public vector<of<DictEntry>> {
48  public:
49   void Sort();
50   void SortRange(size_t start, size_t count);
51 };
52 
53 using DictEntryFilter = function<bool (an<DictEntry> entry)>;
54 
55 class DictEntryFilterBinder {
56  public:
57   virtual ~DictEntryFilterBinder() = default;
58   RIME_API virtual void AddFilter(DictEntryFilter filter);
59 
60  protected:
61   DictEntryFilter filter_;
62 };
63 
64 class Vocabulary;
65 
66 struct VocabularyPage {
67   DictEntryList entries;
68   an<Vocabulary> next_level;
69 };
70 
71 class Vocabulary : public map<int, VocabularyPage> {
72  public:
73   DictEntryList* LocateEntries(const Code& code);
74   void SortHomophones();
75 };
76 
77 // word -> { code, ... }
78 using ReverseLookupTable = map<string, set<string>>;
79 
80 }  // namespace rime
81 
82 #endif  // RIME_VOCABULARY_H_
83