summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2012-12-04 11:05:33 +0800
committerPeng Wu <alexepico@gmail.com>2012-12-04 11:05:33 +0800
commitfdefa16aae400be58033fa0be6daa0b9beeb0492 (patch)
treecb2c6fc769bfdae944fbebe125f347cacd11b954
parent01b200662b3706c071658103c1cc73ea307d685f (diff)
downloadlibpinyin-fdefa16aae400be58033fa0be6daa0b9beeb0492.zip
libpinyin-fdefa16aae400be58033fa0be6daa0b9beeb0492.tar.gz
libpinyin-fdefa16aae400be58033fa0be6daa0b9beeb0492.tar.xz
write pinyin_token_*
-rw-r--r--src/libpinyin.ver5
-rw-r--r--src/pinyin.cpp80
-rw-r--r--src/pinyin.h24
3 files changed, 108 insertions, 1 deletions
diff --git a/src/libpinyin.ver b/src/libpinyin.ver
index 3ed126e..19a4c5d 100644
--- a/src/libpinyin.ver
+++ b/src/libpinyin.ver
@@ -38,6 +38,11 @@ LIBPINYIN {
pinyin_reset;
pinyin_get_chewing_string;
pinyin_get_pinyin_string;
+ pinyin_token_get_phrase;
+ pinyin_token_get_n_pronunciation;
+ pinyin_token_get_nth_pronunciation;
+ pinyin_token_get_unigram_frequency;
+ pinyin_token_add_unigram_frequency;
local:
*;
diff --git a/src/pinyin.cpp b/src/pinyin.cpp
index bba9f1a..8bb7a8b 100644
--- a/src/pinyin.cpp
+++ b/src/pinyin.cpp
@@ -721,7 +721,6 @@ bool pinyin_guess_sentence_with_prefix(pinyin_instance_t * instance,
if (i > MAX_PHRASE_LENGTH)
break;
- phrase_token_t token = null_token;
ucs4_t * start = ucs4_str + len_str - i;
PhraseTokens tokens;
@@ -1817,6 +1816,85 @@ bool pinyin_get_pinyin_string(pinyin_instance_t * instance,
return true;
}
+bool pinyin_token_get_phrase(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint * len,
+ gchar ** utf8_str) {
+ *len = 0; *utf8_str = NULL;
+ pinyin_context_t * & context = instance->m_context;
+ PhraseItem item;
+ ucs4_t buffer[MAX_PHRASE_LENGTH];
+
+ int retval = context->m_phrase_index->get_phrase_item(token, item);
+ if (ERROR_OK != retval)
+ return false;
+
+ item.get_phrase_string(buffer);
+ *len = item.get_phrase_length();
+ *utf8_str = g_ucs4_to_utf8(buffer, *len, NULL, NULL, NULL);
+ return true;
+}
+
+bool pinyin_token_get_n_pronunciation(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint * num){
+ *num = 0;
+ pinyin_context_t * & context = instance->m_context;
+ PhraseItem item;
+
+ int retval = context->m_phrase_index->get_phrase_item(token, item);
+ if (ERROR_OK != retval)
+ return false;
+
+ *num = item.get_n_pronunciation();
+ return true;
+}
+
+bool pinyin_token_get_nth_pronunciation(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint nth,
+ ChewingKeyVector keys){
+ g_array_set_size(keys, 0);
+ pinyin_context_t * & context = instance->m_context;
+ PhraseItem item;
+ ChewingKey buffer[MAX_PHRASE_LENGTH];
+ guint32 freq = 0;
+
+ int retval = context->m_phrase_index->get_phrase_item(token, item);
+ if (ERROR_OK != retval)
+ return false;
+
+ item.get_nth_pronunciation(nth, buffer, freq);
+ guint8 len = item.get_phrase_length();
+ g_array_append_vals(keys, buffer, len);
+ return true;
+}
+
+bool pinyin_token_get_unigram_frequency(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint * freq) {
+ *freq = 0;
+ pinyin_context_t * & context = instance->m_context;
+ PhraseItem item;
+
+ int retval = context->m_phrase_index->get_phrase_item(token, item);
+ if (ERROR_OK != retval)
+ return false;
+
+ *freq = item.get_unigram_frequency();
+ return true;
+}
+
+bool pinyin_token_add_unigram_frequency(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint delta){
+ pinyin_context_t * & context = instance->m_context;
+ int retval = context->m_phrase_index->add_unigram_frequency
+ (token, delta);
+ return ERROR_OK == retval;
+}
+
+
/**
* Note: prefix is the text before the pre-edit string.
diff --git a/src/pinyin.h b/src/pinyin.h
index 03f391f..735dbfb 100644
--- a/src/pinyin.h
+++ b/src/pinyin.h
@@ -541,6 +541,30 @@ bool pinyin_get_pinyin_string(pinyin_instance_t * instance,
ChewingKey * key,
gchar ** utf8_str);
+bool pinyin_token_get_phrase(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint * len,
+ gchar ** utf8_str);
+
+bool pinyin_token_get_n_pronunciation(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint * num);
+
+bool pinyin_token_get_nth_pronunciation(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint nth,
+ ChewingKeyVector keys);
+
+bool pinyin_token_get_unigram_frequency(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint * freq);
+
+bool pinyin_token_add_unigram_frequency(pinyin_instance_t * instance,
+ phrase_token_t token,
+ guint delta);
+
+
+
/* hack here. */
typedef ChewingKey PinyinKey;
typedef ChewingKeyRest PinyinKeyPos;