From e0af17eacda954dadaf84c474e6dedd707986f80 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Tue, 20 Dec 2011 17:28:48 +0800 Subject: [PATCH] use new parsers --- src/Makefile.am | 2 +- src/PYPBopomofoEditor.cc | 15 +++++++------ src/PYPDoublePinyinEditor.cc | 23 ++++++++------------ src/PYPFullPinyinEditor.cc | 52 +++++++++----------------------------------- src/PYPPhoneticEditor.cc | 18 +++++++-------- src/PYPPhoneticEditor.h | 1 - src/PYPPinyinEditor.cc | 2 +- 7 files changed, 38 insertions(+), 75 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 0b4787d..c967aa7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -44,7 +44,6 @@ ibus_engine_pinyin_built_h_sources = \ ibus_engine_pinyin_c_sources = \ PYBopomofoEditor.cc \ PYBopomofoEngine.cc \ - PYPConfig.cc \ PYConfig.cc \ PYDatabase.cc \ PYDoublePinyinEditor.cc \ @@ -120,6 +119,7 @@ ibus_engine_pinyin_h_sources = \ if IBUS_BUILD_LIBPINYIN ibus_engine_pinyin_c_sources += \ + PYPConfig.cc \ PYLibPinyin.cc \ PYPPhoneticEditor.cc \ PYPPinyinEditor.cc \ diff --git a/src/PYPBopomofoEditor.cc b/src/PYPBopomofoEditor.cc index 39a7c95..63ec0b5 100644 --- a/src/PYPBopomofoEditor.cc +++ b/src/PYPBopomofoEditor.cc @@ -241,7 +241,7 @@ LibPinyinBopomofoEditor::updatePinyin (void) if (G_UNLIKELY (m_text.empty ())) { m_pinyin_len = 0; /* TODO: check whether to replace "" with NULL. */ - pinyin_parse_more_chewings (m_instance, ""); + pinyin_parse_more_chewings (m_instance, NULL); return; } @@ -348,18 +348,19 @@ LibPinyinBopomofoEditor::updateAuxiliaryText (void) // guint pinyin_cursor = getPinyinCursor (); PinyinKeyVector & pinyin_keys = m_instance->m_pinyin_keys; - PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_poses; + PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_key_rests; for (guint i = 0; i < pinyin_keys->len; ++i) { PinyinKey *key = &g_array_index (pinyin_keys, PinyinKey, i); PinyinKeyPos *pos = &g_array_index (pinyin_poses, PinyinKeyPos, i); - guint cursor = pos->get_pos (); + guint cursor = pos->m_raw_begin; if (G_UNLIKELY (cursor == m_cursor)) { /* at word boundary. */ - m_buffer << '|' << key->get_key_zhuyin_string (); + m_buffer << '|' << key->get_chewing_string (); } else if (G_LIKELY ( cursor < m_cursor && - m_cursor < pos->get_end_pos() )) { /* in word */ + m_cursor < pos->m_raw_end )) { /* in word */ /* raw text */ - String raw = m_text.substr (cursor, pos->get_length ()); + String raw = m_text.substr (cursor, + pos->m_raw_end - pos->m_raw_begin); guint offset = m_cursor - cursor; m_buffer << ' '; String before = raw.substr (0, offset); @@ -373,7 +374,7 @@ LibPinyinBopomofoEditor::updateAuxiliaryText (void) m_buffer << bopomofo_char[keyvalToBopomofo (*iter)]; } } else { /* other words */ - m_buffer << ' ' << key->get_key_zhuyin_string (); + m_buffer << ' ' << key->get_chewing_string (); } } diff --git a/src/PYPDoublePinyinEditor.cc b/src/PYPDoublePinyinEditor.cc index 4ea039e..74fc648 100644 --- a/src/PYPDoublePinyinEditor.cc +++ b/src/PYPDoublePinyinEditor.cc @@ -23,9 +23,6 @@ #include "PYConfig.h" #include "PYLibPinyin.h" -#define DEFINE_DOUBLE_PINYIN_TABLES -#include "PYDoublePinyinTable.h" - using namespace PY; /* @@ -36,11 +33,6 @@ using namespace PY; #define ID(c) \ ((c >= IBUS_a && c <= IBUS_z) ? c - IBUS_a : (c == IBUS_semicolon ? 26 : -1)) -#define ID_TO_SHENG(id) \ - (double_pinyin_map[m_config.doublePinyinSchema ()].sheng[id]) -#define ID_TO_YUNS(id) \ - (double_pinyin_map[m_config.doublePinyinSchema ()].yun[id]) - #define IS_ALPHA(c) \ ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) @@ -71,9 +63,11 @@ LibPinyinDoublePinyinEditor::insert (gint ch) return FALSE; } +#if 0 if (G_UNLIKELY (m_text.empty () && ID_TO_SHENG (id) == PINYIN_ID_VOID)) { return FALSE; } +#endif m_text.insert (m_cursor++, ch); updatePinyin (); @@ -130,23 +124,24 @@ LibPinyinDoublePinyinEditor::updateAuxiliaryText (void) // guint pinyin_cursor = getPinyinCursor (); PinyinKeyVector & pinyin_keys = m_instance->m_pinyin_keys; - PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_poses; + PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_key_rests; for (guint i = 0; i < pinyin_keys->len; ++i) { PinyinKey *key = &g_array_index (pinyin_keys, PinyinKey, i); PinyinKeyPos *pos = &g_array_index (pinyin_poses, PinyinKeyPos, i); - guint cursor = pos->get_pos (); + guint cursor = pos->m_raw_begin; if (G_UNLIKELY (cursor == m_cursor)) { /* at word boundary. */ - m_buffer << '|' << key->get_key_string (); + m_buffer << '|' << key->get_pinyin_string (); } else if (G_LIKELY ( cursor < m_cursor && - m_cursor < pos->get_end_pos() )) { /* in word */ + m_cursor < pos->m_raw_end )) { /* in word */ /* raw text */ - String raw = m_text.substr (cursor, pos->get_length ()); + String raw = m_text.substr (cursor, + pos->m_raw_end - pos->m_raw_begin); guint offset = m_cursor - cursor; m_buffer << ' ' << raw.substr (0, offset) << '|' << raw.substr (offset); } else { /* other words */ - m_buffer << ' ' << key->get_key_string (); + m_buffer << ' ' << key->get_pinyin_string (); } } diff --git a/src/PYPFullPinyinEditor.cc b/src/PYPFullPinyinEditor.cc index 9c019e3..3ef36b3 100644 --- a/src/PYPFullPinyinEditor.cc +++ b/src/PYPFullPinyinEditor.cc @@ -73,45 +73,12 @@ LibPinyinFullPinyinEditor::updatePinyin (void) if (G_UNLIKELY (m_text.empty ())) { m_pinyin_len = 0; /* TODO: check whether to replace "" with NULL. */ - pinyin_parse_more_full_pinyins (m_instance, ""); + pinyin_parse_more_full_pinyins (m_instance, NULL); return; } - PinyinArray pinyins (MAX_PINYIN_LEN); - - m_pinyin_len = PinyinParser::parse (m_text, // text - m_text.length (), // text length - m_config.option (), // option - pinyins, // result - MAX_PHRASE_LEN); // max result length - - /* propagate to libpinyin */ - g_array_set_size (m_instance->m_pinyin_keys, 0); - g_array_set_size (m_instance->m_pinyin_poses, 0); - - PinyinKey key; PinyinKeyPos pos; - PinyinArray::const_iterator iter = pinyins.begin (); - for ( ; iter != pinyins.end (); ++iter ) { - PinyinSegment py = *iter; - String pinyin = py.pinyin->sheng; - gunichar yun_v = g_utf8_get_char("ü"); - gchar buf[7]; - for (const gchar * p = py.pinyin->yun; *p; p = g_utf8_next_char (p)){ - gunichar cur_yun = g_utf8_get_char (p); - if (G_UNLIKELY(yun_v == cur_yun)) { - pinyin += "v"; - } else { - gint len = g_unichar_to_utf8 (cur_yun, buf); - buf[len] = '\0'; - pinyin += buf; - } - } - pinyin_parse_full_pinyin (m_instance, (const char *)pinyin, &key); - pos.set_pos (py.begin); pos.set_length (py.len); - g_array_append_val (m_instance->m_pinyin_keys, key); - g_array_append_val (m_instance->m_pinyin_poses, pos); - } - + m_pinyin_len = + pinyin_parse_more_full_pinyins (m_instance, m_text.c_str ()); pinyin_guess_sentence (m_instance); } @@ -127,23 +94,24 @@ LibPinyinFullPinyinEditor::updateAuxiliaryText () // guint pinyin_cursor = getPinyinCursor (); PinyinKeyVector & pinyin_keys = m_instance->m_pinyin_keys; - PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_poses; + PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_key_rests; for (guint i = 0; i < pinyin_keys->len; ++i) { PinyinKey *key = &g_array_index (pinyin_keys, PinyinKey, i); PinyinKeyPos *pos = &g_array_index (pinyin_poses, PinyinKeyPos, i); - guint cursor = pos->get_pos (); + guint cursor = pos->m_raw_begin; if (G_UNLIKELY (cursor == m_cursor)) { /* at word boundary. */ - m_buffer << '|' << key->get_key_string (); + m_buffer << '|' << key->get_pinyin_string (); } else if (G_LIKELY ( cursor < m_cursor && - m_cursor < pos->get_end_pos() )) { /* in word */ + m_cursor < pos->m_raw_end )) { /* in word */ /* raw text */ - String raw = m_text.substr (cursor, pos->get_length ()); + String raw = m_text.substr (cursor, + pos->m_raw_end - pos->m_raw_begin); guint offset = m_cursor - cursor; m_buffer << ' ' << raw.substr (0, offset) << '|' << raw.substr (offset); } else { /* other words */ - m_buffer << ' ' << key->get_key_string (); + m_buffer << ' ' << key->get_pinyin_string (); } } diff --git a/src/PYPPhoneticEditor.cc b/src/PYPPhoneticEditor.cc index 32a30a2..82cdbf3 100644 --- a/src/PYPPhoneticEditor.cc +++ b/src/PYPPhoneticEditor.cc @@ -338,12 +338,12 @@ guint LibPinyinPhoneticEditor::getPinyinCursor () { /* Translate cursor position to pinyin position. */ - PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_poses; + PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_key_rests; guint pinyin_cursor = pinyin_poses->len; for (size_t i = 0; i < pinyin_poses->len; ++i) { PinyinKeyPos *pos = &g_array_index (pinyin_poses, PinyinKeyPos, i); - if (pos->get_pos () <= m_cursor && m_cursor < pos->get_end_pos ()) + if (pos->m_raw_begin <= m_cursor && m_cursor < pos->m_raw_end) pinyin_cursor = i; } @@ -381,7 +381,7 @@ LibPinyinPhoneticEditor::selectCandidate (guint i) guint8 len = pinyin_choose_candidate (m_instance, lookup_cursor, *token); pinyin_guess_sentence (m_instance); - PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_poses; + PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_key_rests; lookup_cursor += len; if (lookup_cursor == pinyin_poses->len) { commit(); @@ -389,7 +389,7 @@ LibPinyinPhoneticEditor::selectCandidate (guint i) } PinyinKeyPos *pos = &g_array_index (pinyin_poses, PinyinKeyPos, lookup_cursor); - m_cursor = pos->get_pos(); + m_cursor = pos->m_raw_begin; return TRUE; } @@ -491,18 +491,18 @@ LibPinyinPhoneticEditor::getCursorLeftByWord (void) if (G_UNLIKELY (m_cursor > m_pinyin_len)) { cursor = m_pinyin_len; } else { - PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_poses; + PinyinKeyPosVector & pinyin_poses = m_instance->m_pinyin_key_rests; guint pinyin_cursor = getPinyinCursor (); PinyinKeyPos *pos = &g_array_index (pinyin_poses, PinyinKeyPos, pinyin_cursor); - cursor = pos->m_pos; + cursor = pos->m_raw_begin; /* cursor at the begin of one pinyin */ g_return_val_if_fail (pinyin_cursor > 0, 0); if ( cursor == m_cursor) { pos = &g_array_index (pinyin_poses, PinyinKeyPos, pinyin_cursor - 1); - cursor = pos->m_pos; + cursor = pos->m_raw_begin; } } @@ -519,8 +519,8 @@ LibPinyinPhoneticEditor::getCursorRightByWord (void) } else { guint pinyin_cursor = getPinyinCursor (); PinyinKeyPos *pos = &g_array_index - (m_instance->m_pinyin_poses, PinyinKeyPos, pinyin_cursor); - cursor = pos->get_end_pos (); + (m_instance->m_pinyin_key_rests, PinyinKeyPos, pinyin_cursor); + cursor = pos->m_raw_end; } return cursor; diff --git a/src/PYPPhoneticEditor.h b/src/PYPPhoneticEditor.h index 1360e58..92c19d7 100644 --- a/src/PYPPhoneticEditor.h +++ b/src/PYPPhoneticEditor.h @@ -24,7 +24,6 @@ #include #include "PYLookupTable.h" #include "PYEditor.h" -#include "PYPinyinParser.h" namespace PY { diff --git a/src/PYPPinyinEditor.cc b/src/PYPPinyinEditor.cc index 7acf3a9..2391485 100644 --- a/src/PYPPinyinEditor.cc +++ b/src/PYPPinyinEditor.cc @@ -286,7 +286,7 @@ LibPinyinPinyinEditor::updateAuxiliaryText () if (G_LIKELY (i)) m_buffer << ' '; PinyinKey *key = &g_array_index (pinyin_keys, PinyinKey, i); - m_buffer << key->get_key_string (); + m_buffer << key->get_pinyin_string (); } /* append rest text */ -- 2.7.4