1 //
2 // Copyright RIME Developers
3 // Distributed under the BSD License
4 //
5 // 2011-07-05 GONG Chen <chen.sst@gmail.com>
6 //
7 #ifndef RIME_DICTIONARY_H_
8 #define RIME_DICTIONARY_H_
9 
10 #include <rime_api.h>
11 #include <rime/common.h>
12 #include <rime/component.h>
13 #include <rime/dict/prism.h>
14 #include <rime/dict/table.h>
15 #include <rime/dict/vocabulary.h>
16 
17 namespace rime {
18 
19 namespace dictionary {
20 
21 struct Chunk;
22 struct QueryResult;
23 
24 }  // namespace dictionary
25 
26 class DictEntryIterator : public DictEntryFilterBinder {
27  public:
28   RIME_API DictEntryIterator();
29   virtual ~DictEntryIterator() = default;
30   DictEntryIterator(const DictEntryIterator& other) = default;
31   DictEntryIterator& operator= (const DictEntryIterator& other) = default;
32   DictEntryIterator(DictEntryIterator&& other) = default;
33   DictEntryIterator& operator= (DictEntryIterator&& other) = default;
34 
35   void AddChunk(dictionary::Chunk&& chunk);
36   void Sort();
37   RIME_API void AddFilter(DictEntryFilter filter) override;
38   RIME_API an<DictEntry> Peek();
39   RIME_API bool Next();
40   bool Skip(size_t num_entries);
41   RIME_API bool exhausted() const;
entry_count()42   size_t entry_count() const { return entry_count_; }
43 
44  protected:
45   bool FindNextEntry();
46 
47  private:
48   an<dictionary::QueryResult> query_result_;
49   size_t chunk_index_ = 0;
50   an<DictEntry> entry_ = nullptr;
51   size_t entry_count_ = 0;
52 };
53 
54 using DictEntryCollector = map<size_t, DictEntryIterator>;
55 
56 class Config;
57 class Schema;
58 class EditDistanceCorrector;
59 struct SyllableGraph;
60 struct Ticket;
61 
62 class Dictionary : public Class<Dictionary, const Ticket&> {
63  public:
64   RIME_API Dictionary(string name,
65                       vector<string> packs,
66                       vector<of<Table>> tables,
67                       an<Prism> prism);
68   virtual ~Dictionary();
69 
70   bool Exists() const;
71   RIME_API bool Remove();
72   RIME_API bool Load();
73 
74   RIME_API an<DictEntryCollector> Lookup(const SyllableGraph& syllable_graph,
75                                          size_t start_pos,
76                                          double initial_credibility = 0.0);
77   // if predictive is true, do an expand search with limit,
78   // otherwise do an exact match.
79   // return num of matching keys.
80   RIME_API size_t LookupWords(DictEntryIterator* result,
81                               const string& str_code,
82                               bool predictive, size_t limit = 0);
83   // translate syllable id sequence to string code
84   RIME_API bool Decode(const Code& code, vector<string>* result);
85 
name()86   const string& name() const { return name_; }
87   RIME_API bool loaded() const;
88 
packs()89   const vector<string>& packs() const { return packs_; }
tables()90   const vector<of<Table>>& tables() const { return tables_; }
primary_table()91   const an<Table>& primary_table() const { return tables_[0]; }
prism()92   const an<Prism>& prism() const { return prism_; }
93 
94  private:
95   string name_;
96   vector<string> packs_;
97   vector<of<Table>> tables_;
98   an<Prism> prism_;
99 };
100 
101 class ResourceResolver;
102 
103 class DictionaryComponent : public Dictionary::Component {
104  public:
105   DictionaryComponent();
106   ~DictionaryComponent() override;
107   Dictionary* Create(const Ticket& ticket) override;
108   Dictionary* Create(string dict_name,
109                      string prism_name,
110                      vector<string> packs);
111 
112  private:
113   map<string, weak<Prism>> prism_map_;
114   map<string, weak<Table>> table_map_;
115   the<ResourceResolver> prism_resource_resolver_;
116   the<ResourceResolver> table_resource_resolver_;
117 };
118 
119 }  // namespace rime
120 
121 #endif  // RIME_DICTIONARY_H_
122