From f31c2e6b65d7bfa50af449dd26206b0805968d81 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Tue, 6 Sep 2011 13:57:57 +0800 Subject: add chewing support --- src/pinyin.cpp | 28 ++++++++++++++++++++++++++++ src/pinyin.h | 8 +++++++- tests/Makefile.am | 7 ++++++- tests/test_chewing.cpp | 0 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/test_chewing.cpp diff --git a/src/pinyin.cpp b/src/pinyin.cpp index 822ebe9..8e5773d 100644 --- a/src/pinyin.cpp +++ b/src/pinyin.cpp @@ -9,6 +9,7 @@ struct _pinyin_context_t{ BitmapPinyinValidator m_validator; PinyinDefaultParser * m_default_parser; PinyinShuangPinParser * m_shuang_pin_parser; + PinyinZhuYinParser * m_chewing_parser; PinyinLargeTable * m_pinyin_table; PhraseLargeTable * m_phrase_table; @@ -40,6 +41,7 @@ pinyin_context_t * pinyin_init(const char * systemdir, const char * userdir){ context->m_validator.initialize(context->m_pinyin_table); context->m_default_parser = new PinyinDefaultParser; context->m_shuang_pin_parser = new PinyinShuangPinParser; + context->m_chewing_parser = new PinyinZhuYinParser; context->m_phrase_table = new PhraseLargeTable; chunk = new MemoryChunk; @@ -86,6 +88,7 @@ pinyin_context_t * pinyin_init(const char * systemdir, const char * userdir){ void pinyin_fini(pinyin_context_t * context){ delete context->m_default_parser; delete context->m_shuang_pin_parser; + delete context->m_chewing_parser; delete context->m_pinyin_table; delete context->m_phrase_table; delete context->m_phrase_index; @@ -223,6 +226,31 @@ bool pinyin_parse_more_doubles(pinyin_context_t * context, return pinyin_len == parse_len; } +bool pinyin_parse_chewing(pinyin_context_t * context, + const char * onechewing, + PinyinKey * onekey){ + int chewing_len = strlen(onechewing); + int parse_len = context->m_chewing_parser->parse_one_key + ( context->m_validator, *onekey, onechewing, chewing_len ); + return chewing_len == parse_len; +} + +bool pinyin_parse_more_chewing(pinyin_context_t * context, + const char * chewings, + PinyinKeyVector * pinyin_keys){ + int chewing_len = strlen(chewings); + PinyinKeyPosVector poses; + poses = g_array_new(FALSE, FALSE, sizeof(PinyinKeyPos)); + + int parse_len = context->m_chewing_parser->parse + ( context->m_validator, pinyin_keys, + poses, chewings, chewing_len); + + g_array_free(poses, TRUE); + return pinyin_len == parse_len; +} + + static gint compare_token( gconstpointer lhs, gconstpointer rhs){ phrase_token_t token_lhs = *((phrase_token_t *)lhs); phrase_token_t token_rhs = *((phrase_token_t *)rhs); diff --git a/src/pinyin.h b/src/pinyin.h index 0c612b7..2ac1001 100644 --- a/src/pinyin.h +++ b/src/pinyin.h @@ -72,8 +72,14 @@ bool pinyin_parse_double(pinyin_context_t * context, const char * onepinyin, PinyinKey * onekey); bool pinyin_parse_more_doubles(pinyin_context_t * context, - const char * onepinyin, + const char * pinyins, PinyinKeyVector pinyin_keys); +bool pinyin_parse_chewing(pinyin_context_t * context, + const char * onechewing, + PinyinKey * onekey); +bool pinyin_parse_more_chewing(pinyin_context_t * context, + const char * chewings, + PinyinKeyVector * pinyin_keys); bool pinyin_get_candidates(pinyin_context_t * context, size_t offset, diff --git a/tests/Makefile.am b/tests/Makefile.am index fd9039d..94c831c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -31,7 +31,8 @@ INCLUDES = -I$(top_srcdir)/src \ @GLIB2_CPPFLAGS@ noinst_PROGRAMS = test_pinyin \ - test_phrase + test_phrase \ + test_chewing test_pinyin_SOURCES = test_pinyin.cpp @@ -40,3 +41,7 @@ test_pinyin_LDADD = ../src/libpinyin.la @GLIB2_LDFLAGS@ test_phrase_SOURCES = test_phrase.cpp test_phrase_LDADD = ../src/libpinyin.la @GLIB2_LDFLAGS@ + +test_chewing_SOURCES = test_chewing.cpp + +test_chewing_LDADD = ../src/libpinyin.la @GLIB2_LDFLAGS@ \ No newline at end of file diff --git a/tests/test_chewing.cpp b/tests/test_chewing.cpp new file mode 100644 index 0000000..e69de29 -- cgit