From 813cedc202276fa6d4127446c4688d8a706c5ff5 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Mon, 29 Jan 2018 14:50:29 +0800 Subject: intro nstore parameter --- src/lookup/phonetic_lookup.h | 50 +++++++++++++++++++------------------ src/lookup/phonetic_lookup_heap.h | 18 ++++++------- src/lookup/phonetic_lookup_linear.h | 10 ++++---- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/lookup/phonetic_lookup.h b/src/lookup/phonetic_lookup.h index a8a1def..78fac1b 100644 --- a/src/lookup/phonetic_lookup.h +++ b/src/lookup/phonetic_lookup.h @@ -61,11 +61,11 @@ struct trellis_value_t { } }; -template +template static bool inline trellis_value_less_than(const trellis_value_t * item_lhs, const trellis_value_t * item_rhs) { #if 1 - if (nbest > 1) { + if (nstore > 1) { /* allow longer sentence */ if (item_lhs->m_sentence_length + 1 == item_rhs->m_sentence_length && item_lhs->m_poss + LONG_SENTENCE_PENALTY < item_rhs->m_poss) @@ -138,7 +138,7 @@ struct trellis_constraint_t { /* use maximum heap to get the topest results. */ -template +template bool get_top_results(size_t num, /* out */ GPtrArray * topresults, /* in */ GPtrArray * candidates) { @@ -152,13 +152,13 @@ bool get_top_results(size_t num, trellis_value_t ** end = (trellis_value_t **) &g_ptr_array_index(candidates, candidates->len); - std_lite::make_heap(begin, end, trellis_value_less_than); + std_lite::make_heap(begin, end, trellis_value_less_than); while (end != begin) { trellis_value_t * one = *begin; g_ptr_array_add(topresults, one); - std_lite::pop_heap(begin, end, trellis_value_less_than); + std_lite::pop_heap(begin, end, trellis_value_less_than); --end; if (topresults->len >= num) @@ -176,7 +176,7 @@ static gint trellis_value_compare(const trellis_value_t ** lhs, return -((*lhs)->m_poss - (*rhs)->m_poss); } -template +template class ForwardPhoneticTrellis { private: /* Array of LookupStepIndex */ @@ -233,7 +233,7 @@ public: /* initialize m_steps_index */ g_ptr_array_index(m_steps_index, i) = g_hash_table_new(g_direct_hash, g_direct_equal); /* initialize m_steps_content */ - g_ptr_array_index(m_steps_content, i) = g_array_new(FALSE, FALSE, sizeof(trellis_node)); + g_ptr_array_index(m_steps_content, i) = g_array_new(FALSE, FALSE, sizeof(trellis_node)); } return true; @@ -249,7 +249,7 @@ public: trellis_value_t initial_value(log(1.f)); initial_value.m_handles[1] = token; - trellis_node initial_node; + trellis_node initial_node; assert(initial_node.eval_item(&initial_value)); LookupStepContent initial_step_content = (LookupStepContent) @@ -279,8 +279,8 @@ public: return false; for (size_t i = 0; i < step->len; ++i) { - trellis_node * node = &g_array_index - (step, trellis_node, i); + trellis_node * node = &g_array_index + (step, trellis_node, i); // only initialized in the get_candidates method. node->number(); @@ -307,7 +307,7 @@ public: (step_index, GUINT_TO_POINTER(token), &key, &value); if (!lookup_result) { - trellis_node node; + trellis_node node; assert(node.eval_item(candidate)); g_array_append_val(step_content, node); @@ -315,8 +315,8 @@ public: return true; } else { size_t node_index = GPOINTER_TO_UINT(value); - trellis_node * node = &g_array_index - (step_content, trellis_node, node_index); + trellis_node * node = &g_array_index + (step_content, trellis_node, node_index); return node->eval_item(candidate); } @@ -331,7 +331,7 @@ public: GPtrArray * candidates = g_ptr_array_new(); get_candidates(tail_index, candidates); - get_top_results(nbest, tails, candidates); + get_top_results(nbest, tails, candidates); g_ptr_array_sort(tails, (GCompareFunc)trellis_value_compare); @@ -353,8 +353,8 @@ public: return false; size_t node_index = GPOINTER_TO_UINT(value); - trellis_node * node = &g_array_index - (step_content, trellis_node, node_index); + trellis_node * node = &g_array_index + (step_content, trellis_node, node_index); if (sub_index >= node->length()) return false; @@ -365,8 +365,8 @@ public: } }; -template -bool extract_result(const ForwardPhoneticTrellis * trellis, +template +bool extract_result(const ForwardPhoneticTrellis * trellis, const trellis_value_t * tail, /* out */ MatchResult & result) { /* reset result */ @@ -396,7 +396,7 @@ bool extract_result(const ForwardPhoneticTrellis * trellis, } #if 0 -template +template class BackwardPhoneticMatrix { private: /* Array of matrix_step */ @@ -404,7 +404,7 @@ private: public: /* set tail node */ - bool set_tail(const matrix_step * tail); + bool set_tail(const matrix_step * tail); /* back trace */ /* always assume/assert matrix_step.eval_item(...) return true? */ bool back_trace(const ForwardPhoneticTrellis * trellis); @@ -511,7 +511,7 @@ public: } }; -template +template class PhoneticLookup { private: const gfloat bigram_lambda; @@ -523,7 +523,7 @@ private: SingleGram m_merged_single_gram; protected: - ForwardPhoneticTrellis m_trellis; + ForwardPhoneticTrellis m_trellis; protected: /* saved varibles */ @@ -711,6 +711,8 @@ public: : bigram_lambda(lambda), unigram_lambda(1. - lambda) { + assert(nstore <= nbest); + /* store the pointer. */ m_pinyin_table = pinyin_table; m_phrase_index = phrase_index; @@ -765,7 +767,7 @@ public: continue; m_trellis.get_candidates(i, candidates); - get_top_results(nbeam, topresults, candidates); + get_top_results(nbeam, topresults, candidates); if (0 == topresults->len) continue; @@ -828,7 +830,7 @@ public: const trellis_value_t * tail = (const trellis_value_t *) g_ptr_array_index(tails, i); - assert(extract_result(&m_trellis, tail, result)); + assert(extract_result(&m_trellis, tail, result)); results->add_result(result); } diff --git a/src/lookup/phonetic_lookup_heap.h b/src/lookup/phonetic_lookup_heap.h index 73eac28..310f2b4 100644 --- a/src/lookup/phonetic_lookup_heap.h +++ b/src/lookup/phonetic_lookup_heap.h @@ -21,19 +21,19 @@ #ifndef PHONETIC_LOOKUP_HEAP_H #define PHONETIC_LOOKUP_HEAP_H -template +template static inline bool trellis_value_comp(const trellis_value_t &lhs, const trellis_value_t &rhs) { /* min heap here */ - return trellis_value_less_than(&lhs, &rhs); + return trellis_value_less_than(&lhs, &rhs); } -template +template struct trellis_node { private: gint32 m_nelem; /* invariant: min heap */ - trellis_value_t m_elements[nbest]; + trellis_value_t m_elements[nstore]; public: trellis_node(){ @@ -58,11 +58,11 @@ public: /* min heap here. */ /* still have space */ - if (m_nelem < nbest) { + if (m_nelem < nstore) { m_elements[m_nelem] = *item; m_nelem ++; /* always push heap. */ - std_lite::push_heap(m_elements, m_elements + m_nelem, trellis_value_comp); + std_lite::push_heap(m_elements, m_elements + m_nelem, trellis_value_comp); return true; } @@ -70,10 +70,10 @@ public: trellis_value_t * min = m_elements; /* compare new item */ - if (trellis_value_less_than(min, item)) { - std_lite::pop_heap(m_elements, m_elements + m_nelem, trellis_value_comp); + if (trellis_value_less_than(min, item)) { + std_lite::pop_heap(m_elements, m_elements + m_nelem, trellis_value_comp); m_elements[m_nelem - 1] = *item; - std_lite::push_heap(m_elements, m_elements + m_nelem, trellis_value_comp); + std_lite::push_heap(m_elements, m_elements + m_nelem, trellis_value_comp); return true; } diff --git a/src/lookup/phonetic_lookup_linear.h b/src/lookup/phonetic_lookup_linear.h index 5a9f07b..82cbc56 100644 --- a/src/lookup/phonetic_lookup_linear.h +++ b/src/lookup/phonetic_lookup_linear.h @@ -21,11 +21,11 @@ #ifndef PHONETIC_LOOKUP_LINEAR_H #define PHONETIC_LOOKUP_LINEAR_H -template +template struct trellis_node { private: gint32 m_nelem; - trellis_value_t m_elements[nbest]; + trellis_value_t m_elements[nstore]; public: trellis_node(){ @@ -48,7 +48,7 @@ public: bool eval_item(const trellis_value_t * item) { /* still have space */ - if (m_nelem < nbest) { + if (m_nelem < nstore) { m_elements[m_nelem] = *item; m_nelem ++; return true; @@ -57,12 +57,12 @@ public: /* find minium item */ trellis_value_t * min = m_elements; for (gint32 i = 1; i < m_nelem; ++i) { - if (trellis_value_less_than(m_elements + i, min)) + if (trellis_value_less_than(m_elements + i, min)) min = m_elements + i; } /* compare new item */ - if (trellis_value_less_than(min, item)) { + if (trellis_value_less_than(min, item)) { *min = *item; return true; } -- cgit