summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2020-06-19 15:10:49 +0800
committerPeng Wu <alexepico@gmail.com>2020-07-21 15:27:51 +0800
commitdc67c6437dd2f3a5790c7065f03631a123e144db (patch)
treec27252a6f7f65e08b42463547786c8f0beb63890
parent8d7e340b73caa3033040e229cc6198357a127823 (diff)
downloadibus-libpinyin-dc67c6437dd2f3a5790c7065f03631a123e144db.tar.gz
ibus-libpinyin-dc67c6437dd2f3a5790c7065f03631a123e144db.tar.xz
ibus-libpinyin-dc67c6437dd2f3a5790c7065f03631a123e144db.zip
write readNetworkDictionary method
-rw-r--r--src/PYLibPinyin.cc134
-rw-r--r--src/PYLibPinyin.h18
2 files changed, 152 insertions, 0 deletions
diff --git a/src/PYLibPinyin.cc b/src/PYLibPinyin.cc
index b4b2569..52365ad 100644
--- a/src/PYLibPinyin.cc
+++ b/src/PYLibPinyin.cc
@@ -349,3 +349,137 @@ LibPinyinBackEnd::saveUserDB (void)
pinyin_save (m_chewing_context);
return TRUE;
}
+
+#define TIMESTAMP_LINE "# timestamp: %ld\n"
+
+bool
+LibPinyinBackEnd::readNetworkDictionary(pinyin_context_t * context,
+ const char * filename,
+ /* inout */ time_t & start,
+ /* inout */ time_t & loaded)
+{
+ time_t current = time (NULL);
+
+ FILE * dictfile = fopen (filename, "r");
+
+ if (!checkNetworkDictionary (context, dictfile, start, loaded)) {
+ fclose (dictfile);
+ return FALSE;
+ }
+
+ fseek (dictfile, 0, SEEK_SET);
+
+ /* read to the loaded time. */
+ forwardNetworkDictionary (dictfile, loaded);
+
+ /* import the rest of network dictionary. */
+ importRestNetworkDictionary (context, dictfile, loaded);
+
+ fclose (dictfile);
+
+ pinyin_save (context);
+ return TRUE;
+}
+
+bool
+LibPinyinBackEnd::clearNetworkDictionary (pinyin_context_t * context)
+{
+ pinyin_mask_out (context, PHRASE_INDEX_LIBRARY_MASK,
+ PHRASE_INDEX_MAKE_TOKEN (NETWORK_DICTIONARY, null_token));
+ pinyin_save (context);
+ return TRUE;
+}
+
+bool
+LibPinyinBackEnd::checkNetworkDictionary (pinyin_context_t * context,
+ FILE * dictfile,
+ /* inout */ time_t & start,
+ /* inout */ time_t & loaded)
+{
+ long stamp = 0;
+ /* check the first line with start time. */
+ int retval = fscanf (dictfile, TIMESTAMP_LINE, &stamp);
+
+ /* empty network dictionary. */
+ if (retval == EOF) {
+ clearNetworkDictionary (context);
+ return FALSE;
+ }
+
+ /* clear network dictionary if start time is changed. */
+ if (start != stamp) {
+ clearNetworkDictionary (context);
+
+ /* reset the time */
+ start = stamp;
+ loaded = stamp - 1;
+ }
+
+ return TRUE;
+}
+
+bool
+LibPinyinBackEnd::forwardNetworkDictionary (FILE * dictfile,
+ /* in */ time_t loaded)
+{
+ char* linebuf = NULL; size_t size = 0; ssize_t read;
+ while ((read = getline (&linebuf, &size, dictfile)) != -1) {
+ if (0 == strlen (linebuf))
+ continue;
+
+ if ('#' != linebuf[0])
+ continue;
+
+ time_t stamp;
+ sscanf (linebuf, TIMESTAMP_LINE, &stamp);
+
+ if (loaded < stamp)
+ break;
+ }
+
+ return TRUE;
+}
+
+bool
+LibPinyinBackEnd::importRestNetworkDictionary (pinyin_context_t * context,
+ FILE * dictfile,
+ /* out */ time_t & loaded)
+{
+ import_iterator_t * iter = pinyin_begin_add_phrases
+ (context, NETWORK_DICTIONARY);
+
+ char* linebuf = NULL; size_t size = 0; ssize_t read;
+ while ((read = getline (&linebuf, &size, dictfile)) != -1) {
+ if (0 == strlen (linebuf))
+ continue;
+
+ if ('#' == linebuf[0]) {
+ sscanf (linebuf, TIMESTAMP_LINE, &loaded);
+ 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);
+
+ g_strfreev (items);
+ }
+
+ pinyin_end_add_phrases (iter);
+ return TRUE;
+}
diff --git a/src/PYLibPinyin.h b/src/PYLibPinyin.h
index a01fdae..aa6dfcc 100644
--- a/src/PYLibPinyin.h
+++ b/src/PYLibPinyin.h
@@ -22,6 +22,7 @@
#define __PY_LIB_PINYIN_H_
#include <memory>
+#include <time.h>
#include <glib.h>
typedef struct _pinyin_context_t pinyin_context_t;
@@ -62,10 +63,27 @@ public:
static void finalize (void);
+protected:
+ bool readNetworkDictionary(pinyin_context_t * context,
+ const char * filename,
+ /* inout */ time_t & start,
+ /* inout */ time_t & loaded);
+
private:
gboolean saveUserDB (void);
static gboolean timeoutCallback (gpointer data);
+ bool clearNetworkDictionary (pinyin_context_t * context);
+ bool checkNetworkDictionary (pinyin_context_t * context,
+ FILE * dictfile,
+ /* inout */ time_t & start,
+ /* inout */ time_t & loaded);
+ bool forwardNetworkDictionary (FILE * dictfile,
+ /* in */ time_t loaded);
+ bool importRestNetworkDictionary (pinyin_context_t * context,
+ FILE * dictfile,
+ /* out */ time_t & loaded);
+
private:
/* libpinyin context */
pinyin_context_t *m_pinyin_context;