From e39d4d2f00c5a331e649731da27ff59f16ad6186 Mon Sep 17 00:00:00 2001 From: Al Date: Fri, 24 Feb 2017 02:57:16 -0500 Subject: [PATCH] [parser] check for non-null prev/prev2 before creating tag-based features --- src/averaged_perceptron_tagger.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/averaged_perceptron_tagger.c b/src/averaged_perceptron_tagger.c index 781b64e0..b4b66588 100644 --- a/src/averaged_perceptron_tagger.c +++ b/src/averaged_perceptron_tagger.c @@ -1,7 +1,6 @@ #include "averaged_perceptron_tagger.h" #include "log/log.h" - bool averaged_perceptron_tagger_predict(averaged_perceptron_t *model, void *tagger, void *context, cstring_array *features, cstring_array *prev_tag_features, cstring_array *prev2_tag_features, cstring_array *labels, ap_tagger_feature_function feature_function, tokenized_string_t *tokenized) { // Keep two tags of history in training @@ -15,6 +14,8 @@ bool averaged_perceptron_tagger_predict(averaged_perceptron_t *model, void *tagg for (uint32_t i = 0; i < num_tokens; i++) { cstring_array_clear(features); + cstring_array_clear(prev_tag_features); + cstring_array_clear(prev2_tag_features); if (i > 0) { prev = cstring_array_get_string(model->classes, prev_id); @@ -34,13 +35,17 @@ bool averaged_perceptron_tagger_predict(averaged_perceptron_t *model, void *tagg uint32_t fidx; const char *feature; - cstring_array_foreach(prev_tag_features, fidx, feature, { - feature_array_add(features, 2, (char *)feature, prev); - }) + if (prev) { + cstring_array_foreach(prev_tag_features, fidx, feature, { + feature_array_add(features, 2, (char *)feature, prev); + }) + } - cstring_array_foreach(prev2_tag_features, fidx, feature, { - feature_array_add(features, 3, (char *)feature, prev2, prev); - }) + if (prev2) { + cstring_array_foreach(prev2_tag_features, fidx, feature, { + feature_array_add(features, 3, (char *)feature, prev2, prev); + }) + } uint32_t guess = averaged_perceptron_predict(model, features); char *predicted = cstring_array_get_string(model->classes, guess);