[parser] using trie_new_from_hash instead of an inline implemention in averaged perceptron training
This commit is contained in:
@@ -1,7 +1,4 @@
|
|||||||
#include "averaged_perceptron_trainer.h"
|
#include "averaged_perceptron_trainer.h"
|
||||||
#include "klib/ksort.h"
|
|
||||||
|
|
||||||
KSORT_INIT_STR
|
|
||||||
|
|
||||||
void averaged_perceptron_trainer_destroy(averaged_perceptron_trainer_t *self) {
|
void averaged_perceptron_trainer_destroy(averaged_perceptron_trainer_t *self) {
|
||||||
if (self == NULL) return;
|
if (self == NULL) return;
|
||||||
@@ -152,40 +149,12 @@ averaged_perceptron_t *averaged_perceptron_trainer_finalize(averaged_perceptron_
|
|||||||
|
|
||||||
perceptron->weights = averaged_weights;
|
perceptron->weights = averaged_weights;
|
||||||
|
|
||||||
trie_t *features = trie_new();
|
trie_t *features = trie_new_from_hash(self->features);
|
||||||
const char *key;
|
if (features == NULL) {
|
||||||
uint32_t feature_id;
|
averaged_perceptron_trainer_destroy(self);
|
||||||
|
return NULL;
|
||||||
string_array *feature_keys = string_array_new_size(kh_size(self->features));
|
|
||||||
kh_foreach(self->features, key, feature_id, {
|
|
||||||
string_array_push(feature_keys, (char *)key);
|
|
||||||
})
|
|
||||||
|
|
||||||
ks_introsort(str, feature_keys->n, (const char **)feature_keys->a);
|
|
||||||
|
|
||||||
khiter_t k;
|
|
||||||
|
|
||||||
for (int i = 0; i < feature_keys->n; i++) {
|
|
||||||
char *str = feature_keys->a[i];
|
|
||||||
k = kh_get(str_uint32, self->features, str);
|
|
||||||
if (k == kh_end(self->features)) {
|
|
||||||
log_error("Key not found\n");
|
|
||||||
trie_destroy(features);
|
|
||||||
averaged_perceptron_destroy(perceptron);
|
|
||||||
}
|
|
||||||
|
|
||||||
feature_id = kh_value(self->features, k);
|
|
||||||
|
|
||||||
if (!trie_add(features, str, feature_id)) {
|
|
||||||
log_error("Error adding to trie\n");
|
|
||||||
trie_destroy(features);
|
|
||||||
averaged_perceptron_destroy(perceptron);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string_array_destroy(feature_keys);
|
|
||||||
|
|
||||||
perceptron->features = features;
|
perceptron->features = features;
|
||||||
|
|
||||||
perceptron->num_features = self->num_features;
|
perceptron->num_features = self->num_features;
|
||||||
|
|||||||
Reference in New Issue
Block a user