write importPinyinDictionary
authorPeng Wu <alexepico@gmail.com>
Fri, 8 Feb 2013 03:04:28 +0000 (11:04 +0800)
committerPeng Wu <alexepico@gmail.com>
Fri, 8 Feb 2013 03:04:28 +0000 (11:04 +0800)
src/PYLibPinyin.cc
src/PYLibPinyin.h
src/PYPConfig.cc

index 811400c..285e52c 100644 (file)
@@ -239,7 +239,51 @@ LibPinyinBackEnd::modified (void)
                                           static_cast<gpointer> (this));
 }
 
-bool
+gboolean
+LibPinyinBackEnd::importPinyinDictionary(const char * filename)
+{
+    /* user phrase library should be loaded here. */
+    FILE * dictfile = fopen(filename, "r");
+    if (NULL == dictfile)
+        return FALSE;
+
+    import_iterator_t * iter = pinyin_begin_add_phrases
+        (m_pinyin_context, 15);
+
+    if (NULL == iter)
+        return FALSE;
+
+    char* linebuf = NULL; size_t size = 0; ssize_t read;
+    while ((read = getline(&linebuf, &size, dictfile)) != -1) {
+        if (0 == strlen(linebuf))
+            continue;
+
+        if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
+            linebuf[strlen(linebuf) - 1] = '\0';
+        }
+
+        gchar ** items = g_strsplit_set(linebuf, " \t", 3);
+        guint len = g_strv_length(items);
+
+        gchar * phrase = NULL, * pinyin = NULL;
+        gint count = -1;
+        if (2 == len || 3 == len) {
+            phrase = items[0];
+            pinyin = items[1];
+            if (3 == len)
+                count = atoi(items[2]);
+        } else
+            continue;
+
+        pinyin_iterator_add_phrase(iter, phrase, pinyin, count);
+    }
+
+    pinyin_end_add_phrases(iter);
+    fclose(dictfile);
+}
+
+
+gboolean
 LibPinyinBackEnd::clearPinyinUserData (const char * target)
 {
     if (0 == strcmp("all", target))
@@ -249,6 +293,8 @@ LibPinyinBackEnd::clearPinyinUserData (const char * target)
                         PHRASE_INDEX_MAKE_TOKEN(15, null_token));
     else
         g_warning("unknown clear target: %s.\n", target);
+
+    return TRUE;
 }
 
 gboolean
index 18fd9d1..2400b0e 100644 (file)
@@ -50,8 +50,8 @@ public:
     void freeChewingInstance (pinyin_instance_t *instance);
     void modified (void);
 
-    bool importPinyinDictionary(const char * filename);
-    bool clearPinyinUserData(const char * target);
+    gboolean importPinyinDictionary(const char * filename);
+    gboolean clearPinyinUserData(const char * target);
 
     /* use static initializer in C++. */
     static LibPinyinBackEnd & instance (void) { return *m_instance; }
index de78dda..6a70539 100644 (file)
@@ -51,8 +51,8 @@ const gchar * const CONFIG_GUIDE_KEY                 = "GuideKey";
 const gchar * const CONFIG_AUXILIARY_SELECT_KEY_F    = "AuxiliarySelectKey_F";
 const gchar * const CONFIG_AUXILIARY_SELECT_KEY_KP   = "AuxiliarySelectKey_KP";
 const gchar * const CONFIG_ENTER_KEY                 = "EnterKey";
-const gchar * const CONFIG_CLEAR_USER_DATA           = "ClearUserData";
 const gchar * const CONFIG_IMPORT_DICTIONARY         = "ImportDictionary";
+const gchar * const CONFIG_CLEAR_USER_DATA           = "ClearUserData";
 
 const guint PINYIN_DEFAULT_OPTION =
         PINYIN_INCOMPLETE |
@@ -403,6 +403,10 @@ LibPinyinPinyinConfig::valueChanged (const std::string &section,
         m_comma_period_page = normalizeGVariant (value, true);
     else if (CONFIG_AUTO_COMMIT == name)
         m_auto_commit = normalizeGVariant (value, false);
+    else if (CONFIG_IMPORT_DICTIONARY == name) {
+        std::string filename = normalizeGVariant (value, std::string(""));
+        LibPinyinBackEnd::instance ().importPinyinDictionary(filename.c_str ());
+    }
     else if (CONFIG_CLEAR_USER_DATA == name) {
         std::string target = normalizeGVariant (value, std::string(""));
         LibPinyinBackEnd::instance ().clearPinyinUserData(target.c_str ());