diff --git a/src/crf_context.c b/src/crf_context.c index 275454ee..372a15c9 100644 --- a/src/crf_context.c +++ b/src/crf_context.c @@ -564,6 +564,9 @@ double crf_context_viterbi(crf_context_t *self, uint32_t *labels) { const double *trans = NULL; const size_t T = self->num_items; + if (T == 0) { + return max_score; + } const size_t L = self->num_labels; // This function assumes state and trans scores to be in the logarithm domain. diff --git a/src/crf_trainer_averaged_perceptron.c b/src/crf_trainer_averaged_perceptron.c index 2f7cdeaa..ee3801a2 100644 --- a/src/crf_trainer_averaged_perceptron.c +++ b/src/crf_trainer_averaged_perceptron.c @@ -15,9 +15,8 @@ void crf_averaged_perceptron_trainer_destroy(crf_averaged_perceptron_trainer_t * kh_destroy(feature_class_weights, self->weights); } + khash_t(prev_tag_class_weights) *prev_tag_weights; if (self->prev_tag_weights != NULL) { - khash_t(prev_tag_class_weights) *prev_tag_weights; - kh_foreach(self->prev_tag_weights, feature_id, prev_tag_weights, { if (prev_tag_weights != NULL) { kh_destroy(prev_tag_class_weights, prev_tag_weights); @@ -602,6 +601,10 @@ bool crf_averaged_perceptron_trainer_train_example(crf_averaged_perceptron_train return false; } + if (num_tokens == 0) { + return true; + } + uint32_array_clear(self->sequence_features_indptr); uint32_array_push(self->sequence_features_indptr, 0); cstring_array_clear(self->sequence_features);