summaryrefslogtreecommitdiffstats
path: root/src/lookup
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2017-01-19 15:14:24 +0800
committerPeng Wu <alexepico@gmail.com>2017-01-19 15:14:24 +0800
commitad0987d91b922fa546ff9663a3ca70d4fb39057b (patch)
tree34f1ce635b427d9022ce5e6e0fe3e2d4c664d770 /src/lookup
parentcabb8d9ea34188521fe6aa124136b764041ee958 (diff)
downloadlibpinyin-ad0987d91b922fa546ff9663a3ca70d4fb39057b.tar.gz
libpinyin-ad0987d91b922fa546ff9663a3ca70d4fb39057b.tar.xz
libpinyin-ad0987d91b922fa546ff9663a3ca70d4fb39057b.zip
write get_candidate and get_constraint method
Diffstat (limited to 'src/lookup')
-rw-r--r--src/lookup/phonetic_lookup.h46
1 files changed, 39 insertions, 7 deletions
diff --git a/src/lookup/phonetic_lookup.h b/src/lookup/phonetic_lookup.h
index 35c0407..3593868 100644
--- a/src/lookup/phonetic_lookup.h
+++ b/src/lookup/phonetic_lookup.h
@@ -132,15 +132,15 @@ public:
bool clear() {
/* clear m_steps_index */
for ( size_t i = 0; i < m_steps_index->len; ++i){
- GHashTable * table = (GHashTable *) g_ptr_array_index(m_steps_index, i);
- g_hash_table_destroy(table);
+ LookupStepIndex step_index = (LookupStepIndex) g_ptr_array_index(m_steps_index, i);
+ g_hash_table_destroy(step_index);
g_ptr_array_index(m_steps_index, i) = NULL;
}
/* clear m_steps_content */
for ( size_t i = 0; i < m_steps_content->len; ++i){
- GArray * array = (GArray *) g_ptr_array_index(m_steps_content, i);
- g_array_free(array, TRUE);
+ LookupStepContent step_content = (LookupStepContent) g_ptr_array_index(m_steps_content, i);
+ g_array_free(step_content, TRUE);
g_ptr_array_index(m_steps_content, i) = NULL;
}
@@ -220,14 +220,36 @@ public:
}
/* insert candidate */
- bool insert_candidate(gint32 index, phrase_token_t token,
+ bool insert_candidate(gint32 index, lookup_key_t token,
const trellis_value_t * candidate);
/* get tails */
/* Array of trellis_value_t */
bool get_tails(/* out */ GArray * tails) const;
+
/* get candidate */
- bool get_candidate(gint32 index, phrase_token_t token,
- gint32 sub_index, trellis_value_t * candidate) const;
+ bool get_candidate(gint32 index, lookup_key_t token, gint32 sub_index,
+ const trellis_value_t * & candidate) const {
+ 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)
+ return false;
+
+ size_t node_index = GPOINTER_TO_UINT(value);
+ trellis_node<nbest> * node = &g_array_index
+ (step_content, trellis_node<nbest>, node_index);
+
+ if (sub_index >= node->length())
+ return false;
+
+ candidate = node->begin() + sub_index;
+
+ return true;
+ }
};
template <gint32 nbest>
@@ -265,6 +287,16 @@ public:
int add_constraint(size_t start, size_t end, phrase_token_t token);
bool clear_constraint(size_t index);
bool validate_constraint(PhoneticKeyMatrix * matrix);
+
+ bool get_constraint(size_t index,
+ const trellis_constraint_t * & constraint) const {
+ if (index >= m_constraints->len)
+ return false;
+
+ constraint = &g_array_index(m_constraints, trellis_constraint_t, index);
+
+ return true;
+ }
};