From f0977ae0f4eac1b38096fb02fe048c1d086fb334 Mon Sep 17 00:00:00 2001 From: Yuanle Song Date: Wed, 17 Apr 2019 02:50:14 +0800 Subject: [PATCH] make flags param explicit in parse_pinyin() --- operational | 6 +++++- parse-pinyin-test.cpp | 4 ++-- parse-pinyin.cpp | 7 ++++--- parse-pinyin.h | 24 +++++++++++++++--------- zero-pinyin-service.c | 12 +++++++++++- 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/operational b/operational index 8ca17d2..9a87f82 100644 --- a/operational +++ b/operational @@ -1,6 +1,6 @@ * COMMENT -*- mode: org -*- #+Date: 2019-04-05 -Time-stamp: <2019-04-05> +Time-stamp: <2019-04-17> #+STARTUP: content * notes :entry: ** 2019-04-05 zero-pinyin-service file structure :doc: @@ -42,5 +42,9 @@ Time-stamp: <2019-04-05> * later :entry: * current :entry: ** +** 2019-04-17 make flags configurable at runtime. +- add dbus method to set flags. +- make the method work. use gobject property maybe. +- set default flags to my flags. reflect this in UI/config file. * done :entry: * wontfix :entry: diff --git a/parse-pinyin-test.cpp b/parse-pinyin-test.cpp index 204f582..21b85e6 100644 --- a/parse-pinyin-test.cpp +++ b/parse-pinyin-test.cpp @@ -9,7 +9,7 @@ test_parse_pinyin() GList *result = NULL; Pinyin *thispy = NULL; - result = parse_pinyin("liyifeng", 15); + result = parse_pinyin("liyifeng", 15, PINYIN_FLAGS_NONE); g_assert_cmpint(g_list_length(result), ==, 3); thispy = (Pinyin *) g_list_nth_data(result, 0); @@ -52,7 +52,7 @@ test_parse_pinyin_incomplete_pinyin() GList *result = NULL; Pinyin *thispy = NULL; - result = parse_pinyin("zhey", 15); + result = parse_pinyin("zhey", 15, PINYIN_INCOMPLETE_PINYIN); print_parse_result(result); g_assert_cmpint(g_list_length(result), ==, 2); diff --git a/parse-pinyin.cpp b/parse-pinyin.cpp index 5d7e5c3..74450bd 100644 --- a/parse-pinyin.cpp +++ b/parse-pinyin.cpp @@ -4,13 +4,14 @@ #include "../Const.h" GList * -parse_pinyin(const char *preedit_str, const guint max_pinyin) +parse_pinyin(const char *preedit_str, const guint max_pinyin, const guint flags) { GList *result = NULL; PyZy::PinyinArray pyar = {0}; Pinyin *thispy = NULL; - const guint FLAGS = PINYIN_INCOMPLETE_PINYIN | PINYIN_CORRECT_ALL | PINYIN_FUZZY_ALL; - PyZy::PinyinParser::parse(preedit_str, strlen(preedit_str), FLAGS, pyar, max_pinyin); + + PyZy::PinyinParser::parse(preedit_str, strlen(preedit_str), flags, + pyar, max_pinyin); for (guint i = 0; i < pyar.size(); ++i) { thispy = g_new(Pinyin, 1); thispy->shengmu_i = (int) pyar[i].pinyin->pinyin_id[0].sheng; diff --git a/parse-pinyin.h b/parse-pinyin.h index 347de30..04a0f27 100644 --- a/parse-pinyin.h +++ b/parse-pinyin.h @@ -3,20 +3,26 @@ #include #include "zero-pinyin-service.h" +#include "../Const.h" -#ifdef __cplusplus -extern "C" -{ -#endif +G_BEGIN_DECLS + +#define PINYIN_FLAGS_NONE 0 /** * parse preedit_str to groups of pinyin. - * caller should free each Pinyin and the GList after use. + * + * @preedit_str: the preedit str + * @max_pinyin: parse at most this many pinyin from preedit str. + * @flags: support incomplete pinyin, fuzzy pinyin, correction, see ../Const.h + * + * Returns: a list of Pinyin struct. caller should free each Pinyin and the + * GList after use. */ -GList *parse_pinyin(const char *preedit_str, const guint max_pinyin); +GList *parse_pinyin(const char *preedit_str, + const guint max_pinyin, + const guint flags); -#ifdef __cplusplus -} -#endif +G_END_DECLS #endif /* _PARSE_PINYIN_H_ */ diff --git a/zero-pinyin-service.c b/zero-pinyin-service.c index 034f9a0..d941ed7 100644 --- a/zero-pinyin-service.c +++ b/zero-pinyin-service.c @@ -310,8 +310,18 @@ get_candidates(sqlite3 *db, } GList *pylist = NULL; guint pylist_len = 0; + const guint FLAGS = (PINYIN_INCOMPLETE_PINYIN | + PINYIN_FUZZY_C_CH | + PINYIN_FUZZY_CH_C | + PINYIN_FUZZY_Z_ZH | + PINYIN_FUZZY_ZH_Z | + PINYIN_FUZZY_S_SH | + PINYIN_FUZZY_SH_S | + PINYIN_FUZZY_IN_ING | + PINYIN_FUZZY_ING_IN | + PINYIN_CORRECT_ALL); - pylist = parse_pinyin(preedit_str, 15); + pylist = parse_pinyin(preedit_str, 15, FLAGS); pylist_len = g_list_length(pylist); guint group_size = pylist_len; -- GitLab