librime: dictionary.h Source File

| 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>
15#include <rime/dict/vocabulary.h>
16
17namespace rime {
18
19namespace dictionary {
20
21struct Chunk;
22struct QueryResult;
23
24} // namespace dictionary
25
26 class RIME_DLL DictEntryIterator : public DictEntryFilterBinder {
27 public:
28 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 void AddFilter(DictEntryFilter filter) override;
38 an<DictEntry> Peek();
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
54 using DictEntryCollector = map<size_t, DictEntryIterator>;
55
56class Config;
57class Schema;
58class EditDistanceCorrector;
59struct SyllableGraph;
60struct Ticket;
61
62 class Dictionary : public Class<Dictionary, const Ticket&> {
63 public:
64 RIME_DLL 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_DLL bool Remove();
72 RIME_DLL bool Load();
73
74 RIME_DLL an<DictEntryCollector> Lookup(
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.
83 RIME_DLL size_t LookupWords(DictEntryIterator* result,
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
108 class DictionaryComponent : public Dictionary::Component {
109 public:
110 DictionaryComponent();
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_
common.h
component.h
prism.h
vocabulary.h
table.h
rime_api.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::DictEntryCollector
map< size_t, DictEntryIterator > DictEntryCollector
Definition dictionary.h:54
rime::DictEntryFilter
function< bool(an< DictEntry > entry)> DictEntryFilter
Definition vocabulary.h:82
rime::the
std::unique_ptr< T > the
Definition common.h:58
rime::hash_set
boost::unordered_set< T > hash_set
Definition common.h:55
rime::of
an< T > of
Definition common.h:62
rime::dictionary
Definition dictionary.cc:19
rime::SyllableGraph
Definition syllabifier.h:39
rime::Class
Definition component.h:21
rime::Config
Definition config_component.h:21
rime::EditDistanceCorrector
Definition corrector.h:94
rime::dictionary::Chunk
Definition dictionary.cc:21
rime::dictionary::QueryResult
Definition dictionary.cc:69
rime::DictEntryIterator
Definition dictionary.h:26
rime::DictEntryIterator::Skip
bool Skip(size_t num_entries)
Definition dictionary.cc:204
rime::DictEntryIterator::Peek
an< DictEntry > Peek()
Definition dictionary.cc:153
rime::DictEntryIterator::AddFilter
void AddFilter(DictEntryFilter filter) override
Definition dictionary.cc:143
rime::DictEntryIterator::Next
bool Next()
Definition dictionary.cc:193
rime::DictEntryIterator::DictEntryIterator
DictEntryIterator(DictEntryIterator &&other)=default
rime::DictEntryIterator::~DictEntryIterator
virtual ~DictEntryIterator()=default
rime::DictEntryIterator::AddChunk
void AddChunk(dictionary::Chunk &&chunk)
Definition dictionary.cc:130
rime::DictEntryIterator::DictEntryIterator
DictEntryIterator(const DictEntryIterator &other)=default
rime::DictEntryIterator::operator=
DictEntryIterator & operator=(const DictEntryIterator &other)=default
rime::DictEntryIterator::DictEntryIterator
DictEntryIterator()
Definition dictionary.cc:127
rime::DictEntryIterator::operator=
DictEntryIterator & operator=(DictEntryIterator &&other)=default
rime::DictEntryIterator::exhausted
bool exhausted() const
Definition dictionary.cc:219
rime::DictEntryIterator::Sort
void Sort()
Definition dictionary.cc:135
rime::DictEntryIterator::entry_count
size_t entry_count() const
Definition dictionary.h:42
rime::Dictionary
Definition dictionary.h:62
rime::Dictionary::name
const string & name() const
Definition dictionary.h:91
rime::Dictionary::Dictionary
RIME_DLL Dictionary(string name, vector< string > packs, vector< of< Table > > tables, an< Prism > prism)
Definition dictionary.cc:225
rime::Dictionary::primary_table
const an< Table > & primary_table() const
Definition dictionary.h:96
rime::Dictionary::Lookup
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
rime::Dictionary::prism
const an< Prism > & prism() const
Definition dictionary.h:97
rime::Dictionary::Remove
RIME_DLL bool Remove()
Definition dictionary.cc:369
rime::Dictionary::loaded
RIME_DLL bool loaded() const
Definition dictionary.cc:405
rime::Dictionary::LookupWords
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
rime::Dictionary::Exists
bool Exists() const
Definition dictionary.cc:364
rime::Dictionary::packs
const vector< string > & packs() const
Definition dictionary.h:94
rime::Dictionary::Load
RIME_DLL bool Load()
Definition dictionary.cc:379
rime::Dictionary::tables
const vector< of< Table > > & tables() const
Definition dictionary.h:95
rime::Dictionary::Decode
RIME_DLL bool Decode(const Code &code, vector< string > *result)
Definition dictionary.cc:351
rime::DictionaryComponent::Create
Dictionary * Create(const Ticket &ticket) override
Definition dictionary.cc:425
rime::DictionaryComponent::DictionaryComponent
DictionaryComponent()
Definition dictionary.cc:415
rime::Code
Definition vocabulary.h:21
rime::DictEntryFilterBinder
Definition vocabulary.h:84
rime::Schema
Definition schema.h:15
rime::Ticket
Definition ticket.h:17