summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2018-01-29 14:50:29 +0800
committerPeng Wu <alexepico@gmail.com>2018-01-29 14:50:29 +0800
commit813cedc202276fa6d4127446c4688d8a706c5ff5 (patch)
tree94a231dbef875d794c8ab7ff5153c8d02366d987
parentf8c1d027bd98ba452a4cc0210d80a692077e3f47 (diff)
downloadlibpinyin-813cedc202276fa6d4127446c4688d8a706c5ff5.zip
libpinyin-813cedc202276fa6d4127446c4688d8a706c5ff5.tar.gz
libpinyin-813cedc202276fa6d4127446c4688d8a706c5ff5.tar.xz
intro nstore parameter
-rw-r--r--src/lookup/phonetic_lookup.h50
-rw-r--r--src/lookup/phonetic_lookup_heap.h18
-rw-r--r--src/lookup/phonetic_lookup_linear.h10
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 <gint32 nbest>
+template <gint32 nstore>
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<gint32 nbest>
+template<gint32 nstore>
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<nbest>);
+ std_lite::make_heap(begin, end, trellis_value_less_than<nstore>);
while (end != begin) {
trellis_value_t * one = *begin;
g_ptr_array_add(topresults, one);
- std_lite::pop_heap(begin, end, trellis_value_less_than<nbest>);
+ std_lite::pop_heap(begin, end, trellis_value_less_than<nstore>);
--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 <gint32 nbest>
+template <gint32 nstore, gint32 nbest>
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<nbest>));
+ g_ptr_array_index(m_steps_content, i) = g_array_new(FALSE, FALSE, sizeof(trellis_node<nstore>));
}
return true;
@@ -249,7 +249,7 @@ public:
trellis_value_t initial_value(log(1.f));
initial_value.m_handles[1] = token;
- trellis_node<nbest> initial_node;
+ trellis_node<nstore> 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<nbest> * node = &g_array_index
- (step, trellis_node<nbest>, i);
+ trellis_node<nstore> * node = &g_array_index
+ (step, trellis_node<nstore>, 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<nbest> node;
+ trellis_node<nstore> 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<nbest> * node = &g_array_index
- (step_content, trellis_node<nbest>, node_index);
+ trellis_node<nstore> * node = &g_array_index
+ (step_content, trellis_node<nstore>, 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>(nbest, tails, candidates);
+ get_top_results<nstore>(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<nbest> * node = &g_array_index
- (step_content, trellis_node<nbest>, node_index);
+ trellis_node<nstore> * node = &g_array_index
+ (step_content, trellis_node<nstore>, node_index);
if (sub_index >= node->length())
return false;
@@ -365,8 +365,8 @@ public:
}
};
-template <gint32 nbest>
-bool extract_result(const ForwardPhoneticTrellis<nbest> * trellis,
+template <gint32 nstore, gint32 nbest>
+bool extract_result(const ForwardPhoneticTrellis<nstore, nbest> * trellis,
const trellis_value_t * tail,
/* out */ MatchResult & result) {
/* reset result */
@@ -396,7 +396,7 @@ bool extract_result(const ForwardPhoneticTrellis<nbest> * trellis,
}
#if 0
-template <gint32 nbest>
+template <gint32 nstore>
class BackwardPhoneticMatrix {
private:
/* Array of matrix_step */
@@ -404,7 +404,7 @@ private:
public:
/* set tail node */
- bool set_tail(const matrix_step<nbest> * tail);
+ bool set_tail(const matrix_step<nstore> * tail);
/* back trace */
/* always assume/assert matrix_step.eval_item(...) return true? */
bool back_trace(const ForwardPhoneticTrellis * trellis);
@@ -511,7 +511,7 @@ public:
}
};
-template <gint32 nbest>
+template <gint32 nstore, gint32 nbest>
class PhoneticLookup {
private:
const gfloat bigram_lambda;
@@ -523,7 +523,7 @@ private:
SingleGram m_merged_single_gram;
protected:
- ForwardPhoneticTrellis<nbest> m_trellis;
+ ForwardPhoneticTrellis<nstore, nbest> 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<nbest>(nbeam, topresults, candidates);
+ get_top_results<nstore>(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<nbest>(&m_trellis, tail, result));
+ assert(extract_result<nstore>(&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 <gint32 nbest>
+template <gint32 nstore>
static inline bool trellis_value_comp(const trellis_value_t &lhs,
const trellis_value_t &rhs) {
/* min heap here */
- return trellis_value_less_than<nbest>(&lhs, &rhs);
+ return trellis_value_less_than<nstore>(&lhs, &rhs);
}
-template <gint32 nbest>
+template <gint32 nstore>
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<nbest>);
+ std_lite::push_heap(m_elements, m_elements + m_nelem, trellis_value_comp<nstore>);
return true;
}
@@ -70,10 +70,10 @@ public:
trellis_value_t * min = m_elements;
/* compare new item */
- if (trellis_value_less_than<nbest>(min, item)) {
- std_lite::pop_heap(m_elements, m_elements + m_nelem, trellis_value_comp<nbest>);
+ if (trellis_value_less_than<nstore>(min, item)) {
+ std_lite::pop_heap(m_elements, m_elements + m_nelem, trellis_value_comp<nstore>);
m_elements[m_nelem - 1] = *item;
- std_lite::push_heap(m_elements, m_elements + m_nelem, trellis_value_comp<nbest>);
+ std_lite::push_heap(m_elements, m_elements + m_nelem, trellis_value_comp<nstore>);
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 <gint32 nbest>
+template <gint32 nstore>
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<nbest>(m_elements + i, min))
+ if (trellis_value_less_than<nstore>(m_elements + i, min))
min = m_elements + i;
}
/* compare new item */
- if (trellis_value_less_than<nbest>(min, item)) {
+ if (trellis_value_less_than<nstore>(min, item)) {
*min = *item;
return true;
}