diff options
Diffstat (limited to 'src/lookup/phrase_lookup.cpp')
-rw-r--r-- | src/lookup/phrase_lookup.cpp | 44 |
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]; |