summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2011-09-06 11:34:04 +0800
committerPeng Wu <alexepico@gmail.com>2011-09-06 11:34:04 +0800
commite2f06b37b9e2f8c1c1a9428c1619af4a8c5566c8 (patch)
tree7b47ed0cd159f5d61ac0e4b1d33d33bb5324971e
parentdbdfd3b658f0f723513fd08415d08554d125f302 (diff)
downloadlibpinyin-e2f06b37b9e2f8c1c1a9428c1619af4a8c5566c8.zip
libpinyin-e2f06b37b9e2f8c1c1a9428c1619af4a8c5566c8.tar.gz
libpinyin-e2f06b37b9e2f8c1c1a9428c1619af4a8c5566c8.tar.xz
refine pinyin apis
-rw-r--r--src/libpinyin.ver2
-rw-r--r--src/pinyin.cpp61
-rw-r--r--src/pinyin.h14
-rw-r--r--tests/test_phrase.cpp2
-rw-r--r--tests/test_pinyin.cpp19
5 files changed, 63 insertions, 35 deletions
diff --git a/src/libpinyin.ver b/src/libpinyin.ver
index 78432c5..0bc8c09 100644
--- a/src/libpinyin.ver
+++ b/src/libpinyin.ver
@@ -19,6 +19,8 @@ LIBPINYIN {
pinyin_update_constraints;
pinyin_train;
pinyin_translate_token;
+ pinyin_alloc_auxiliary_arrays;
+ pinyin_free_auxiliary_arrays;
local:
*;
diff --git a/src/pinyin.cpp b/src/pinyin.cpp
index 054ef13..7bec3cb 100644
--- a/src/pinyin.cpp
+++ b/src/pinyin.cpp
@@ -18,11 +18,6 @@ struct _pinyin_context_t{
PinyinLookup * m_pinyin_lookup;
PhraseLookup * m_phrase_lookup;
-#if 0
- PinyinKeyVector m_pinyin_keys;
- MatchResults m_match_results;
- CandidateConstraints m_constraints;
-#endif
char * m_system_dir;
char * m_user_dir;
@@ -85,14 +80,6 @@ pinyin_context_t * pinyin_init(const char * systemdir, const char * userdir){
(context->m_phrase_table, context->m_phrase_index,
context->m_system_bigram, context->m_user_bigram);
-#if 0
- context->m_pinyin_keys = g_array_new(FALSE, FALSE, sizeof(PinyinKey));
- context->m_match_results = g_array_new
- (FALSE, FALSE, sizeof(phrase_token_t));
- context->m_constraints = g_array_new
- (FALSE, FALSE, sizeof(lookup_constraint_t));
-#endif
-
return context;
}
@@ -107,19 +94,39 @@ void pinyin_fini(pinyin_context_t * context){
delete context->m_pinyin_lookup;
delete context->m_phrase_lookup;
-
-#if 0
- g_array_free(context->m_pinyin_keys, true);
- g_array_free(context->m_match_results, true);
- g_array_free(context->m_constraints, true);
-#endif
-
g_free(context->m_system_dir);
g_free(context->m_user_dir);
delete context;
}
+bool pinyin_alloc_auxiliary_arrays(pinyin_context_t * context,
+ PinyinKeyVector * pinyin_keys,
+ CandidateConstraints * constraints,
+ MatchResults * match_results){
+
+ *pinyin_keys = g_array_new(FALSE, FALSE, sizeof(PinyinKey));
+ *constraints = g_array_new(FALSE, FALSE, sizeof(lookup_constraint_t));
+ *match_results = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
+
+ return true;
+}
+
+bool pinyin_free_auxiliary_arrays(pinyin_context_t * context,
+ PinyinKeyVector * pinyin_keys,
+ CandidateConstraints * constraints,
+ MatchResults * match_results){
+ g_array_free(*pinyin_keys, true);
+ *pinyin_keys = NULL;
+ g_array_free(*constraints, true);
+ *constraints = NULL;
+ g_array_free(*match_results, true);
+ *match_results = NULL;
+
+ return true;
+}
+
+
/* copy from custom to context->m_custom. */
bool pinyin_set_options(pinyin_context_t * context,
PinyinCustomSettings * custom){
@@ -415,12 +422,14 @@ bool pinyin_save(pinyin_context_t * context){
return true;
}
-bool pinyin_reset(pinyin_context_t * context){
-#if 0
- g_array_set_size(context->m_pinyin_keys, 0);
- g_array_set_size(context->m_match_results, 0);
- g_array_set_size(context->m_constraints, 0);
-#endif
+bool pinyin_reset(pinyin_context_t * context,
+ PinyinKeyVector pinyin_keys,
+ CandidateConstraints constraints,
+ MatchResults match_results){
+
+ g_array_set_size(pinyin_keys, 0);
+ g_array_set_size(constraints, 0);
+ g_array_set_size(match_results, 0);
/* TODO: to be implemented. */
return true;
diff --git a/src/pinyin.h b/src/pinyin.h
index 57c2dba..0c612b7 100644
--- a/src/pinyin.h
+++ b/src/pinyin.h
@@ -37,6 +37,15 @@ typedef struct _pinyin_context_t pinyin_context_t;
pinyin_context_t * pinyin_init(const char * systemdir, const char * userdir);
void pinyin_fini(pinyin_context_t * context);
+bool pinyin_alloc_auxiliary_arrays(pinyin_context_t * context,
+ PinyinKeyVector * pinyin_keys,
+ CandidateConstraints * constraints,
+ MatchResults * match_results);
+bool pinyin_free_auxiliary_arrays(pinyin_context_t * context,
+ PinyinKeyVector * pinyin_keys,
+ CandidateConstraints * constraints,
+ MatchResults * match_results);
+
bool pinyin_set_options(pinyin_context_t * context,
PinyinCustomSettings * custom);
@@ -94,7 +103,10 @@ bool pinyin_train(pinyin_context_t * context,
CandidateConstraints constraints,
MatchResults match_results);
bool pinyin_save(pinyin_context_t * context);
-bool pinyin_reset(pinyin_context_t * context);
+bool pinyin_reset(pinyin_context_t * context,
+ PinyinKeyVector pinyin_keys,
+ CandidateConstraints constraints,
+ MatchResults match_results);
}
diff --git a/tests/test_phrase.cpp b/tests/test_phrase.cpp
index 73aa2de..0137e08 100644
--- a/tests/test_phrase.cpp
+++ b/tests/test_phrase.cpp
@@ -56,8 +56,6 @@ int main(int argc, char * argv[]){
}
printf("\n");
- pinyin_train(context);
- pinyin_reset(context);
pinyin_save(context);
}
diff --git a/tests/test_pinyin.cpp b/tests/test_pinyin.cpp
index fe37ba5..ce00f14 100644
--- a/tests/test_pinyin.cpp
+++ b/tests/test_pinyin.cpp
@@ -27,8 +27,12 @@ int main(int argc, char * argv[]){
pinyin_context_t * context =
pinyin_init("../data", "../data");
- PinyinKeyVector pinyin_keys =
- g_array_new(FALSE, FALSE, sizeof(PinyinKey));
+ PinyinKeyVector pinyin_keys = NULL;
+ CandidateConstraints constraints = NULL;
+ MatchResults match_results = NULL;
+
+ pinyin_alloc_auxiliary_arrays
+ (context, &pinyin_keys, &constraints, &match_results);
char* linebuf = NULL;
size_t size = 0;
@@ -42,17 +46,20 @@ int main(int argc, char * argv[]){
break;
pinyin_parse_more_fulls(context, linebuf, pinyin_keys);
- pinyin_set_pinyin_keys(context, pinyin_keys);
char * sentence = NULL;
- pinyin_get_guessed_sentence(context, &sentence);
+ pinyin_get_guessed_tokens(context, pinyin_keys, constraints,
+ match_results);
+ pinyin_get_sentence (context, match_results, &sentence);
printf("%s\n", sentence);
g_free(sentence);
- pinyin_train(context);
- pinyin_reset(context);
+ pinyin_train(context, pinyin_keys, constraints, match_results);
+ pinyin_reset(context, pinyin_keys, constraints, match_results);
pinyin_save(context);
}
+ pinyin_free_auxiliary_arrays
+ (context, &pinyin_keys, &constraints, &match_results);
pinyin_fini(context);
g_array_free(pinyin_keys, TRUE);
free(linebuf);