Skip to content

Commit

Permalink
Merge 43a1086 into 85a681d
Browse files Browse the repository at this point in the history
  • Loading branch information
hiunnhue committed Jun 29, 2017
2 parents 85a681d + 43a1086 commit bc859d4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/chewingio.c
Expand Up @@ -1661,12 +1661,6 @@ CHEWING_API int chewing_handle_CtrlNum(ChewingContext *ctx, int key)
ChewingData *pgdata;
ChewingOutput *pgo;
int keystrokeRtn = KEYSTROKE_ABSORB;
int newPhraseLen;
int i;
uint16_t addPhoneSeq[MAX_PHONE_SEQ_LEN];
char addWordSeq[MAX_PHONE_SEQ_LEN * MAX_UTF8_SIZE + 1];
int phraseState;
int cursor;

if (!ctx) {
return -1;
Expand All @@ -1682,7 +1676,6 @@ CHEWING_API int chewing_handle_CtrlNum(ChewingContext *ctx, int key)
return 0;

CallPhrasing(pgdata, 0);
newPhraseLen = key - '0';

if (key == '0' || key == '1') {
pgdata->bSelect = 1;
Expand All @@ -1694,15 +1687,24 @@ CHEWING_API int chewing_handle_CtrlNum(ChewingContext *ctx, int key)
return 0;
}

cursor = PhoneSeqCursor(pgdata);
if (!pgdata->config.bAddPhraseForward) {
if (newPhraseLen >= 1 && cursor + newPhraseLen - 1 <= pgdata->nPhoneSeq) {
if (NoSymbolBetween(pgdata, cursor, cursor + newPhraseLen)) {
if (key >= '2' && key <= '9') {
int i;
int newPhraseLen = key - '0';
int phraseState = 0;
int cursor = PhoneSeqCursor(pgdata);
int key_buf_cursor = pgdata->chiSymbolCursor;
uint16_t addPhoneSeq[MAX_PHONE_SEQ_LEN];
char addWordSeq[MAX_PHONE_SEQ_LEN * MAX_UTF8_SIZE + 1];

if (!pgdata->config.bAddPhraseForward) {
if (cursor + newPhraseLen <= pgdata->nPhoneSeq &&
NoSymbolBetween(pgdata, key_buf_cursor, key_buf_cursor + newPhraseLen)) {

/* Manually add phrase to the user phrase database. */
memcpy(addPhoneSeq, &pgdata->phoneSeq[cursor], sizeof(uint16_t) * newPhraseLen);
addPhoneSeq[newPhraseLen] = 0;

copyStringFromPreeditBuf(pgdata, cursor, newPhraseLen, addWordSeq, sizeof(addWordSeq));
copyStringFromPreeditBuf(pgdata, key_buf_cursor, newPhraseLen, addWordSeq, sizeof(addWordSeq));

phraseState = UserUpdatePhrase(pgdata, addPhoneSeq, addWordSeq);
SetUpdatePhraseMsg(pgdata, addWordSeq, newPhraseLen, phraseState);
Expand All @@ -1711,15 +1713,15 @@ CHEWING_API int chewing_handle_CtrlNum(ChewingContext *ctx, int key)
for (i = 1; i < newPhraseLen; i++)
pgdata->bUserArrBrkpt[cursor + i] = 0;
}
}
} else {
if (newPhraseLen >= 1 && cursor - newPhraseLen >= 0) {
if (NoSymbolBetween(pgdata, cursor - newPhraseLen, cursor)) {
} else {
if (cursor - newPhraseLen >= 0 &&
NoSymbolBetween(pgdata, key_buf_cursor - newPhraseLen, key_buf_cursor)) {

/* Manually add phrase to the user phrase database. */
memcpy(addPhoneSeq, &pgdata->phoneSeq[cursor - newPhraseLen], sizeof(uint16_t) * newPhraseLen);
addPhoneSeq[newPhraseLen] = 0;

copyStringFromPreeditBuf(pgdata, cursor - newPhraseLen, newPhraseLen, addWordSeq, sizeof(addWordSeq));
copyStringFromPreeditBuf(pgdata, key_buf_cursor - newPhraseLen, newPhraseLen, addWordSeq, sizeof(addWordSeq));

phraseState = UserUpdatePhrase(pgdata, addPhoneSeq, addWordSeq);
SetUpdatePhraseMsg(pgdata, addWordSeq, newPhraseLen, phraseState);
Expand All @@ -1729,10 +1731,18 @@ CHEWING_API int chewing_handle_CtrlNum(ChewingContext *ctx, int key)
pgdata->bUserArrBrkpt[cursor - newPhraseLen + i] = 0;
}
}

CallPhrasing(pgdata, 0);
MakeOutputWithRtn(pgo, pgdata, keystrokeRtn);

if (!phraseState) {
/* No userphrase modification, so clear the old aux message. */
pgdata->showMsgLen = 0;
} else {
MakeOutputAddMsgAndCleanInterval(pgo, pgdata);
}
}
CallPhrasing(pgdata, 0);
MakeOutputWithRtn(pgo, pgdata, keystrokeRtn);
MakeOutputAddMsgAndCleanInterval(pgo, pgdata);

return 0;
}

Expand Down
80 changes: 80 additions & 0 deletions test/test-userphrase.c
Expand Up @@ -114,6 +114,8 @@ void test_CtrlNum_add_phrase_right()
static const char msg[] = "\xE5\x8A\xA0\xE5\x85\xA5\xEF\xBC\x9A\xE6\xB8\xAC\xE8\xA9\xA6" /* 加入:測試 */ ;
static const char msg_already_have[] =
"\xE5\xB7\xB2\xE6\x9C\x89\xEF\xBC\x9A\xE6\xB8\xAC\xE8\xA9\xA6" /* 已有:測試 */ ;

int ret;
int cursor;
ChewingContext *ctx;

Expand All @@ -136,6 +138,14 @@ void test_CtrlNum_add_phrase_right()
type_keystroke_by_string(ctx, "<C2>");
ok_aux_buffer(ctx, msg_already_have);

type_keystroke_by_string(ctx, "<EN><C2>");

ret = chewing_aux_Check(ctx);
ok(ret == 0, "chewing_aux_Check() returns `%d', shall be `0'", ret);

ret = chewing_aux_Length(ctx);
ok(ret == 0, "chewing_aux_Length() returns `%d', shall be `0'", ret);

chewing_delete(ctx);
}

Expand All @@ -146,6 +156,8 @@ void test_CtrlNum_add_phrase_left()
static const char msg_add[] = "\xE5\x8A\xA0\xE5\x85\xA5\xEF\xBC\x9A\xE6\xB8\xAC\xE8\xA9\xA6" /* 加入:測試 */ ;
static const char msg_already_have[] =
"\xE5\xB7\xB2\xE6\x9C\x89\xEF\xBC\x9A\xE6\xB8\xAC\xE8\xA9\xA6" /* 已有:測試 */ ;

int ret;
int cursor;
ChewingContext *ctx;

Expand All @@ -168,6 +180,14 @@ void test_CtrlNum_add_phrase_left()
type_keystroke_by_string(ctx, "<C2>");
ok_aux_buffer(ctx, msg_already_have);

type_keystroke_by_string(ctx, "<H><C2>");

ret = chewing_aux_Check(ctx);
ok(ret == 0, "chewing_aux_Check() returns `%d', shall be `0'", ret);

ret = chewing_aux_Length(ctx);
ok(ret == 0, "chewing_aux_Length() returns `%d', shall be `0'", ret);

chewing_delete(ctx);
}

Expand Down Expand Up @@ -219,12 +239,72 @@ void test_CtrlNum_add_phrase_left_symbol_in_between()
chewing_delete(ctx);
}

void test_CtrlNum_add_phrase_right_start_with_symbol()
{
static const char bopomofo[] =
"\xE3\x84\x89\xE3\x84\xA4\xCB\x87 \xE3\x84\x8A\xE3\x84\xA8\xCB\x87 \xE3\x84\x91\xE3\x84\xA7\xE3\x84\xA4\xCB\x8A" /* ㄉㄤˇ ㄊㄨˇ ㄑㄧㄤˊ */ ;
static const char phrase[] = "\xE6\x93\x8B\xE5\x9C\x9F\xE7\x89\x86"; /* 擋土牆 */

const char *const_buf;
ChewingContext *ctx;

clean_userphrase();
ctx = chewing_new();
start_testcase(ctx, fd);
chewing_set_maxChiSymbolLen(ctx, 16);
chewing_set_addPhraseDirection(ctx, 0);

ok(has_userphrase(ctx, bopomofo, NULL) == 0, "`%s' shall not be in userphrase", bopomofo);

type_keystroke_by_string(ctx, "`1hk4g42;3wj3fu;6<L><L><L><D>3<C3>");
ok(has_userphrase(ctx, bopomofo, NULL) == 1, "`%s' shall be in userphrase", bopomofo);

chewing_cand_open(ctx);
chewing_cand_Enumerate(ctx);
const_buf = chewing_cand_string_by_index_static(ctx, 0);
ok(strcmp(const_buf, phrase) == 0, "first candidate `%s' shall be `%s'", const_buf, phrase);

chewing_delete(ctx);
}

void test_CtrlNum_add_phrase_left_start_with_symbol()
{
static const char bopomofo[] =
"\xE3\x84\x89\xE3\x84\xA4\xCB\x87 \xE3\x84\x8A\xE3\x84\xA8\xCB\x87 \xE3\x84\x91\xE3\x84\xA7\xE3\x84\xA4\xCB\x8A" /* ㄉㄤˇ ㄊㄨˇ ㄑㄧㄤˊ */ ;
static const char phrase[] = "\xE6\x93\x8B\xE5\x9C\x9F\xE7\x89\x86"; /* 擋土牆 */

const char *const_buf;
ChewingContext *ctx;

clean_userphrase();
ctx = chewing_new();
start_testcase(ctx, fd);
chewing_set_maxChiSymbolLen(ctx, 16);
chewing_set_addPhraseDirection(ctx, 1);

ok(has_userphrase(ctx, bopomofo, NULL) == 0, "`%s' shall not be in userphrase", bopomofo);

type_keystroke_by_string(ctx, "`1hk4g42;3wj3fu;6<L><L><L><D>3<EN><C3>");
ok(has_userphrase(ctx, bopomofo, NULL) == 1, "`%s' shall be in userphrase", bopomofo);

type_keystroke_by_string(ctx, "<L><L><L>");
chewing_cand_open(ctx);
chewing_cand_Enumerate(ctx);
const_buf = chewing_cand_string_by_index_static(ctx, 0);
ok(strcmp(const_buf, phrase) == 0, "first candidate `%s' shall be `%s'", const_buf, phrase);

chewing_delete(ctx);
}


void test_CtrlNum()
{
test_CtrlNum_add_phrase_right();
test_CtrlNum_add_phrase_left();
test_CtrlNum_add_phrase_right_symbol_in_between();
test_CtrlNum_add_phrase_left_symbol_in_between();
test_CtrlNum_add_phrase_right_start_with_symbol();
test_CtrlNum_add_phrase_left_start_with_symbol();
}

void test_userphrase_auto_learn()
Expand Down

0 comments on commit bc859d4

Please sign in to comment.