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 
19 namespace 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;
Count()40   size_t Count() const {
41     return vertices_.empty() ? 0 : vertices_.size() - 1;
42   }
start()43   size_t start() const {
44     return vertices_.empty() ? 0 : vertices_.front();
45   }
end()46   size_t end() const {
47     return vertices_.empty() ? 0 : vertices_.back();
48   }
49   bool HasVertex(size_t vertex) const;
set_vertices(vector<size_t> && vertices)50   void set_vertices(vector<size_t>&& vertices) {
51     vertices_ = vertices;
52   }
53 
54  private:
55   vector<size_t> vertices_;
56 };
57 
58 class Phrase;
59 
60 class PhraseSyllabifier {
61  public:
62   virtual ~PhraseSyllabifier() = default;
63 
64   virtual Spans Syllabify(const Phrase* phrase) = 0;
65 };
66 
67 //
68 
69 class Language;
70 
71 class Phrase : public Candidate {
72  public:
Phrase(const Language * language,const string & type,size_t start,size_t end,const an<DictEntry> & entry)73   Phrase(const Language* language,
74          const string& type,
75          size_t start,
76          size_t end,
77          const an<DictEntry>& entry)
78       : Candidate(type, start, end),
79         language_(language),
80         entry_(entry) {
81   }
text()82   const string& text() const { return entry_->text; }
comment()83   string comment() const { return entry_->comment; }
preedit()84   string preedit() const { return entry_->preedit; }
set_comment(const string & comment)85   void set_comment(const string& comment) {
86     entry_->comment = comment;
87   }
set_preedit(const string & preedit)88   void set_preedit(const string& preedit) {
89     entry_->preedit = preedit;
90   }
set_syllabifier(an<PhraseSyllabifier> syllabifier)91   void set_syllabifier(an<PhraseSyllabifier> syllabifier) {
92     syllabifier_ = syllabifier;
93   }
weight()94   double weight() const { return entry_->weight; }
set_weight(double weight)95   void set_weight(double weight) { entry_->weight = weight; }
code()96   Code& code() const { return entry_->code; }
entry()97   const DictEntry& entry() const { return *entry_; }
language()98   const Language* language() const { return language_; }
spans()99   Spans spans() {
100     return syllabifier_ ? syllabifier_->Syllabify(this)
101                         : Spans();
102   }
103 
104  protected:
105   const Language* language_;
106   an<DictEntry> entry_;
107   an<PhraseSyllabifier> syllabifier_;
108 };
109 
110 //
111 
112 class Sentence : public Phrase {
113  public:
Sentence(const Language * language)114   Sentence(const Language* language)
115       : Phrase(language, "sentence", 0, 0, New<DictEntry>()) {}
Sentence(const Sentence & other)116   Sentence(const Sentence& other)
117       : Phrase(other),
118         components_(other.components_),
119         word_lengths_(other.word_lengths_) {
120     entry_ = New<DictEntry>(other.entry());
121   }
122   void Extend(const DictEntry& another, size_t end_pos, double new_weight);
123   void Offset(size_t offset);
124 
empty()125   bool empty() const {
126     return components_.empty();
127   }
128 
size()129   size_t size() const {
130     return components_.size();
131   }
132 
components()133   const vector<DictEntry>& components() const {
134     return components_;
135   }
word_lengths()136   const vector<size_t>& word_lengths() const {
137     return word_lengths_;
138   }
139 
140  protected:
141   vector<DictEntry> components_;
142   vector<size_t> word_lengths_;
143 };
144 
145 //
146 
147 struct Ticket;
148 
149 class TranslatorOptions {
150  public:
151   TranslatorOptions(const Ticket& ticket);
152   bool IsUserDictDisabledFor(const string& input) const;
153 
delimiters()154   const string& delimiters() const { return delimiters_; }
tag()155   const string& tag() const { return tag_; }
set_tag(const string & tag)156   void set_tag(const string& tag) { tag_ = tag; }
contextual_suggestions()157   bool contextual_suggestions() const { return contextual_suggestions_; }
set_contextual_suggestions(bool enabled)158   void set_contextual_suggestions(bool enabled) {
159     contextual_suggestions_ = enabled;
160   }
enable_completion()161   bool enable_completion() const { return enable_completion_; }
set_enable_completion(bool enabled)162   void set_enable_completion(bool enabled) { enable_completion_ = enabled; }
strict_spelling()163   bool strict_spelling() const { return strict_spelling_; }
set_strict_spelling(bool is_strict)164   void set_strict_spelling(bool is_strict) { strict_spelling_ = is_strict; }
initial_quality()165   double initial_quality() const { return initial_quality_; }
set_initial_quality(double quality)166   void set_initial_quality(double quality) { initial_quality_ = quality; }
preedit_formatter()167   Projection& preedit_formatter() { return preedit_formatter_; }
comment_formatter()168   Projection& comment_formatter() { return comment_formatter_; }
169 
170  protected:
171   string delimiters_;
172   string tag_ = "abc";
173   bool contextual_suggestions_ = false;
174   bool enable_completion_ = true;
175   bool strict_spelling_ = false;
176   double initial_quality_ = 0.;
177   Projection preedit_formatter_;
178   Projection comment_formatter_;
179   Patterns user_dict_disabling_patterns_;
180 };
181 
182 }  // namespace rime
183 
184 #endif  // RIME_TRANSLATOR_COMMONS_H_
185