summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2012-03-05 16:04:25 +0800
committerPeng Wu <alexepico@gmail.com>2012-03-05 16:04:53 +0800
commitda7323a57cb89ca385676b9566f5b70e4a42e1a3 (patch)
tree68ab6f84aac99a9fcd608e15f1daed8d3f56e137 /src
parent536b587a1a5c2aa273026890cec64881e58e5cc5 (diff)
downloadlibpinyin-da7323a57cb89ca385676b9566f5b70e4a42e1a3.tar.gz
libpinyin-da7323a57cb89ca385676b9566f5b70e4a42e1a3.tar.xz
libpinyin-da7323a57cb89ca385676b9566f5b70e4a42e1a3.zip
add pinyin_guess_sentence_with_prefix
Diffstat (limited to 'src')
-rw-r--r--src/libpinyin.ver1
-rw-r--r--src/pinyin.cpp35
-rw-r--r--src/pinyin.h12
3 files changed, 48 insertions, 0 deletions
diff --git a/src/libpinyin.ver b/src/libpinyin.ver
index f4e8932..653158d 100644
--- a/src/libpinyin.ver
+++ b/src/libpinyin.ver
@@ -9,6 +9,7 @@ LIBPINYIN {
pinyin_alloc_instance;
pinyin_free_instance;
pinyin_guess_sentence;
+ pinyin_guess_sentence_with_prefix;
pinyin_phrase_segment;
pinyin_get_sentence;
pinyin_parse_full_pinyin;
diff --git a/src/pinyin.cpp b/src/pinyin.cpp
index bf1345b..5f6ebc6 100644
--- a/src/pinyin.cpp
+++ b/src/pinyin.cpp
@@ -330,6 +330,41 @@ bool pinyin_guess_sentence(pinyin_instance_t * instance){
return retval;
}
+bool pinyin_guess_sentence_with_prefix(pinyin_instance_t * instance,
+ const char * prefix){
+ pinyin_context_t * & context = instance->m_context;
+
+ g_array_set_size(instance->m_prefixes, 0);
+ g_array_append_val(instance->m_prefixes, sentence_start);
+
+ glong written = 0;
+ ucs4_t * ucs4_str = g_utf8_to_ucs4(prefix, -1, NULL, &written, NULL);
+
+ if (ucs4_str && written) {
+ /* add prefixes. */
+ for (ssize_t i = 1; i <= written; ++i) {
+ if (i > MAX_PHRASE_LENGTH)
+ break;
+
+ phrase_token_t token = null_token;
+ ucs4_t * start = ucs4_str + written - i;
+ int result = context->m_phrase_table->search(i, start, token);
+ if (result & SEARCH_OK)
+ g_array_append_val(instance->m_prefixes, token);
+ }
+ }
+ g_free(ucs4_str);
+
+ pinyin_update_constraints(instance);
+ bool retval = context->m_pinyin_lookup->get_best_match
+ (instance->m_prefixes,
+ instance->m_pinyin_keys,
+ instance->m_constraints,
+ instance->m_match_results);
+
+ return retval;
+}
+
bool pinyin_phrase_segment(pinyin_instance_t * instance,
const char * sentence){
pinyin_context_t * & context = instance->m_context;
diff --git a/src/pinyin.h b/src/pinyin.h
index da2e820..fcd10eb 100644
--- a/src/pinyin.h
+++ b/src/pinyin.h
@@ -143,6 +143,18 @@ void pinyin_free_instance(pinyin_instance_t * instance);
bool pinyin_guess_sentence(pinyin_instance_t * instance);
/**
+ * pinyin_guess_sentence_with_prefix:
+ * @instance: the pinyin instance.
+ * @prefix: the prefix before the sentence.
+ * @returns: whether the sentence are guessed successfully.
+ *
+ * Guess a sentence from the saved pinyin keys with a prefix.
+ *
+ */
+bool pinyin_guess_sentence_with_prefix(pinyin_instance_t * instance,
+ const char * prefix);
+
+/**
* pinyin_phrase_segment:
* @instance: the pinyin instance.
* @sentence: the utf-8 sentence to be segmented.