add pinyin_guess_sentence_with_prefix
authorPeng Wu <alexepico@gmail.com>
Mon, 5 Mar 2012 08:04:25 +0000 (16:04 +0800)
committerPeng Wu <alexepico@gmail.com>
Mon, 5 Mar 2012 08:04:53 +0000 (16:04 +0800)
src/libpinyin.ver
src/pinyin.cpp
src/pinyin.h

index f4e8932..653158d 100644 (file)
@@ -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;
index bf1345b..5f6ebc6 100644 (file)
@@ -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;
index da2e820..fcd10eb 100644 (file)
@@ -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.