summaryrefslogtreecommitdiffstats
path: root/src/pinyin.cpp
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2012-07-02 11:51:47 +0800
committerPeng Wu <alexepico@gmail.com>2012-07-02 11:51:47 +0800
commitb1563019cb3cad50e93416e310e8ab2cdaa516b2 (patch)
tree8be992fad258b6f2e527a26613ab453b6a48d0ad /src/pinyin.cpp
parent495bb0474cf8dd6515ff6ff749c9fbbf5b537911 (diff)
downloadlibpinyin-b1563019cb3cad50e93416e310e8ab2cdaa516b2.tar.gz
libpinyin-b1563019cb3cad50e93416e310e8ab2cdaa516b2.tar.xz
libpinyin-b1563019cb3cad50e93416e310e8ab2cdaa516b2.zip
enable remove duplicated items
Diffstat (limited to 'src/pinyin.cpp')
-rw-r--r--src/pinyin.cpp46
1 files changed, 34 insertions, 12 deletions
diff --git a/src/pinyin.cpp b/src/pinyin.cpp
index d021e21..4b1b9ed 100644
--- a/src/pinyin.cpp
+++ b/src/pinyin.cpp
@@ -1022,6 +1022,19 @@ static bool _remove_duplicated_items_by_phrase_string
return true;
}
+static bool _free_candidates(CandidateVector candidates) {
+ /* free candidates */
+ for (size_t i = 0; i < candidates->len; ++i) {
+ lookup_candidate_t * candidate = &g_array_index
+ (candidates, lookup_candidate_t, i);
+ g_free(candidate->m_phrase_string);
+ g_free(candidate->m_new_pinyins);
+ }
+ g_array_set_size(candidates, 0);
+
+ return true;
+}
+
bool pinyin_get_candidates(pinyin_instance_t * instance,
size_t offset,
CandidateVector candidates) {
@@ -1029,7 +1042,8 @@ bool pinyin_get_candidates(pinyin_instance_t * instance,
pinyin_context_t * & context = instance->m_context;
pinyin_option_t & options = context->m_options;
ChewingKeyVector & pinyin_keys = instance->m_pinyin_keys;
- g_array_set_size(candidates, 0);
+
+ _free_candidates(candidates);
size_t pinyin_len = pinyin_keys->len - offset;
ssize_t i;
@@ -1097,13 +1111,20 @@ bool pinyin_get_candidates(pinyin_instance_t * instance,
}
g_array_free(items, TRUE);
-
context->m_phrase_index->destroy_ranges(ranges);
-
if (system_gram)
delete system_gram;
if (user_gram)
delete user_gram;
+
+ /* post process to remove duplicated candidates */
+
+ _prepend_sentence_candidate(candidates);
+
+ _compute_phrase_strings_of_items(instance, candidates);
+
+ _remove_duplicated_items_by_phrase_string(instance, candidates);
+
return true;
}
@@ -1348,13 +1369,7 @@ bool pinyin_get_full_pinyin_candidates(pinyin_instance_t * instance,
pinyin_option_t & options = context->m_options;
ChewingKeyVector & pinyin_keys = instance->m_pinyin_keys;
- /* free memory */
- for (size_t i = 0; i < candidates->len; ++i) {
- lookup_candidate_t * candidate = &g_array_index
- (candidates, lookup_candidate_t, i);
- g_free(candidate->m_new_pinyins);
- }
- g_array_set_size(candidates, 0);
+ _free_candidates(candidates);
size_t pinyin_len = pinyin_keys->len - offset;
pinyin_len = std_lite::min((size_t)MAX_PHRASE_LENGTH, pinyin_len);
@@ -1470,13 +1485,20 @@ bool pinyin_get_full_pinyin_candidates(pinyin_instance_t * instance,
}
g_array_free(items, TRUE);
-
context->m_phrase_index->destroy_ranges(ranges);
-
if (system_gram)
delete system_gram;
if (user_gram)
delete user_gram;
+
+ /* post process to remove duplicated candidates */
+
+ _prepend_sentence_candidate(candidates);
+
+ _compute_phrase_strings_of_items(instance, candidates);
+
+ _remove_duplicated_items_by_phrase_string(instance, candidates);
+
return true;
}