From de6b1f956fec426ba45f3ec371e5ce802070fd67 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Thu, 18 Dec 2014 11:29:36 +0800 Subject: write rememberUserInput method --- src/PYLibPinyin.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/PYLibPinyin.h | 2 ++ src/PYPBopomofoEditor.cc | 2 ++ src/PYPPinyinEditor.cc | 2 ++ 4 files changed, 51 insertions(+) (limited to 'src') diff --git a/src/PYLibPinyin.cc b/src/PYLibPinyin.cc index 7f13897..3ea3cff 100644 --- a/src/PYLibPinyin.cc +++ b/src/PYLibPinyin.cc @@ -268,6 +268,51 @@ LibPinyinBackEnd::clearPinyinUserData (const char * target) return TRUE; } +gboolean +LibPinyinBackEnd::rememberUserInput (pinyin_instance_t * instance) +{ + /* pre-check the incomplete pinyin keys. */ + guint len = 0; + g_assert (pinyin_get_n_pinyin (instance, &len)); + size_t i = 0; + for ( ; i < len; ++i) { + PinyinKey *key = NULL; + g_assert (pinyin_get_pinyin_key (instance, i, &key)); + if (pinyin_get_pinyin_is_incomplete (instance, key)) + return FALSE; + } + + /* prepare pinyin string. */ + GPtrArray *array = g_ptr_array_new (); + for (i = 0; i < len; ++i) { + PinyinKey *key = NULL; + g_assert (pinyin_get_pinyin_key (instance, i, &key)); + gchar * pinyin = NULL; + g_assert (pinyin_get_pinyin_string (instance, key, &pinyin)); + g_ptr_array_add (array, pinyin); + } + g_ptr_array_add (array, NULL); + + gchar **strings = (gchar **) g_ptr_array_free (array, FALSE); + gchar *pinyins = g_strjoinv ("'", strings); + g_strfreev (strings); + + /* remember user input. */ + import_iterator_t * iter = NULL; + pinyin_context_t * context = pinyin_get_context (instance); + iter = pinyin_begin_add_phrases (context, USER_DICTIONARY); + char * phrase = NULL; + g_assert (pinyin_get_sentence (instance, &phrase)); + g_assert (pinyin_iterator_add_phrase (iter, phrase, pinyins, -1)); + pinyin_end_add_phrases (iter); + g_free (phrase); + g_free (pinyins); + + /* save later, + will mark modified from pinyin/bopomofo editor. */ + return TRUE; +} + gboolean LibPinyinBackEnd::timeoutCallback (gpointer data) { diff --git a/src/PYLibPinyin.h b/src/PYLibPinyin.h index 45b2757..3e6a22e 100644 --- a/src/PYLibPinyin.h +++ b/src/PYLibPinyin.h @@ -53,6 +53,8 @@ public: gboolean importPinyinDictionary (const char * filename); gboolean clearPinyinUserData (const char * target); + gboolean rememberUserInput (pinyin_instance_t * instance); + /* use static initializer in C++. */ static LibPinyinBackEnd & instance (void) { return *m_instance; } diff --git a/src/PYPBopomofoEditor.cc b/src/PYPBopomofoEditor.cc index e0788a7..43a0203 100644 --- a/src/PYPBopomofoEditor.cc +++ b/src/PYPBopomofoEditor.cc @@ -287,6 +287,8 @@ BopomofoEditor::commit () } pinyin_train(m_instance); + if (m_config.rememberEveryInput ()) + LibPinyinBackEnd::instance ().rememberUserInput (m_instance); LibPinyinBackEnd::instance ().modified(); PhoneticEditor::commit ((const gchar *)m_buffer); reset(); diff --git a/src/PYPPinyinEditor.cc b/src/PYPPinyinEditor.cc index 2ab5151..e5933be 100644 --- a/src/PYPPinyinEditor.cc +++ b/src/PYPPinyinEditor.cc @@ -232,6 +232,8 @@ PinyinEditor::commit () } pinyin_train (m_instance); + if (m_config.rememberEveryInput ()) + LibPinyinBackEnd::instance ().rememberUserInput (m_instance); LibPinyinBackEnd::instance ().modified (); PhoneticEditor::commit ((const gchar *)m_buffer); reset(); -- cgit