chewing / libchewing

libchewing - The intelligent phonetic input method library
https://chewing.im/
GNU Lesser General Public License v2.1
359 stars 90 forks source link

Implement has_userphrase with chewing_userphrase_lookup #96

Open czchen opened 10 years ago

czchen commented 10 years ago

has_userphrase is used to do userphrase test. After chewing_userphrase_lookup is implemented, has_userphrase shall use chewing_userphrase_lookup instead of libchewing internal functions.

ShengYenPeng commented 8 years ago

Hi @czchen

I traced the test-userphrase.c

the functiontest_userphrase_auto_learn use has_userphrase in this way: pass the NULL pointer in compare phrase field

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

and the function test_userphrase_auto_learn_with_symbol use has_userphrase in same way:

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

However, the chewing_userphrase_lookup has different return value with internal_has_userphrase:

the chewing_userphrase_lookup wiil return 0

    if (!ctx || !phrase_buf || !bopomofo_buf) {
        return 0;
    }

the internal_has_userphrase will return 1

 for (userphrase = UserGetPhraseFirst(ctx->data, phone);
         userphrase != NULL; userphrase = UserGetPhraseNext(ctx->data, phone)) {
        if (phrase == NULL || strcmp(userphrase->wordSeq, phrase) == 0) {
            ret = 1;
            goto end;
        }

this will make make check failed if I Implement has_userphrase with chewing_userphrase_lookup.

should the chewing_userphrase_lookup return 1 when the phrase_buf is NULL and the user_phrase_data = UserGetPhraseFirst(pgdata, phone_buf) is not NULL ?