summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2010-11-01 16:38:11 +0800
committerPeng Wu <alexepico@gmail.com>2010-11-01 16:38:11 +0800
commit0542734a86d0684345fd7b33fb5428fa6669c396 (patch)
tree816212ac8d3df7ef2b196b68df329eab4822f93c /src
parent195792a51d876ad22a304a4645588efe87e42c09 (diff)
downloadlibpinyin-0542734a86d0684345fd7b33fb5428fa6669c396.tar.gz
libpinyin-0542734a86d0684345fd7b33fb5428fa6669c396.tar.xz
libpinyin-0542734a86d0684345fd7b33fb5428fa6669c396.zip
write final step for phrase lookup
Diffstat (limited to 'src')
-rw-r--r--src/lookup/phrase_lookup.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/lookup/phrase_lookup.cpp b/src/lookup/phrase_lookup.cpp
index 1b9c967..e0f530f 100644
--- a/src/lookup/phrase_lookup.cpp
+++ b/src/lookup/phrase_lookup.cpp
@@ -214,6 +214,50 @@ bool PhraseLookup::save_next_step(int next_step_pos, lookup_value_t * cur_value,
}
}
+bool PhraseLookup::final_step(MatchResults & results ){
+ //reset results
+ g_array_set_size(results, m_steps_content->len);
+ for ( size_t i = 0; i < m_steps_content->len; ++i ){
+ phrase_token_t * token = &g_array_index(results, phrase_token_t, i);
+ *token = null_token;
+ }
+
+ //find max element
+ size_t last_step_pos = m_steps_content->len - 1;
+ GArray * last_step_array = (GArray *) g_ptr_array_index(m_steps_content, last_step_pos);
+ if ( last_step_array->len == 0 )
+ return false;
+
+ lookup_value_t * max_value = &g_array_index(last_step_array, lookup_value_t, 0);
+ for ( size_t i = 1; i < last_step_array->len; ++i ){
+ lookup_value_t * cur_value = &g_array_index(last_step_array, lookup_value_t, i);
+ if ( cur_value->m_poss > max_value->m_poss )
+ max_value = cur_value;
+ }
+
+ //backtracing
+ while( true ){
+ int cur_step_pos = max_value->m_last_step;
+ if ( -1 = cur_step_pos )
+ break;
+
+ phrase_token_t * token = &g_array_index(results, phrase_token_t, cur_step_pos);
+ *token = max_value->m_handles[1];
+
+ phrase_token_t last_token = max_value->m_handles[0];
+ GHashTable * lookup_step_index = (GHashTable *) g_ptr_array_index(m_steps_index, cur_step_pos);
+ gpointer key, value;
+ gboolean result = g_hash_table_lookup_extended(lookup_step_index, GUINT_TO_POINTER(last_token), &key, &value);
+ if ( !result )
+ return false;
+ GArray * lookup_step_content = (GArray *) g_ptr_array_index(m_steps_content, cur_step_pos);
+ max_value = &g_array_index(lookup_step_content, lookup_value_t, GPOINTER_TO_UINT(value));
+ }
+
+ //no need to reverse the result
+ return true;
+}
+
bool PhraseLookup::convert_to_utf8(phrase_token_t token, /* out */ char * & phrase){
m_phrase_index->get_phrase_item(token, m_cache_phrase_item);
utf16_t buffer[MAX_PHRASE_LENGTH];