librime 1.2
Rime Input Method Engine, the core library
dictionary.h
Go to the documentation of this file.
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>
16
17namespace rime {
18
19namespace dictionary {
20
21struct Chunk;
22struct QueryResult;
23
24} // namespace dictionary
25
27 public:
29 virtual ~DictEntryIterator() = default;
30 DictEntryIterator(const DictEntryIterator& other) = default;
34
35 void AddChunk(dictionary::Chunk&& chunk);
36 void Sort();
37 void AddFilter(DictEntryFilter filter) override;
39 bool Next();
40 bool Skip(size_t num_entries);
41 bool exhausted() const;
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
54using DictEntryCollector = map<size_t, DictEntryIterator>;
55
56class Config;
57class Schema;
59struct SyllableGraph;
60struct Ticket;
61
62class Dictionary : public Class<Dictionary, const Ticket&> {
63 public:
65 vector<string> packs,
66 vector<of<Table>> tables,
68 virtual ~Dictionary();
69
70 bool Exists() const;
71 RIME_DLL bool Remove();
72 RIME_DLL bool Load();
73
75 const SyllableGraph& syllable_graph,
76 size_t start_pos,
77 const hash_set<string>* blacklist = nullptr,
78 bool predict_word = false,
79 double initial_credibility = 0.0);
80 // if predictive is true, do an expand search with limit,
81 // otherwise do an exact match.
82 // return num of matching keys.
84 const string& str_code,
85 bool predictive,
86 size_t limit = 0,
87 const hash_set<string>* blacklist = nullptr);
88 // translate syllable id sequence to string code
89 RIME_DLL bool Decode(const Code& code, vector<string>* result);
90
91 const string& name() const { return name_; }
92 RIME_DLL bool loaded() const;
93
94 const vector<string>& packs() const { return packs_; }
95 const vector<of<Table>>& tables() const { return tables_; }
96 const an<Table>& primary_table() const { return tables_[0]; }
97 const an<Prism>& prism() const { return prism_; }
98
99 private:
100 string name_;
101 vector<string> packs_;
102 vector<of<Table>> tables_;
103 an<Prism> prism_;
104};
105
106class ResourceResolver;
107
108class DictionaryComponent : public Dictionary::Component {
109 public:
111 ~DictionaryComponent() override;
112 Dictionary* Create(const Ticket& ticket) override;
113 Dictionary* Create(string dict_name, string prism_name, vector<string> packs);
114
115 private:
116 map<string, weak<Prism>> prism_map_;
117 map<string, weak<Table>> table_map_;
118 the<ResourceResolver> prism_resource_resolver_;
119 the<ResourceResolver> table_resource_resolver_;
120};
121
122} // namespace rime
123
124#endif // RIME_DICTIONARY_H_
#define RIME_DLL
Definition rime_api.h:33
Definition algebra.cc:12
std::shared_ptr< T > an
Definition common.h:60
map< size_t, DictEntryIterator > DictEntryCollector
Definition dictionary.h:54
function< bool(an< DictEntry > entry)> DictEntryFilter
Definition vocabulary.h:82
std::unique_ptr< T > the
Definition common.h:58
boost::unordered_set< T > hash_set
Definition common.h:55
an< T > of
Definition common.h:62
Definition dictionary.cc:19
Definition syllabifier.h:39
Definition component.h:21
Definition config_component.h:21
Definition corrector.h:94
Definition dictionary.cc:21
Definition dictionary.cc:69
Definition dictionary.h:26
bool Skip(size_t num_entries)
Definition dictionary.cc:204
an< DictEntry > Peek()
Definition dictionary.cc:153
void AddFilter(DictEntryFilter filter) override
Definition dictionary.cc:143
bool Next()
Definition dictionary.cc:193
DictEntryIterator(DictEntryIterator &&other)=default
virtual ~DictEntryIterator()=default
void AddChunk(dictionary::Chunk &&chunk)
Definition dictionary.cc:130
DictEntryIterator(const DictEntryIterator &other)=default
DictEntryIterator & operator=(const DictEntryIterator &other)=default
DictEntryIterator()
Definition dictionary.cc:127
DictEntryIterator & operator=(DictEntryIterator &&other)=default
bool exhausted() const
Definition dictionary.cc:219
void Sort()
Definition dictionary.cc:135
size_t entry_count() const
Definition dictionary.h:42
Definition dictionary.h:62
const string & name() const
Definition dictionary.h:91
RIME_DLL Dictionary(string name, vector< string > packs, vector< of< Table > > tables, an< Prism > prism)
Definition dictionary.cc:225
const an< Table > & primary_table() const
Definition dictionary.h:96
RIME_DLL an< DictEntryCollector > Lookup(const SyllableGraph &syllable_graph, size_t start_pos, const hash_set< string > *blacklist=nullptr, bool predict_word=false, double initial_credibility=0.0)
Definition dictionary.cc:271
const an< Prism > & prism() const
Definition dictionary.h:97
RIME_DLL bool Remove()
Definition dictionary.cc:369
RIME_DLL bool loaded() const
Definition dictionary.cc:405
RIME_DLL size_t LookupWords(DictEntryIterator *result, const string &str_code, bool predictive, size_t limit=0, const hash_set< string > *blacklist=nullptr)
Definition dictionary.cc:299
bool Exists() const
Definition dictionary.cc:364
const vector< string > & packs() const
Definition dictionary.h:94
RIME_DLL bool Load()
Definition dictionary.cc:379
const vector< of< Table > > & tables() const
Definition dictionary.h:95
RIME_DLL bool Decode(const Code &code, vector< string > *result)
Definition dictionary.cc:351
Dictionary * Create(const Ticket &ticket) override
Definition dictionary.cc:425
DictionaryComponent()
Definition dictionary.cc:415
Definition vocabulary.h:21
Definition vocabulary.h:84
Definition schema.h:15
Definition ticket.h:17