Skip to content

Commit 74dd54f

Browse files
committed
allow interpuncts in phrases
1 parent bffd0cb commit 74dd54f

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

src/rime/algo/encoder.cc

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ namespace rime {
1414

1515
static const int kEncoderDfsLimit = 32;
1616
static const int kMaxPhraseLength = 32;
17+
// U+00B7 MIDDLE DOT, U+2027 HYPHENATION POINT, U+2010 HYPHEN,
18+
// U+FF0D FULLWIDTH HYPHEN-MINUS, U+FF0C FULLWIDTH COMMA
19+
static const string& interpuncts =
20+
"\xc2\xb7\xe2\x80\xa7\xe2\x80\x90\xef\xbc\x8d\xef\xbc\x8c";
21+
22+
string stripPunct(const string& phrase) {
23+
string phrase_no_punct;
24+
size_t start_pos = 0;
25+
while (start_pos < phrase.length()) {
26+
const char* word_start = phrase.c_str() + start_pos;
27+
const char* word_end = word_start;
28+
utf8::unchecked::next(word_end);
29+
size_t word_len = word_end - word_start;
30+
string word(word_start, word_len);
31+
if (interpuncts.find(word) == string::npos)
32+
phrase_no_punct += word;
33+
start_pos += word_len;
34+
}
35+
return phrase_no_punct;
36+
}
1737

1838
string RawCode::ToString() const {
1939
return strings::join(*this, " ");
@@ -234,8 +254,10 @@ int TableEncoder::CalculateCodeIndex(const string& code, int index, int start) {
234254
}
235255

236256
bool TableEncoder::EncodePhrase(const string& phrase, const string& value) {
257+
string phrase_no_punct = stripPunct(phrase);
237258
size_t phrase_length = utf8::unchecked::distance(
238-
phrase.c_str(), phrase.c_str() + phrase.length());
259+
phrase_no_punct.c_str(),
260+
phrase_no_punct.c_str() + phrase_no_punct.length());
239261
if (static_cast<int>(phrase_length) > max_phrase_length_)
240262
return false;
241263

@@ -249,7 +271,8 @@ bool TableEncoder::DfsEncode(const string& phrase,
249271
size_t start_pos,
250272
RawCode* code,
251273
int* limit) {
252-
if (start_pos == phrase.length()) {
274+
string phrase_no_punct = stripPunct(phrase);
275+
if (start_pos == phrase_no_punct.length()) {
253276
if (limit) {
254277
--*limit;
255278
}
@@ -265,7 +288,7 @@ bool TableEncoder::DfsEncode(const string& phrase,
265288
return false;
266289
}
267290
}
268-
const char* word_start = phrase.c_str() + start_pos;
291+
const char* word_start = phrase_no_punct.c_str() + start_pos;
269292
const char* word_end = word_start;
270293
utf8::unchecked::next(word_end);
271294
size_t word_len = word_end - word_start;
@@ -292,8 +315,10 @@ bool TableEncoder::DfsEncode(const string& phrase,
292315
ScriptEncoder::ScriptEncoder(PhraseCollector* collector) : Encoder(collector) {}
293316

294317
bool ScriptEncoder::EncodePhrase(const string& phrase, const string& value) {
318+
string phrase_no_punct = stripPunct(phrase);
295319
size_t phrase_length = utf8::unchecked::distance(
296-
phrase.c_str(), phrase.c_str() + phrase.length());
320+
phrase_no_punct.c_str(),
321+
phrase_no_punct.c_str() + phrase_no_punct.length());
297322
if (static_cast<int>(phrase_length) > kMaxPhraseLength)
298323
return false;
299324

@@ -307,16 +332,17 @@ bool ScriptEncoder::DfsEncode(const string& phrase,
307332
size_t start_pos,
308333
RawCode* code,
309334
int* limit) {
310-
if (start_pos == phrase.length()) {
335+
string phrase_no_punct = stripPunct(phrase);
336+
if (start_pos == phrase_no_punct.length()) {
311337
if (limit) {
312338
--*limit;
313339
}
314340
collector_->CreateEntry(phrase, code->ToString(), value);
315341
return true;
316342
}
317343
bool ret = false;
318-
for (size_t k = phrase.length() - start_pos; k > 0; --k) {
319-
string word(phrase.substr(start_pos, k));
344+
for (size_t k = phrase_no_punct.length() - start_pos; k > 0; --k) {
345+
string word(phrase_no_punct.substr(start_pos, k));
320346
vector<string> translations;
321347
if (collector_->TranslateWord(word, &translations)) {
322348
for (const string& x : translations) {

0 commit comments

Comments
 (0)