From d13ff12005105e8b7ab8414377e95692c05a060b Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Wed, 14 Oct 2015 15:49:15 +0800 Subject: write chewing_key.cpp --- src/storage/chewing_key.cpp | 103 +++++++++++++++++++++++++++++++++++++++++ src/storage/chewing_key.h | 2 + src/storage/pinyin_parser2.cpp | 50 -------------------- 3 files changed, 105 insertions(+), 50 deletions(-) create mode 100644 src/storage/chewing_key.cpp diff --git a/src/storage/chewing_key.cpp b/src/storage/chewing_key.cpp new file mode 100644 index 0000000..26a3642 --- /dev/null +++ b/src/storage/chewing_key.cpp @@ -0,0 +1,103 @@ +/* + * libpinyin + * Library to deal with pinyin. + * + * Copyright (C) 2015 Peng Wu + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "chewing_key.h" +#include +#include "pinyin_parser2.h" +#include "pinyin_parser_table.h" +#include "zhuyin_table.h" + + +gint _ChewingKey::get_table_index() { + assert(m_initial < CHEWING_NUMBER_OF_INITIALS); + assert(m_middle < CHEWING_NUMBER_OF_MIDDLES); + assert(m_final < CHEWING_NUMBER_OF_FINALS); + + gint index = chewing_key_table[(m_initial * CHEWING_NUMBER_OF_MIDDLES + m_middle) * CHEWING_NUMBER_OF_FINALS + m_final]; + return index == -1 ? 0 : index; +} + +gchar * _ChewingKey::get_pinyin_string() { + assert(m_tone < CHEWING_NUMBER_OF_TONES); + gint index = get_table_index(); + assert(index < (int) G_N_ELEMENTS(content_table)); + const content_table_item_t & item = content_table[index]; + + if (CHEWING_ZERO_TONE == m_tone) { + return g_strdup(item.m_pinyin_str); + } else { + return g_strdup_printf("%s%d", item.m_pinyin_str, m_tone); + } +} + +gchar * _ChewingKey::get_luoma_pinyin_string() { + assert(m_tone < CHEWING_NUMBER_OF_TONES); + gint index = get_table_index(); + assert(index < (int) G_N_ELEMENTS(content_table)); + const content_table_item_t & item = content_table[index]; + + if (CHEWING_ZERO_TONE == m_tone) { + return g_strdup(item.m_luoma_pinyin_str); + } else { + return g_strdup_printf("%s%d", item.m_luoma_pinyin_str, m_tone); + } +} + +gchar * _ChewingKey::get_secondary_zhuyin_string() { + assert(m_tone < CHEWING_NUMBER_OF_TONES); + gint index = get_table_index(); + assert(index < (int) G_N_ELEMENTS(content_table)); + const content_table_item_t & item = content_table[index]; + + if (CHEWING_ZERO_TONE == m_tone) { + return g_strdup(item.m_secondary_zhuyin_str); + } else { + return g_strdup_printf("%s%d", item.m_secondary_zhuyin_str, m_tone); + } +} + +gchar * _ChewingKey::get_shengmu_string() { + gint index = get_table_index(); + assert(index < (int) G_N_ELEMENTS(content_table)); + const content_table_item_t & item = content_table[index]; + return g_strdup(item.m_shengmu_str); +} + +gchar * _ChewingKey::get_yunmu_string() { + gint index = get_table_index(); + assert(index < (int) G_N_ELEMENTS(content_table)); + const content_table_item_t & item = content_table[index]; + return g_strdup(item.m_yunmu_str); +} + +gchar * _ChewingKey::get_chewing_string() { + assert(m_tone < CHEWING_NUMBER_OF_TONES); + gint index = get_table_index(); + assert(index < (int) G_N_ELEMENTS(content_table)); + const content_table_item_t & item = content_table[index]; + + if (CHEWING_ZERO_TONE == m_tone) { + return g_strdup(item.m_zhuyin_str); + } else { + return g_strdup_printf("%s%s", item.m_zhuyin_str, + chewing_tone_table[m_tone]); + } +} diff --git a/src/storage/chewing_key.h b/src/storage/chewing_key.h index f3202e8..8c6ef8d 100644 --- a/src/storage/chewing_key.h +++ b/src/storage/chewing_key.h @@ -66,6 +66,8 @@ public: /* Note: the return value should be freed by g_free. */ gchar * get_pinyin_string(); + gchar * get_luoma_pinyin_string(); + gchar * get_secondary_zhuyin_string(); gchar * get_shengmu_string(); gchar * get_yunmu_string(); gchar * get_chewing_string(); diff --git a/src/storage/pinyin_parser2.cpp b/src/storage/pinyin_parser2.cpp index 0015ccc..0a1f170 100644 --- a/src/storage/pinyin_parser2.cpp +++ b/src/storage/pinyin_parser2.cpp @@ -58,56 +58,6 @@ static bool check_pinyin_options(pinyin_option_t options, const pinyin_index_ite return true; } -gint _ChewingKey::get_table_index() { - assert(m_initial < CHEWING_NUMBER_OF_INITIALS); - assert(m_middle < CHEWING_NUMBER_OF_MIDDLES); - assert(m_final < CHEWING_NUMBER_OF_FINALS); - - gint index = chewing_key_table[(m_initial * CHEWING_NUMBER_OF_MIDDLES + m_middle) * CHEWING_NUMBER_OF_FINALS + m_final]; - return index == -1 ? 0 : index; -} - -gchar * _ChewingKey::get_pinyin_string() { - assert(m_tone < CHEWING_NUMBER_OF_TONES); - gint index = get_table_index(); - assert(index < (int) G_N_ELEMENTS(content_table)); - const content_table_item_t & item = content_table[index]; - - if (CHEWING_ZERO_TONE == m_tone) { - return g_strdup(item.m_pinyin_str); - } else { - return g_strdup_printf("%s%d", item.m_pinyin_str, m_tone); - } -} - -gchar * _ChewingKey::get_shengmu_string() { - gint index = get_table_index(); - assert(index < (int) G_N_ELEMENTS(content_table)); - const content_table_item_t & item = content_table[index]; - return g_strdup(item.m_shengmu_str); -} - -gchar * _ChewingKey::get_yunmu_string() { - gint index = get_table_index(); - assert(index < (int) G_N_ELEMENTS(content_table)); - const content_table_item_t & item = content_table[index]; - return g_strdup(item.m_yunmu_str); -} - -gchar * _ChewingKey::get_chewing_string() { - assert(m_tone < CHEWING_NUMBER_OF_TONES); - gint index = get_table_index(); - assert(index < (int) G_N_ELEMENTS(content_table)); - const content_table_item_t & item = content_table[index]; - - if (CHEWING_ZERO_TONE == m_tone) { - return g_strdup(item.m_zhuyin_str); - } else { - return g_strdup_printf("%s%s", item.m_zhuyin_str, - chewing_tone_table[m_tone]); - } -} - /* Pinyin Parsers */ -- cgit