librime: encoder.h Source File

| librime 1.2 Rime Input Method Engine, the core library

encoder.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2013-07-17 GONG Chen <chen.sst@gmail.com>
6//
7
8#ifndef RIME_ENCODER_H_
9#define RIME_ENCODER_H_
10
11#include <boost/regex.hpp>
12#include <rime_api.h>
13
14namespace rime {
15
16 class RawCode : public vector<string> {
17 public:
18 RIME_DLL string ToString() const;
19 RIME_DLL void FromString(const string& code_str);
20};
21
22 class PhraseCollector {
23 public:
24 PhraseCollector() = default;
25 virtual ~PhraseCollector() = default;
26
27 virtual void CreateEntry(const string& phrase,
28 const string& code_str,
29 const string& value) = 0;
30 // return a list of alternative code for the given word
31 virtual bool TranslateWord(const string& word, vector<string>* code) = 0;
32};
33
34class Config;
35
36 class Encoder {
37 public:
38 Encoder(PhraseCollector* collector) : collector_(collector) {}
39 virtual ~Encoder() = default;
40
41 virtual bool LoadSettings(Config* config) { return false; }
42
43 virtual bool EncodePhrase(const string& phrase, const string& value) = 0;
44
45 void set_collector(PhraseCollector* collector) { collector_ = collector; }
46
47 protected:
48 PhraseCollector* collector_;
49};
50
51// Aa : code at index 0 for character at index 0
52// Az : code at index -1 for character at index 0
53// Za : code at index 0 for character at index -1
54 struct CodeCoords {
55 int char_index;
56 int code_index;
57};
58
59 struct TableEncodingRule {
60 int min_word_length;
61 int max_word_length;
62 vector<CodeCoords> coords;
63};
64
65// for rule-based phrase encoding
66 class RIME_DLL TableEncoder : public Encoder {
67 public:
68 TableEncoder(PhraseCollector* collector = NULL);
69
70 bool LoadSettings(Config* config);
71
72 bool Encode(const RawCode& code, string* result);
73 bool EncodePhrase(const string& phrase, const string& value);
74
75 bool IsCodeExcluded(const string& code);
76
77 bool loaded() const { return loaded_; }
78 const vector<TableEncodingRule>& encoding_rules() const {
79 return encoding_rules_;
80 }
81 const string& tail_anchor() const { return tail_anchor_; }
82
83 protected:
84 bool ParseFormula(const string& formula, TableEncodingRule* rule);
85 int CalculateCodeIndex(const string& code, int index, int start);
86 bool DfsEncode(const string& phrase,
87 const string& value,
88 size_t start_pos,
89 RawCode* code,
90 int* limit);
91
92 bool loaded_;
93 // settings
94 vector<TableEncodingRule> encoding_rules_;
95 vector<boost::regex> exclude_patterns_;
96 string tail_anchor_;
97 // for optimization
98 int max_phrase_length_;
99};
100
101// for syllable-based phrase encoding
102 class ScriptEncoder : public Encoder {
103 public:
104 ScriptEncoder(PhraseCollector* collector);
105
106 bool EncodePhrase(const string& phrase, const string& value);
107
108 private:
109 bool DfsEncode(const string& phrase,
110 const string& value,
111 size_t start_pos,
112 RawCode* code,
113 int* limit);
114};
115
116} // namespace rime
117
118#endif // RIME_ENCODER_H_
rime_api.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
rime
Definition algebra.cc:12
rime::RawCode
Definition encoder.h:16
rime::RawCode::ToString
RIME_DLL string ToString() const
Definition encoder.cc:18
rime::RawCode::FromString
RIME_DLL void FromString(const string &code_str)
Definition encoder.cc:22
rime::PhraseCollector
Definition encoder.h:22
rime::PhraseCollector::PhraseCollector
PhraseCollector()=default
rime::PhraseCollector::CreateEntry
virtual void CreateEntry(const string &phrase, const string &code_str, const string &value)=0
rime::PhraseCollector::TranslateWord
virtual bool TranslateWord(const string &word, vector< string > *code)=0
rime::PhraseCollector::~PhraseCollector
virtual ~PhraseCollector()=default
rime::Encoder::Encoder
Encoder(PhraseCollector *collector)
Definition encoder.h:38
rime::Encoder::set_collector
void set_collector(PhraseCollector *collector)
Definition encoder.h:45
rime::Encoder::collector_
PhraseCollector * collector_
Definition encoder.h:48
rime::Encoder::LoadSettings
virtual bool LoadSettings(Config *config)
Definition encoder.h:41
rime::Encoder::~Encoder
virtual ~Encoder()=default
rime::Encoder::EncodePhrase
virtual bool EncodePhrase(const string &phrase, const string &value)=0
rime::CodeCoords
Definition encoder.h:54
rime::CodeCoords::code_index
int code_index
Definition encoder.h:56
rime::CodeCoords::char_index
int char_index
Definition encoder.h:55
rime::TableEncodingRule
Definition encoder.h:59
rime::TableEncodingRule::min_word_length
int min_word_length
Definition encoder.h:60
rime::TableEncodingRule::max_word_length
int max_word_length
Definition encoder.h:61
rime::TableEncodingRule::coords
vector< CodeCoords > coords
Definition encoder.h:62
rime::TableEncoder::TableEncoder
TableEncoder(PhraseCollector *collector=NULL)
Definition encoder.cc:27
rime::TableEncoder::exclude_patterns_
vector< boost::regex > exclude_patterns_
Definition encoder.h:95
rime::TableEncoder::loaded
bool loaded() const
Definition encoder.h:77
rime::TableEncoder::loaded_
bool loaded_
Definition encoder.h:92
rime::TableEncoder::max_phrase_length_
int max_phrase_length_
Definition encoder.h:98
rime::TableEncoder::Encode
bool Encode(const RawCode &code, string *result)
Definition encoder.cc:140
rime::TableEncoder::LoadSettings
bool LoadSettings(Config *config)
Definition encoder.cc:44
rime::TableEncoder::tail_anchor
const string & tail_anchor() const
Definition encoder.h:81
rime::TableEncoder::IsCodeExcluded
bool IsCodeExcluded(const string &code)
Definition encoder.cc:132
rime::TableEncoder::EncodePhrase
bool EncodePhrase(const string &phrase, const string &value)
Definition encoder.cc:235
rime::TableEncoder::tail_anchor_
string tail_anchor_
Definition encoder.h:96
rime::TableEncoder::encoding_rules_
vector< TableEncodingRule > encoding_rules_
Definition encoder.h:94
rime::TableEncoder::encoding_rules
const vector< TableEncodingRule > & encoding_rules() const
Definition encoder.h:78
rime::ScriptEncoder::ScriptEncoder
ScriptEncoder(PhraseCollector *collector)
Definition encoder.cc:291
rime::ScriptEncoder::EncodePhrase
bool EncodePhrase(const string &phrase, const string &value)
Definition encoder.cc:293
rime::Config
Definition config_component.h:21