Skip to content
parse-pinyin.cpp 1.31 KiB
Newer Older
#include "parse-pinyin.h"
Yuanle Song's avatar
Yuanle Song committed
#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)
Yuanle Song's avatar
Yuanle Song committed
{
	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;
}

GList*
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);
	for (guint i = 0; i < pyar.size(); ++i) {
		thispy = g_new (Pinyin, 1);
		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 = g_list_append (result, thispy);
	}
	return result;
}