summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2012-09-06 13:46:56 +0800
committerPeng Wu <alexepico@gmail.com>2012-09-06 13:46:56 +0800
commit732e8b4010613953e5af74ea6062af24b770f572 (patch)
treec7fec427063b4ba29cde1460f12a34325cf8d889
parentbb3218247dfddf2c90f0a353059908dfb6890119 (diff)
downloadlibpinyin-732e8b4010613953e5af74ea6062af24b770f572.tar.gz
libpinyin-732e8b4010613953e5af74ea6062af24b770f572.tar.xz
libpinyin-732e8b4010613953e5af74ea6062af24b770f572.zip
write TAGLIB_GET_TAGVALUE
-rw-r--r--utils/storage/import_interpolation.cpp24
-rw-r--r--utils/training/import_k_mixture_model.cpp54
2 files changed, 35 insertions, 43 deletions
diff --git a/utils/storage/import_interpolation.cpp b/utils/storage/import_interpolation.cpp
index 9295096..787f893 100644
--- a/utils/storage/import_interpolation.cpp
+++ b/utils/storage/import_interpolation.cpp
@@ -33,6 +33,14 @@
(phrase_table, phrase_index, string); \
}
+#define TAGLIB_GET_TAGVALUE(type, var, conv) \
+ type var; \
+ { \
+ gpointer value = NULL; \
+ assert(g_hash_table_lookup_extended \
+ (required, #var, NULL, &value)); \
+ var = conv((const char *)value); \
+ }
enum LINE_TYPE{
BEGIN_LINE = 1,
@@ -81,10 +89,9 @@ bool parse_headline(){
}
assert(line_type == BEGIN_LINE);
- char * value = NULL;
- assert(g_hash_table_lookup_extended
- (required, "model", NULL, (gpointer *)&value));
- if ( !( strcmp("interpolation", value) == 0 ) ) {
+ /* check header */
+ TAGLIB_GET_TAGVALUE(const char *, model, (const char *));
+ if ( !( strcmp("interpolation", model) == 0 ) ) {
fprintf(stderr, "error: interpolation model expected.\n");
return false;
}
@@ -137,9 +144,7 @@ bool parse_unigram(FILE * input, PhraseLargeTable2 * phrase_table,
/* handle \item in \1-gram */
TAGLIB_GET_VALUE(token, 0);
- gpointer value = NULL;
- assert(g_hash_table_lookup_extended(required, "count", NULL, &value));
- glong count = atol((const char *)value);
+ TAGLIB_GET_TAGVALUE(glong, count, atol);
phrase_index->add_unigram_frequency(token, count);
break;
}
@@ -174,10 +179,7 @@ bool parse_bigram(FILE * input, PhraseLargeTable2 * phrase_table,
TAGLIB_GET_VALUE(token1, 0);
TAGLIB_GET_VALUE(token2, 1);
- gpointer value = NULL;
- /* tag: count */
- assert(g_hash_table_lookup_extended(required, "count", NULL, &value));
- glong count = atol((const char *)value);
+ TAGLIB_GET_TAGVALUE(glong, count, atol);
if ( last_token != token1 ) {
if ( last_token && last_single_gram ) {
diff --git a/utils/training/import_k_mixture_model.cpp b/utils/training/import_k_mixture_model.cpp
index 5bbd603..7af768e 100644
--- a/utils/training/import_k_mixture_model.cpp
+++ b/utils/training/import_k_mixture_model.cpp
@@ -33,6 +33,16 @@
(phrase_table, phrase_index, string); \
}
+#define TAGLIB_GET_TAGVALUE(type, var, conv) \
+ type var; \
+ { \
+ gpointer value = NULL; \
+ assert(g_hash_table_lookup_extended \
+ (required, #var, NULL, &value)); \
+ var = conv((const char *)value); \
+ }
+
+
enum LINE_TYPE{
BEGIN_LINE = 1,
END_LINE,
@@ -85,20 +95,16 @@ bool parse_headline(KMixtureModelBigram * bigram){
}
assert(line_type == BEGIN_LINE);
- gpointer value = NULL;
- assert(g_hash_table_lookup_extended(required, "model", NULL, &value));
- const char * model = (const char *)value;
+ /* check header */
+ TAGLIB_GET_TAGVALUE(const char *, model, (const char *));
if ( !( strcmp("k mixture model", model) == 0 ) ) {
fprintf(stderr, "error: k mixture model expected.\n");
return false;
}
- assert(g_hash_table_lookup_extended(required, "count", NULL, &value));
- glong count = atol((char *)value);
- assert(g_hash_table_lookup_extended(required, "N", NULL, &value));
- glong N = atol((char *) value);
- assert(g_hash_table_lookup_extended(required, "total_freq", NULL, &value));
- glong total_freq = atol((char *)value);
+ TAGLIB_GET_TAGVALUE(glong, count, atol);
+ TAGLIB_GET_TAGVALUE(glong, N, atol);
+ TAGLIB_GET_TAGVALUE(glong, total_freq, atol);
KMixtureModelMagicHeader magic_header;
memset(&magic_header, 0, sizeof(KMixtureModelMagicHeader));
@@ -156,13 +162,8 @@ bool parse_unigram(FILE * input, PhraseLargeTable2 * phrase_table,
/* handle \item in \1-gram */
TAGLIB_GET_VALUE(token, 0);
- gpointer value = NULL;
- assert(g_hash_table_lookup_extended(required, "count",
- NULL, &value));
- glong count = atol((const char *)value);
- assert(g_hash_table_lookup_extended(required, "freq",
- NULL, &value));
- glong freq = atol((const char *)value);
+ TAGLIB_GET_TAGVALUE(glong, count, atol);
+ TAGLIB_GET_TAGVALUE(glong, freq, atol);
KMixtureModelArrayHeader array_header;
memset(&array_header, 0, sizeof(KMixtureModelArrayHeader));
@@ -203,23 +204,12 @@ bool parse_bigram(FILE * input, PhraseLargeTable2 * phrase_table,
TAGLIB_GET_VALUE(token1, 0);
TAGLIB_GET_VALUE(token2, 1);
- gpointer value = NULL;
- /* tag: count */
- assert(g_hash_table_lookup_extended(required, "count", NULL, &value));
- glong count = atol((char *)value);
- /* tag: T */
- assert(g_hash_table_lookup_extended(required, "T", NULL, &value));
- glong T = atol((char *)value);
+ TAGLIB_GET_TAGVALUE(glong, count, atol);
+ TAGLIB_GET_TAGVALUE(glong, T, atol);
assert(count == T);
- /* tag: N_n_0 */
- assert(g_hash_table_lookup_extended(required, "N_n_0", NULL, &value));
- glong N_n_0 = atol((char *)value);
- /* tag: n_1 */
- assert(g_hash_table_lookup_extended(required, "n_1", NULL, &value));
- glong n_1 = atol((char *)value);
- /* tag: Mr */
- assert(g_hash_table_lookup_extended(required, "Mr", NULL, &value));
- glong Mr = atol((char *)value);
+ TAGLIB_GET_TAGVALUE(glong, N_n_0, atol);
+ TAGLIB_GET_TAGVALUE(glong, n_1, atol);
+ TAGLIB_GET_TAGVALUE(glong, Mr, atol);
KMixtureModelArrayItem array_item;
memset(&array_item, 0, sizeof(KMixtureModelArrayItem));