Commit 732801da authored by Yuanle Song's avatar Yuanle Song
Browse files

v0.3.0 enable incomplete pinyin support;

enabled some fuzzy pinyin support.
parent 65255018
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
# -*- mode: conf -*-
project('zero-pinyin-service', ['c', 'cpp'],
  version: '0.2.1',
  version: '0.3.0',
  license: 'GPL',
  default_options: [
    'warning_level=2',
+43 −26
Original line number Diff line number Diff line
#include "parse-pinyin.h"
#include <iostream>

static void
test_parse_pinyin_cpp ()
{
	std::vector<Pinyin*>* 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 <glib.h>
#include <glib/gprintf.h>

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 ();
}
+3 −26
Original line number Diff line number Diff line
#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<Pinyin*>*
parse_pinyin_cpp (const char* preedit_str, const guint max_pinyin)
{
	std::vector<Pinyin*>* result = new std::vector<Pinyin*>();
	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;
+0 −9
Original line number Diff line number Diff line
@@ -5,15 +5,6 @@
#include "zero-pinyin-service.h"

#ifdef __cplusplus

#include <vector>

/**
 * parse preedit_str to groups of pinyin.
 * caller should free each Pinyin and the vector after use.
 */
std::vector<Pinyin*>* parse_pinyin_cpp (const char* preedit_str, const guint max_pinyin=15);

extern "C"
{
#endif