summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2017-01-19 15:35:21 +0800
committerPeng Wu <alexepico@gmail.com>2017-01-19 15:35:21 +0800
commit0145f45ff7ab413add71631ba95dc4c6d1cb9e40 (patch)
tree7ab299eb475b909f11282530341b10c42d0cd20b /src
parentad0987d91b922fa546ff9663a3ca70d4fb39057b (diff)
downloadlibpinyin-0145f45ff7ab413add71631ba95dc4c6d1cb9e40.tar.gz
libpinyin-0145f45ff7ab413add71631ba95dc4c6d1cb9e40.tar.xz
libpinyin-0145f45ff7ab413add71631ba95dc4c6d1cb9e40.zip
write insert_candidate and get_tails method
Diffstat (limited to 'src')
-rw-r--r--src/lookup/phonetic_lookup.cpp7
-rw-r--r--src/lookup/phonetic_lookup.h45
2 files changed, 46 insertions, 6 deletions
diff --git a/src/lookup/phonetic_lookup.cpp b/src/lookup/phonetic_lookup.cpp
index 4ea8b72..8f48755 100644
--- a/src/lookup/phonetic_lookup.cpp
+++ b/src/lookup/phonetic_lookup.cpp
@@ -67,8 +67,9 @@ static bool trellis_value_less_than(trellis_value_t * lhs,
}
/* use maximum heap to get the topest results. */
-static bool get_top_results(/* out */ GPtrArray * topresults,
- /* in */ GPtrArray * candidates) {
+bool get_top_results(size_t num,
+ /* out */ GPtrArray * topresults,
+ /* in */ GPtrArray * candidates) {
g_ptr_array_set_size(topresults, 0);
if (0 == candidates->len)
@@ -88,7 +89,7 @@ static bool get_top_results(/* out */ GPtrArray * topresults,
std_lite::pop_heap(begin, end, trellis_value_less_than);
--end;
- if (topresults->len >= nbeam)
+ if (topresults->len >= num)
break;
}
diff --git a/src/lookup/phonetic_lookup.h b/src/lookup/phonetic_lookup.h
index 3593868..b6bceec 100644
--- a/src/lookup/phonetic_lookup.h
+++ b/src/lookup/phonetic_lookup.h
@@ -108,6 +108,10 @@ typedef GHashTable * LookupStepIndex;
/* Array of trellis_node */
typedef GArray * LookupStepContent;
+bool get_top_results(size_t num,
+ /* out */ GPtrArray * topresults,
+ /* in */ GPtrArray * candidates);
+
template <gint32 nbest>
class ForwardPhoneticTrellis {
private:
@@ -221,10 +225,45 @@ public:
/* insert candidate */
bool insert_candidate(gint32 index, lookup_key_t token,
- const trellis_value_t * candidate);
+ const trellis_value_t * candidate) {
+ LookupStepIndex step_index = (LookupStepIndex) g_ptr_array_index(m_steps_index, index);
+ LookupStepContent step_content = (LookupStepContent) g_ptr_array_index(m_steps_content, index);
+
+ gpointer key = NULL, value = NULL;
+ gboolean lookup_result = g_hash_table_lookup_extended
+ (step_index, GUINT_TO_POINTER(token), &key, &value);
+
+ if (!lookup_result) {
+ trellis_node<nbest> node;
+ assert(node.eval_item(candidate));
+
+ g_array_append_val(step_content, node);
+ g_hash_table_insert(step_index, GUINT_TO_POINTER(token), GUINT_TO_POINTER(step_content->len - 1));
+ 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);
+
+ return node->eval_item(candidate);
+ }
+
+ assert(FALSE);
+ }
+
/* get tails */
- /* Array of trellis_value_t */
- bool get_tails(/* out */ GArray * tails) const;
+ /* Array of trellis_value_t * */
+ bool get_tails(/* out */ GPtrArray * tails) const {
+ assert(m_steps_index->len == m_steps_content->len);
+ gint32 tail_index = m_steps_index->len - 1;
+
+ GPtrArray * candidates = g_ptr_array_new();
+ get_candidates(tail_index, candidates);
+ get_top_results(nbest, tails, candidates);
+
+ g_ptr_array_free(candidates, TRUE);
+ return true;
+ }
/* get candidate */
bool get_candidate(gint32 index, lookup_key_t token, gint32 sub_index,