librime: user_dictionary.h Source File

| librime 1.2 Rime Input Method Engine, the core library

user_dictionary.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-10-30 GONG Chen <chen.sst@gmail.com>
6//
7#ifndef RIME_USER_DICTIONARY_H_
8#define RIME_USER_DICTIONARY_H_
9
10#include <time.h>
11#include <rime/common.h>
12#include <rime/component.h>
13#include <rime/dict/user_db.h>
14#include <rime/dict/vocabulary.h>
15
16namespace rime {
17
18 class UserDictEntryIterator : public DictEntryFilterBinder {
19 public:
20 UserDictEntryIterator() = default;
21
22 void Add(an<DictEntry>&& entry);
23 void SetEntries(DictEntryList&& entries);
24 void SortRange(size_t start, size_t count);
25
26 void AddFilter(DictEntryFilter filter) override;
27 an<DictEntry> Peek();
28 bool Next();
29 bool exhausted() const { return index_ >= cache_.size(); }
30 size_t cache_size() const { return cache_.size(); }
31
32 protected:
33 bool FindNextEntry();
34
35 DictEntryList cache_;
36 size_t index_ = 0;
37};
38
39 using UserDictEntryCollector = map<size_t, UserDictEntryIterator>;
40
41class Schema;
42class Table;
43class Prism;
44class Db;
45struct SyllableGraph;
46struct DfsState;
47struct Ticket;
48
49 class UserDictionary : public Class<UserDictionary, const Ticket&> {
50 public:
51 UserDictionary(const string& name, an<Db> db);
52 virtual ~UserDictionary();
53
54 void Attach(const an<Table>& table, const an<Prism>& prism);
55 bool Load();
56 bool loaded() const;
57 bool readonly() const;
58
59 an<UserDictEntryCollector> Lookup(const SyllableGraph& syllable_graph,
60 size_t start_pos,
61 size_t depth_limit = 0,
62 size_t predict_word_from_depth = 0,
63 double initial_credibility = 0.0);
64 size_t LookupWords(UserDictEntryIterator* result,
65 const string& input,
66 bool predictive,
67 size_t limit = 0,
68 string* resume_key = NULL);
69 bool UpdateEntry(const DictEntry& entry, int commits);
70 bool UpdateEntry(const DictEntry& entry,
71 int commits,
72 const string& new_entry_prefix);
73 bool UpdateTickCount(TickCount increment);
74
75 bool NewTransaction();
76 bool RevertRecentTransaction();
77 bool CommitPendingTransaction();
78
79 const string& name() const { return name_; }
80 TickCount tick() const { return tick_; }
81
82 static an<DictEntry> CreateDictEntry(const string& key,
83 const string& value,
84 TickCount present_tick,
85 double credibility = 0.0,
86 double quality_len = 0.0,
87 string* full_code = nullptr);
88
89 protected:
90 bool Initialize();
91 bool FetchTickCount();
92 bool TranslateCodeToString(const Code& code, string* result);
93 void DfsLookup(const SyllableGraph& syll_graph,
94 size_t current_pos,
95 const string& current_prefix,
96 DfsState* state);
97
98 private:
99 string name_;
100 an<Db> db_;
101 an<Table> table_;
102 an<Prism> prism_;
103 hash_map<string, SyllableId> syllabary_;
104 hash_map<SyllableId, string> rev_syllabary_;
105 TickCount tick_ = 0;
106 time_t transaction_time_ = 0;
107};
108
109 class UserDictionaryComponent : public UserDictionary::Component {
110 public:
111 UserDictionaryComponent();
112 UserDictionary* Create(const Ticket& ticket);
113 UserDictionary* Create(const string& dict_name, const string& db_class);
114
115 private:
116 hash_map<string, weak<Db>> db_pool_;
117};
118
119} // namespace rime
120
121#endif // RIME_USER_DICTIONARY_H_
common.h
component.h
user_db.h
vocabulary.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::DictEntryFilter
function< bool(an< DictEntry > entry)> DictEntryFilter
Definition vocabulary.h:82
rime::hash_map
boost::unordered_map< Key, T > hash_map
Definition common.h:53
rime::TickCount
uint64_t TickCount
Definition user_db.h:17
rime::UserDictEntryCollector
map< size_t, UserDictEntryIterator > UserDictEntryCollector
Definition user_dictionary.h:39
rime::prism
Definition prism.h:20
rime::table
Definition table.h:36
rime::SyllableGraph
Definition syllabifier.h:39
rime::Class
Definition component.h:21
rime::Db
Definition db.h:33
rime::Prism
Definition prism.h:67
rime::Table
Definition table.h:191
rime::DfsState
Definition user_dictionary.cc:27
rime::UserDictEntryIterator
Definition user_dictionary.h:18
rime::UserDictEntryIterator::SetEntries
void SetEntries(DictEntryList &&entries)
Definition user_dictionary.cc:109
rime::UserDictEntryIterator::Add
void Add(an< DictEntry > &&entry)
Definition user_dictionary.cc:105
rime::UserDictEntryIterator::cache_
DictEntryList cache_
Definition user_dictionary.h:35
rime::UserDictEntryIterator::AddFilter
void AddFilter(DictEntryFilter filter) override
Definition user_dictionary.cc:117
rime::UserDictEntryIterator::cache_size
size_t cache_size() const
Definition user_dictionary.h:30
rime::UserDictEntryIterator::exhausted
bool exhausted() const
Definition user_dictionary.h:29
rime::UserDictEntryIterator::SortRange
void SortRange(size_t start, size_t count)
Definition user_dictionary.cc:113
rime::UserDictEntryIterator::Peek
an< DictEntry > Peek()
Definition user_dictionary.cc:126
rime::UserDictEntryIterator::UserDictEntryIterator
UserDictEntryIterator()=default
rime::UserDictEntryIterator::FindNextEntry
bool FindNextEntry()
Definition user_dictionary.cc:133
rime::UserDictEntryIterator::Next
bool Next()
Definition user_dictionary.cc:141
rime::UserDictEntryIterator::index_
size_t index_
Definition user_dictionary.h:36
rime::UserDictionary
Definition user_dictionary.h:49
rime::UserDictionary::Initialize
bool Initialize()
Definition user_dictionary.cc:468
rime::UserDictionary::NewTransaction
bool NewTransaction()
Definition user_dictionary.cc:486
rime::UserDictionary::CommitPendingTransaction
bool CommitPendingTransaction()
Definition user_dictionary.cc:504
rime::UserDictionary::Attach
void Attach(const an< Table > &table, const an< Prism > &prism)
Definition user_dictionary.cc:164
rime::UserDictionary::UserDictionary
UserDictionary(const string &name, an< Db > db)
Definition user_dictionary.cc:155
rime::UserDictionary::name
const string & name() const
Definition user_dictionary.h:79
rime::UserDictionary::TranslateCodeToString
bool TranslateCodeToString(const Code &code, string *result)
Definition user_dictionary.cc:512
rime::UserDictionary::Lookup
an< UserDictEntryCollector > Lookup(const SyllableGraph &syllable_graph, size_t start_pos, size_t depth_limit=0, size_t predict_word_from_depth=0, double initial_credibility=0.0)
Definition user_dictionary.cc:314
rime::UserDictionary::FetchTickCount
bool FetchTickCount()
Definition user_dictionary.cc:472
rime::UserDictionary::loaded
bool loaded() const
Definition user_dictionary.cc:185
rime::UserDictionary::Load
bool Load()
Definition user_dictionary.cc:169
rime::UserDictionary::UpdateTickCount
bool UpdateTickCount(TickCount increment)
Definition user_dictionary.cc:459
rime::UserDictionary::RevertRecentTransaction
bool RevertRecentTransaction()
Definition user_dictionary.cc:495
rime::UserDictionary::tick
TickCount tick() const
Definition user_dictionary.h:80
rime::UserDictionary::UpdateEntry
bool UpdateEntry(const DictEntry &entry, int commits)
Definition user_dictionary.cc:421
rime::UserDictionary::LookupWords
size_t LookupWords(UserDictEntryIterator *result, const string &input, bool predictive, size_t limit=0, string *resume_key=NULL)
Definition user_dictionary.cc:358
rime::UserDictionary::DfsLookup
void DfsLookup(const SyllableGraph &syll_graph, size_t current_pos, const string &current_prefix, DfsState *state)
Definition user_dictionary.cc:216
rime::UserDictionary::CreateDictEntry
static an< DictEntry > CreateDictEntry(const string &key, const string &value, TickCount present_tick, double credibility=0.0, double quality_len=0.0, string *full_code=nullptr)
Definition user_dictionary.cc:532
rime::UserDictionary::readonly
bool readonly() const
Definition user_dictionary.cc:189
rime::UserDictionaryComponent::Create
UserDictionary * Create(const Ticket &ticket)
Definition user_dictionary.cc:588
rime::UserDictionaryComponent::UserDictionaryComponent
UserDictionaryComponent()
Definition user_dictionary.cc:571
rime::Code
Definition vocabulary.h:21
rime::DictEntry
Definition vocabulary.h:46
rime::DictEntryList
Definition vocabulary.h:76
rime::DictEntryFilterBinder
Definition vocabulary.h:84
rime::Schema
Definition schema.h:15
rime::Ticket
Definition ticket.h:17