7
7
#include < boost/algorithm/string.hpp>
8
8
#include < utf8.h>
9
9
#include < rime/algo/calculus.h>
10
+ #include < rime/common.h>
10
11
11
12
namespace rime {
12
13
14
+ const double kAbbreviationPenalty = 0.5 ;
15
+ const double kFuzzySpellingPenalty = 0.5 ;
16
+
13
17
Calculus::Calculus () {
14
18
Register (" xlit" , &Transliteration::Parse);
15
19
Register (" xform" , &Transformation::Parse);
@@ -33,8 +37,7 @@ Calculation* Calculus::Parse(const string& definition) {
33
37
boost::is_from_range (definition[sep], definition[sep]));
34
38
if (args.empty ())
35
39
return NULL ;
36
- map<string,
37
- Calculation::Factory*>::iterator it = factories_.find (args[0 ]);
40
+ auto it = factories_.find (args[0 ]);
38
41
if (it == factories_.end ())
39
42
return NULL ;
40
43
Calculation* result = (*it->second )(args);
@@ -58,9 +61,9 @@ Calculation* Transliteration::Parse(const vector<string>& args) {
58
61
char_map[cl] = cr;
59
62
}
60
63
if (cl == 0 && cr == 0 ) {
61
- Transliteration* x = new Transliteration;
64
+ the< Transliteration> x ( new Transliteration) ;
62
65
x->char_map_ .swap (char_map);
63
- return x;
66
+ return x. release () ;
64
67
}
65
68
return NULL ;
66
69
}
@@ -101,17 +104,17 @@ Calculation* Transformation::Parse(const vector<string>& args) {
101
104
const string& right (args[2 ]);
102
105
if (left.empty ())
103
106
return NULL ;
104
- Transformation* x = new Transformation;
107
+ the< Transformation> x ( new Transformation) ;
105
108
x->pattern_ .assign (left);
106
109
x->replacement_ .assign (right);
107
- return x;
110
+ return x. release () ;
108
111
}
109
112
110
113
bool Transformation::Apply (Spelling* spelling) {
111
114
if (!spelling || spelling->str .empty ())
112
115
return false ;
113
- string result ( boost::regex_replace (spelling->str ,
114
- pattern_, replacement_) );
116
+ string result = boost::regex_replace (spelling->str ,
117
+ pattern_, replacement_);
115
118
if (result == spelling->str )
116
119
return false ;
117
120
spelling->str .swap (result);
@@ -126,9 +129,9 @@ Calculation* Erasion::Parse(const vector<string>& args) {
126
129
const string& pattern (args[1 ]);
127
130
if (pattern.empty ())
128
131
return NULL ;
129
- Erasion* x = new Erasion;
132
+ the< Erasion> x ( new Erasion) ;
130
133
x->pattern_ .assign (pattern);
131
- return x;
134
+ return x. release () ;
132
135
}
133
136
134
137
bool Erasion::Apply (Spelling* spelling) {
@@ -149,10 +152,10 @@ Calculation* Derivation::Parse(const vector<string>& args) {
149
152
const string& right (args[2 ]);
150
153
if (left.empty ())
151
154
return NULL ;
152
- Derivation* x = new Derivation;
155
+ the< Derivation> x ( new Derivation) ;
153
156
x->pattern_ .assign (left);
154
157
x->replacement_ .assign (right);
155
- return x;
158
+ return x. release () ;
156
159
}
157
160
158
161
// Fuzzing
@@ -164,17 +167,17 @@ Calculation* Fuzzing::Parse(const vector<string>& args) {
164
167
const string& right (args[2 ]);
165
168
if (left.empty ())
166
169
return NULL ;
167
- Fuzzing* x = new Fuzzing;
170
+ the< Fuzzing> x ( new Fuzzing) ;
168
171
x->pattern_ .assign (left);
169
172
x->replacement_ .assign (right);
170
- return x;
173
+ return x. release () ;
171
174
}
172
175
173
176
bool Fuzzing::Apply (Spelling* spelling) {
174
177
bool result = Transformation::Apply (spelling);
175
178
if (result) {
176
179
spelling->properties .type = kFuzzySpelling ;
177
- spelling->properties .credibility *= 0.5 ;
180
+ spelling->properties .credibility *= kFuzzySpellingPenalty ;
178
181
}
179
182
return result;
180
183
}
@@ -188,17 +191,17 @@ Calculation* Abbreviation::Parse(const vector<string>& args) {
188
191
const string& right (args[2 ]);
189
192
if (left.empty ())
190
193
return NULL ;
191
- Abbreviation* x = new Abbreviation;
194
+ the< Abbreviation> x ( new Abbreviation) ;
192
195
x->pattern_ .assign (left);
193
196
x->replacement_ .assign (right);
194
- return x;
197
+ return x. release () ;
195
198
}
196
199
197
200
bool Abbreviation::Apply (Spelling* spelling) {
198
201
bool result = Transformation::Apply (spelling);
199
202
if (result) {
200
203
spelling->properties .type = kAbbreviation ;
201
- spelling->properties .credibility *= 0.5 ;
204
+ spelling->properties .credibility *= kAbbreviationPenalty ;
202
205
}
203
206
return result;
204
207
}
0 commit comments