From bd0a7f68729c7c68a0d607237d0e7cd256f22f52 Mon Sep 17 00:00:00 2001 From: Yuanle Song Date: Mon, 8 Apr 2019 11:06:19 +0800 Subject: [PATCH] v0.5.1 support pinyin with ' in them. now xi'an etc types okay. --- meson.build | 2 +- zero-pinyin-service.c | 45 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/meson.build b/meson.build index a0e565d..8defd75 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,6 @@ # -*- mode: conf -*- project('zero-pinyin-service', ['c', 'cpp'], - version: '0.5.0', + version: '0.5.1', license: 'GPL', default_options: [ 'warning_level=2', diff --git a/zero-pinyin-service.c b/zero-pinyin-service.c index 5f993a8..3796102 100644 --- a/zero-pinyin-service.c +++ b/zero-pinyin-service.c @@ -112,9 +112,44 @@ build_sql_for_n_pinyin (GList* pylist, return result; } +/** + * For a candidate of length group_size, calculate the matched py length. + * + * This is part of get_candidates_for_n_pinyin(). + * + * see param meaning there. + */ +static guint +get_matched_py_length (const char* preedit_str, + GList* pylist, + const guint group_size) +{ + guint matched_py_length = 0; + GList* iter = pylist; + + g_assert_cmpint (group_size, >=, 1); + /* For usual pinyin string, just add up the Pinyin length. But for + * pinyin that contains ', when a Pinyin in pylist is used, also take + * the ' before and after it. */ + for (guint i = 0; i < group_size; ++i) { + while (preedit_str[matched_py_length] == '\'') { + matched_py_length++; + } + matched_py_length += ((Pinyin*) iter->data)->length; + while (preedit_str[matched_py_length] == '\'') { + matched_py_length++; + } + iter = iter->next; + } + return matched_py_length; +} + /** * fetch candidates for a fixed word length. * + * @db: sqlite3 db handler. + * @preedit_str: the pinyin preedit str. can contain '. This is needed to + * calculate matched_py_length. * @pylist: the pinyin list. * @group_size: the fixed word length. use this many pinyin from pinyin list. * @limit: fetch this many result is enough for user. more is not a problem though. @@ -124,6 +159,7 @@ build_sql_for_n_pinyin (GList* pylist, */ static guint get_candidates_for_n_pinyin (sqlite3* db, + const char* preedit_str, GList* pylist, const guint group_size, const guint limit, @@ -143,12 +179,7 @@ get_candidates_for_n_pinyin (sqlite3* db, sql = build_sql_for_n_pinyin (pylist, group_size, MAX (limit, DEFAULT_LIMIT)); g_message ("build_sql_for_n_pinyin result SQL:\n\n%s\n", sql); - guint matched_py_length = 0; - GList* iter = pylist; - for (guint i = 0; i < group_size; ++i) { - matched_py_length += ((Pinyin*) iter->data)->length; - iter = iter->next; - } + guint matched_py_length = get_matched_py_length (preedit_str, pylist, group_size); sqlite3_stmt* stmt = NULL; const char* unused; @@ -221,7 +252,7 @@ get_candidates (sqlite3* db, GList* candidates = NULL; while (fetched_size < fetch_size && group_size > 0) { g_message ("phrase length=%u", group_size); - r = get_candidates_for_n_pinyin (db, pylist, group_size, fetch_size - fetched_size, &candidates); + r = get_candidates_for_n_pinyin (db, preedit_str, pylist, group_size, fetch_size - fetched_size, &candidates); if (candidates) { GList* iter = g_list_first (candidates); Candidate* c = NULL; -- GitLab