From 732801da0d447993dbf8dd9b82bf849afdbf34af Mon Sep 17 00:00:00 2001 From: Yuanle Song Date: Fri, 5 Apr 2019 14:40:50 +0800 Subject: [PATCH] v0.3.0 enable incomplete pinyin support; enabled some fuzzy pinyin support. --- meson.build | 2 +- parse-pinyin-test.cpp | 69 +++++++++++++++++++++++++++---------------- parse-pinyin.cpp | 29 ++---------------- parse-pinyin.h | 9 ------ 4 files changed, 47 insertions(+), 62 deletions(-) diff --git a/meson.build b/meson.build index c882410..69212eb 100644 --- a/meson.build +++ b/meson.build @@ -1,6 +1,6 @@ # -*- mode: conf -*- project('zero-pinyin-service', ['c', 'cpp'], - version: '0.2.1', + version: '0.3.0', license: 'GPL', default_options: [ 'warning_level=2', diff --git a/parse-pinyin-test.cpp b/parse-pinyin-test.cpp index cd41862..7aa5a8e 100644 --- a/parse-pinyin-test.cpp +++ b/parse-pinyin-test.cpp @@ -1,29 +1,7 @@ #include "parse-pinyin.h" #include - -static void -test_parse_pinyin_cpp () -{ - std::vector* result = parse_pinyin_cpp ("liyifeng"); - g_assert_cmpint (result->size (), ==, 3); - - g_assert_cmpint (result->at(0)->shengmu_i, ==, 10); - g_assert_cmpint (result->at(0)->yunmu_i, ==, 34); - g_assert_cmpint (result->at(0)->length, ==, 2); - - g_assert_cmpint (result->at(1)->shengmu_i, ==, 21); - g_assert_cmpint (result->at(1)->yunmu_i, ==, 34); - g_assert_cmpint (result->at(1)->length, ==, 2); - - g_assert_cmpint (result->at(2)->shengmu_i, ==, 5); - g_assert_cmpint (result->at(2)->yunmu_i, ==, 32); - g_assert_cmpint (result->at(2)->length, ==, 4); - - for (guint i = 0; i < result->size (); ++i) { - delete result->at(i); - } - delete result; -} +#include +#include static void test_parse_pinyin () @@ -52,14 +30,53 @@ test_parse_pinyin () g_list_free_full (result, g_free); } +/** + * print GList of Pinyin. + */ +static void +print_parse_result (GList* result) +{ + GList* iter = result; + Pinyin* thispy = NULL; + while (iter != NULL) { + thispy = (Pinyin*) iter->data; + g_printf ("shengmu_i=%i yunmu_i=%i length=%u\n", + thispy->shengmu_i, thispy->yunmu_i, thispy->length); + iter = iter->next; + } +} + +static void +test_parse_pinyin_incomplete_pinyin () +{ + GList* result = NULL; + Pinyin* thispy = NULL; + + result = parse_pinyin ("zhey", 15); + print_parse_result (result); + g_assert_cmpint (g_list_length (result), ==, 2); + + thispy = (Pinyin*) g_list_nth_data (result, 0); + g_assert_cmpint (thispy->shengmu_i, ==, 23); + g_assert_cmpint (thispy->yunmu_i, ==, 29); + g_assert_cmpint (thispy->length, ==, 3); + + thispy = (Pinyin*) g_list_nth_data (result, 1); + g_assert_cmpint (thispy->shengmu_i, ==, 21); + g_assert_cmpint (thispy->yunmu_i, ==, 0); + g_assert_cmpint (thispy->length, ==, 1); + + g_list_free_full (result, g_free); +} + int main (int argc, char *argv[]) { g_test_init (&argc, &argv, NULL); - g_test_add_func ("/zero/test_parse_pinyin_cpp", - test_parse_pinyin_cpp); g_test_add_func ("/zero/test_parse_pinyin", test_parse_pinyin); + g_test_add_func ("/zero/test_parse_pinyin incomplete pinyin", + test_parse_pinyin_incomplete_pinyin); return g_test_run (); } diff --git a/parse-pinyin.cpp b/parse-pinyin.cpp index 1b0a1de..095d3af 100644 --- a/parse-pinyin.cpp +++ b/parse-pinyin.cpp @@ -1,30 +1,7 @@ #include "parse-pinyin.h" #include "../PinyinArray.h" #include "../PinyinParser.h" - -/** - * parse preedit_str to array of Pinyin. - * each Pinyin contains shengmu_i, yunmu_i, and pinyin's length. - * - * @preedit_str: the preedit string - * - * returns: a list of Pinyin - */ -std::vector* -parse_pinyin_cpp (const char* preedit_str, const guint max_pinyin) -{ - std::vector* result = new std::vector(); - PyZy::PinyinArray pyar = {0}; - PyZy::PinyinParser::parse(preedit_str, strlen(preedit_str), 0, pyar, max_pinyin); - Pinyin thispy = {0}; - for (guint i = 0; i < pyar.size(); ++i) { - thispy.shengmu_i = (int) pyar[i].pinyin->pinyin_id[0].sheng; - thispy.yunmu_i = (int) pyar[i].pinyin->pinyin_id[0].yun; - thispy.length = pyar[i].len; - result->push_back (new Pinyin(thispy)); - } - return result; -} +#include "../Const.h" GList* parse_pinyin (const char* preedit_str, const guint max_pinyin) @@ -32,8 +9,8 @@ parse_pinyin (const char* preedit_str, const guint max_pinyin) GList* result = NULL; PyZy::PinyinArray pyar = {0}; Pinyin* thispy = NULL; - - PyZy::PinyinParser::parse (preedit_str, strlen(preedit_str), 0, pyar, max_pinyin); + const guint FLAGS = PINYIN_INCOMPLETE_PINYIN | PINYIN_CORRECT_ALL | PINYIN_FUZZY_ALL; + 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 e169b1a..eb791ce 100644 --- a/parse-pinyin.h +++ b/parse-pinyin.h @@ -5,15 +5,6 @@ #include "zero-pinyin-service.h" #ifdef __cplusplus - -#include - -/** - * parse preedit_str to groups of pinyin. - * caller should free each Pinyin and the vector after use. - */ -std::vector* parse_pinyin_cpp (const char* preedit_str, const guint max_pinyin=15); - extern "C" { #endif -- GitLab