summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2017-01-19 17:20:13 +0800
committerPeng Wu <alexepico@gmail.com>2017-01-19 17:20:13 +0800
commit05e009b90626d989349e9cf60e2af45aac4a4ffe (patch)
tree1b8537a77e145bb65403352061ed096023e5412c /src
parent91de72879019d2461330dbcd16991f1b73d41f82 (diff)
downloadlibpinyin-05e009b90626d989349e9cf60e2af45aac4a4ffe.tar.gz
libpinyin-05e009b90626d989349e9cf60e2af45aac4a4ffe.tar.xz
libpinyin-05e009b90626d989349e9cf60e2af45aac4a4ffe.zip
write trellis_value_less_than function
Diffstat (limited to 'src')
-rw-r--r--src/lookup/phonetic_lookup.h22
-rw-r--r--src/lookup/phonetic_lookup_heap.h23
-rw-r--r--src/lookup/phonetic_lookup_linear.h8
3 files changed, 44 insertions, 9 deletions
diff --git a/src/lookup/phonetic_lookup.h b/src/lookup/phonetic_lookup.h
index 065a31b..fc8272f 100644
--- a/src/lookup/phonetic_lookup.h
+++ b/src/lookup/phonetic_lookup.h
@@ -24,6 +24,7 @@
#include "novel_types.h"
#include <limits.h>
+#include <math.h>
#include "phonetic_key_matrix.h"
#include "ngram.h"
@@ -53,6 +54,27 @@ struct trellis_value_t {
}
};
+template <gint32 nbest>
+static bool inline trellis_value_less_than(const trellis_value_t * exist_item,
+ const trellis_value_t * new_item) {
+ /* shorter sentence */
+ if (exist_item->m_sentence_length > new_item->m_sentence_length ||
+ /* the same length but better possibility */
+ (exist_item->m_sentence_length == new_item->m_sentence_length &&
+ exist_item->m_poss < new_item->m_poss))
+ return true;
+
+ if (nbest > 1) {
+ /* allow longer sentence */
+ if (exist_item->m_current_index == 0 &&
+ exist_item->m_sentence_length == new_item->m_sentence_length + 1 &&
+ exist_item->m_poss < new_item->m_poss)
+ return true;
+ }
+
+ return false;
+}
+
#if 0
struct matrix_value_t {
phrase_token_t m_cur_token;
diff --git a/src/lookup/phonetic_lookup_heap.h b/src/lookup/phonetic_lookup_heap.h
index cd9e2bc..4ceefde 100644
--- a/src/lookup/phonetic_lookup_heap.h
+++ b/src/lookup/phonetic_lookup_heap.h
@@ -21,10 +21,11 @@
#ifndef PHONETIC_LOOKUP_HEAP_H
#define PHONETIC_LOOKUP_HEAP_H
-static inline bool trellis_value_more_than(const trellis_value_t &lhs,
- const trellis_value_t &rhs) {
+template <gint32 nbest>
+static inline bool trellis_value_more_than(const trellis_value_t &exist_item,
+ const trellis_value_t &new_item) {
/* min heap here */
- return lhs.m_poss > rhs.m_poss;
+ return trellis_value_less_than<nbest>(&new_item, &exist_item);
}
template <gint32 nbest>
@@ -58,7 +59,12 @@ public:
if (m_nelem < nbest) {
m_elements[m_nelem] = *item;
m_nelem ++;
- push_heap(begin(), end(), trellis_value_more_than);
+
+ /* mark the first slot of trellis_node. */
+ if (1 == m_nelem)
+ m_elements[0].m_current_index = 0;
+
+ push_heap(begin(), end(), trellis_value_more_than<nbest>);
return true;
}
@@ -67,9 +73,9 @@ public:
/* compare new item */
if (item->m_poss > min->m_poss) {
- pop_heap(begin(), end(), trellis_value_more_than);
+ pop_heap(begin(), end(), trellis_value_more_than<nbest>);
m_elements[m_nelem - 1] = *item;
- push_heap(begin(), end(), trellis_value_more_than);
+ push_heap(begin(), end(), trellis_value_more_than<nbest>);
return true;
}
@@ -97,7 +103,10 @@ public:
/* return true if the item is stored into m_element. */
bool eval_item(const trellis_value_t * item) {
- if (item->m_poss > m_element.m_poss) {
+ /* mark the first slot of trellis_node. */
+ m_element.m_current_index = 0;
+
+ if (compare_tellis_value<nbest>(&m_element, item)) {
m_element = *item;
return true;
}
diff --git a/src/lookup/phonetic_lookup_linear.h b/src/lookup/phonetic_lookup_linear.h
index 9079c24..7bed44e 100644
--- a/src/lookup/phonetic_lookup_linear.h
+++ b/src/lookup/phonetic_lookup_linear.h
@@ -49,18 +49,22 @@ public:
if (m_nelem < nbest) {
m_elements[m_nelem] = *item;
m_nelem ++;
+
+ /* mark the first slot of trellis_node. */
+ if (1 == m_nelem)
+ m_elements[0].m_current_index = 0;
return true;
}
/* find minium item */
trellis_value_t * min = m_elements;
for (gint32 i = 1; i < m_nelem; ++i) {
- if (min->m_poss > m_elements[i].m_poss)
+ if (trellis_value_less_than<nbest>(m_elements + i, min))
min = m_elements + i;
}
/* compare new item */
- if (item->m_poss > min->m_poss) {
+ if (trellis_value_less_than<nbest>(min, item)) {
*min = *item;
return true;
}