librime: translator_commons.h Source File

| librime 1.2 Rime Input Method Engine, the core library

translator_commons.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2012-04-22 GONG Chen <chen.sst@gmail.com>
6//
7#ifndef RIME_TRANSLATOR_COMMONS_H_
8#define RIME_TRANSLATOR_COMMONS_H_
9
10#include <boost/regex.hpp>
11#include <rime/common.h>
12#include <rime/config.h>
13#include <rime/candidate.h>
14#include <rime/translation.h>
15#include <rime/algo/algebra.h>
16#include <rime/algo/syllabifier.h>
17#include <rime/dict/vocabulary.h>
18
19namespace rime {
20
21//
22
23 class Patterns : public vector<boost::regex> {
24 public:
25 bool Load(an<ConfigList> patterns);
26};
27
28//
29
30 class Spans {
31 public:
32 void AddVertex(size_t vertex);
33 void AddSpan(size_t start, size_t end);
34 void AddSpans(const Spans& spans);
35 void Clear();
36 // move by syllable by returning a value different from caret_pos
37 size_t PreviousStop(size_t caret_pos) const;
38 size_t NextStop(size_t caret_pos) const;
39 size_t Count(size_t start_pos, size_t end_pos) const;
40 size_t Count() const { return vertices_.empty() ? 0 : vertices_.size() - 1; }
41 size_t start() const { return vertices_.empty() ? 0 : vertices_.front(); }
42 size_t end() const { return vertices_.empty() ? 0 : vertices_.back(); }
43 bool HasVertex(size_t vertex) const;
44 void set_vertices(vector<size_t>&& vertices) { vertices_ = vertices; }
45
46 private:
47 vector<size_t> vertices_;
48};
49
50class Phrase;
51
52 class PhraseSyllabifier {
53 public:
54 virtual ~PhraseSyllabifier() = default;
55
56 virtual Spans Syllabify(const Phrase* phrase) = 0;
57};
58
59//
60
61class Language;
62
63 class Phrase : public Candidate {
64 public:
65 Phrase(const Language* language,
66 const string& type,
67 size_t start,
68 size_t end,
69 const an<DictEntry>& entry)
70 : Candidate(type, start, end), language_(language), entry_(entry) {}
71 const string& text() const { return entry_->text; }
72 string comment() const { return entry_->comment; }
73 string preedit() const { return entry_->preedit; }
74 void set_comment(const string& comment) { entry_->comment = comment; }
75 void set_preedit(const string& preedit) { entry_->preedit = preedit; }
76 void set_syllabifier(an<PhraseSyllabifier> syllabifier) {
77 syllabifier_ = syllabifier;
78 }
79 double weight() const { return entry_->weight; }
80 void set_weight(double weight) { entry_->weight = weight; }
81 Code& code() const { return entry_->code; }
82 const DictEntry& entry() const { return *entry_; }
83 const Language* language() const { return language_; }
84 size_t matching_code_size() const {
85 return entry_->matching_code_size != 0 ? entry_->matching_code_size
86 : entry_->code.size();
87 }
88 bool is_exact_match() const {
89 return entry_->matching_code_size == 0 ||
90 entry_->matching_code_size == entry_->code.size();
91 }
92 bool is_predicitve_match() const {
93 return entry_->matching_code_size != 0 &&
94 entry_->matching_code_size < entry_->code.size();
95 }
96 Code matching_code() const {
97 return entry_->matching_code_size == 0
98 ? entry_->code
99 : Code(entry_->code.begin(),
100 entry_->code.begin() + entry_->matching_code_size);
101 }
102 Spans spans() {
103 return syllabifier_ ? syllabifier_->Syllabify(this) : Spans();
104 }
105
106 protected:
107 const Language* language_;
108 an<DictEntry> entry_;
109 an<PhraseSyllabifier> syllabifier_;
110};
111
112//
113
114 class Sentence : public Phrase {
115 public:
116 Sentence(const Language* language)
117 : Phrase(language, "sentence", 0, 0, New<DictEntry>()) {}
118 Sentence(const Sentence& other)
119 : Phrase(other),
120 components_(other.components_),
121 word_lengths_(other.word_lengths_) {
122 entry_ = New<DictEntry>(other.entry());
123 }
124 void Extend(const DictEntry& another, size_t end_pos, double new_weight);
125 void Offset(size_t offset);
126
127 bool empty() const { return components_.empty(); }
128
129 size_t size() const { return components_.size(); }
130
131 const vector<DictEntry>& components() const { return components_; }
132 const vector<size_t>& word_lengths() const { return word_lengths_; }
133
134 protected:
135 vector<DictEntry> components_;
136 vector<size_t> word_lengths_;
137};
138
139//
140
141struct Ticket;
142
143 class TranslatorOptions {
144 public:
145 TranslatorOptions(const Ticket& ticket);
146 bool IsUserDictDisabledFor(const string& input) const;
147
148 const string& delimiters() const { return delimiters_; }
149 vector<string> tags() const { return tags_; }
150 void set_tags(const vector<string>& tags) {
151 tags_ = tags;
152 if (tags_.size() == 0) {
153 tags_.push_back("abc");
154 }
155 }
156 const string& tag() const { return tags_[0]; }
157 void set_tag(const string& tag) { tags_[0] = tag; }
158 bool contextual_suggestions() const { return contextual_suggestions_; }
159 void set_contextual_suggestions(bool enabled) {
160 contextual_suggestions_ = enabled;
161 }
162 bool enable_completion() const { return enable_completion_; }
163 void set_enable_completion(bool enabled) { enable_completion_ = enabled; }
164 bool strict_spelling() const { return strict_spelling_; }
165 void set_strict_spelling(bool is_strict) { strict_spelling_ = is_strict; }
166 double initial_quality() const { return initial_quality_; }
167 void set_initial_quality(double quality) { initial_quality_ = quality; }
168 Projection& preedit_formatter() { return preedit_formatter_; }
169 Projection& comment_formatter() { return comment_formatter_; }
170 const hash_set<string>& blacklist() { return blacklist_; }
171
172 protected:
173 string delimiters_;
174 vector<string> tags_{"abc"}; // invariant: non-empty
175 bool contextual_suggestions_ = false;
176 bool enable_completion_ = true;
177 bool strict_spelling_ = false;
178 double initial_quality_ = 0.;
179 int max_sentences_ = 1;
180 double sentence_cutoff_threshold_ = 0.1;
181 Projection preedit_formatter_;
182 Projection comment_formatter_;
183 Patterns user_dict_disabling_patterns_;
184 hash_set<string> blacklist_;
185};
186
187} // namespace rime
188
189#endif // RIME_TRANSLATOR_COMMONS_H_
algebra.h
syllabifier.h
translation.h
common.h
config.h
candidate.h
vocabulary.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::hash_set
boost::unordered_set< T > hash_set
Definition common.h:55
rime::New
an< T > New(Args &&... args)
Definition common.h:77
rime::Projection
Definition algebra.h:29
rime::Candidate::Candidate
Candidate()=default
rime::Candidate::type
const string & type() const
Definition candidate.h:26
rime::Candidate::start
size_t start() const
Definition candidate.h:28
rime::Candidate::end
size_t end() const
Definition candidate.h:29
rime::Code
Definition vocabulary.h:21
rime::Code::Code
Code()=default
rime::DictEntry
Definition vocabulary.h:46
rime::Patterns
Definition translator_commons.h:23
rime::Patterns::Load
bool Load(an< ConfigList > patterns)
Definition translator_commons.cc:19
rime::Spans
Definition translator_commons.h:30
rime::Spans::start
size_t start() const
Definition translator_commons.h:41
rime::Spans::set_vertices
void set_vertices(vector< size_t > &&vertices)
Definition translator_commons.h:44
rime::Spans::NextStop
size_t NextStop(size_t caret_pos) const
Definition translator_commons.cc:67
rime::Spans::Clear
void Clear()
Definition translator_commons.cc:55
rime::Spans::HasVertex
bool HasVertex(size_t vertex) const
Definition translator_commons.cc:88
rime::Spans::AddVertex
void AddVertex(size_t vertex)
Definition translator_commons.cc:33
rime::Spans::end
size_t end() const
Definition translator_commons.h:42
rime::Spans::PreviousStop
size_t PreviousStop(size_t caret_pos) const
Definition translator_commons.cc:59
rime::Spans::Count
size_t Count(size_t start_pos, size_t end_pos) const
Definition translator_commons.cc:75
rime::Spans::AddSpans
void AddSpans(const Spans &spans)
Definition translator_commons.cc:49
rime::Spans::Count
size_t Count() const
Definition translator_commons.h:40
rime::Spans::AddSpan
void AddSpan(size_t start, size_t end)
Definition translator_commons.cc:44
rime::PhraseSyllabifier
Definition translator_commons.h:52
rime::PhraseSyllabifier::Syllabify
virtual Spans Syllabify(const Phrase *phrase)=0
rime::PhraseSyllabifier::~PhraseSyllabifier
virtual ~PhraseSyllabifier()=default
rime::Phrase
Definition translator_commons.h:63
rime::Phrase::set_syllabifier
void set_syllabifier(an< PhraseSyllabifier > syllabifier)
Definition translator_commons.h:76
rime::Phrase::syllabifier_
an< PhraseSyllabifier > syllabifier_
Definition translator_commons.h:109
rime::Phrase::entry
const DictEntry & entry() const
Definition translator_commons.h:82
rime::Phrase::spans
Spans spans()
Definition translator_commons.h:102
rime::Phrase::comment
string comment() const
Definition translator_commons.h:72
rime::Phrase::text
const string & text() const
Definition translator_commons.h:71
rime::Phrase::is_exact_match
bool is_exact_match() const
Definition translator_commons.h:88
rime::Phrase::preedit
string preedit() const
Definition translator_commons.h:73
rime::Phrase::code
Code & code() const
Definition translator_commons.h:81
rime::Phrase::Phrase
Phrase(const Language *language, const string &type, size_t start, size_t end, const an< DictEntry > &entry)
Definition translator_commons.h:65
rime::Phrase::matching_code
Code matching_code() const
Definition translator_commons.h:96
rime::Phrase::matching_code_size
size_t matching_code_size() const
Definition translator_commons.h:84
rime::Phrase::weight
double weight() const
Definition translator_commons.h:79
rime::Phrase::is_predicitve_match
bool is_predicitve_match() const
Definition translator_commons.h:92
rime::Phrase::language_
const Language * language_
Definition translator_commons.h:107
rime::Phrase::set_preedit
void set_preedit(const string &preedit)
Definition translator_commons.h:75
rime::Phrase::entry_
an< DictEntry > entry_
Definition translator_commons.h:108
rime::Phrase::set_weight
void set_weight(double weight)
Definition translator_commons.h:80
rime::Phrase::language
const Language * language() const
Definition translator_commons.h:83
rime::Phrase::set_comment
void set_comment(const string &comment)
Definition translator_commons.h:74
rime::Sentence::empty
bool empty() const
Definition translator_commons.h:127
rime::Sentence::Sentence
Sentence(const Sentence &other)
Definition translator_commons.h:118
rime::Sentence::Sentence
Sentence(const Language *language)
Definition translator_commons.h:116
rime::Sentence::word_lengths
const vector< size_t > & word_lengths() const
Definition translator_commons.h:132
rime::Sentence::Offset
void Offset(size_t offset)
Definition translator_commons.cc:108
rime::Sentence::components
const vector< DictEntry > & components() const
Definition translator_commons.h:131
rime::Sentence::word_lengths_
vector< size_t > word_lengths_
Definition translator_commons.h:136
rime::Sentence::Extend
void Extend(const DictEntry &another, size_t end_pos, double new_weight)
Definition translator_commons.cc:94
rime::Sentence::size
size_t size() const
Definition translator_commons.h:129
rime::Sentence::components_
vector< DictEntry > components_
Definition translator_commons.h:135
rime::TranslatorOptions::set_strict_spelling
void set_strict_spelling(bool is_strict)
Definition translator_commons.h:165
rime::TranslatorOptions::delimiters
const string & delimiters() const
Definition translator_commons.h:148
rime::TranslatorOptions::enable_completion
bool enable_completion() const
Definition translator_commons.h:162
rime::TranslatorOptions::tags
vector< string > tags() const
Definition translator_commons.h:149
rime::TranslatorOptions::initial_quality_
double initial_quality_
Definition translator_commons.h:178
rime::TranslatorOptions::strict_spelling
bool strict_spelling() const
Definition translator_commons.h:164
rime::TranslatorOptions::delimiters_
string delimiters_
Definition translator_commons.h:173
rime::TranslatorOptions::set_initial_quality
void set_initial_quality(double quality)
Definition translator_commons.h:167
rime::TranslatorOptions::strict_spelling_
bool strict_spelling_
Definition translator_commons.h:177
rime::TranslatorOptions::set_tag
void set_tag(const string &tag)
Definition translator_commons.h:157
rime::TranslatorOptions::blacklist
const hash_set< string > & blacklist()
Definition translator_commons.h:170
rime::TranslatorOptions::comment_formatter_
Projection comment_formatter_
Definition translator_commons.h:182
rime::TranslatorOptions::tag
const string & tag() const
Definition translator_commons.h:156
rime::TranslatorOptions::TranslatorOptions
TranslatorOptions(const Ticket &ticket)
Definition translator_commons.cc:115
rime::TranslatorOptions::set_tags
void set_tags(const vector< string > &tags)
Definition translator_commons.h:150
rime::TranslatorOptions::contextual_suggestions
bool contextual_suggestions() const
Definition translator_commons.h:158
rime::TranslatorOptions::enable_completion_
bool enable_completion_
Definition translator_commons.h:176
rime::TranslatorOptions::sentence_cutoff_threshold_
double sentence_cutoff_threshold_
Definition translator_commons.h:180
rime::TranslatorOptions::contextual_suggestions_
bool contextual_suggestions_
Definition translator_commons.h:175
rime::TranslatorOptions::preedit_formatter
Projection & preedit_formatter()
Definition translator_commons.h:168
rime::TranslatorOptions::initial_quality
double initial_quality() const
Definition translator_commons.h:166
rime::TranslatorOptions::user_dict_disabling_patterns_
Patterns user_dict_disabling_patterns_
Definition translator_commons.h:183
rime::TranslatorOptions::IsUserDictDisabledFor
bool IsUserDictDisabledFor(const string &input) const
Definition translator_commons.cc:170
rime::TranslatorOptions::set_enable_completion
void set_enable_completion(bool enabled)
Definition translator_commons.h:163
rime::TranslatorOptions::set_contextual_suggestions
void set_contextual_suggestions(bool enabled)
Definition translator_commons.h:159
rime::TranslatorOptions::tags_
vector< string > tags_
Definition translator_commons.h:174
rime::TranslatorOptions::max_sentences_
int max_sentences_
Definition translator_commons.h:179
rime::TranslatorOptions::blacklist_
hash_set< string > blacklist_
Definition translator_commons.h:184
rime::TranslatorOptions::preedit_formatter_
Projection preedit_formatter_
Definition translator_commons.h:181
rime::TranslatorOptions::comment_formatter
Projection & comment_formatter()
Definition translator_commons.h:169
rime::Language
Definition language.h:12
rime::Ticket
Definition ticket.h:17