Skip to content

Commit

Permalink
fix bug: missing evaluate in otner test
Browse files Browse the repository at this point in the history
  • Loading branch information
endyul committed Aug 18, 2015
1 parent 67150d5 commit 3014c08
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/ner/instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ class Instance {
return ret;
}

size_t num_recalled_entites() const {
size_t num_recalled_entities() const {
size_t len = 0;
size_t ret = 0;
size_t gold_len = 0, predict_len = 0;

for (size_t i = 0; i < entities.size(); ++ i) { len += entities[i].size(); }

for (size_t i = 0, j = 0; i < entities.size() && j < predict_entities.size(); ) {
if ((entities[i] == predict_entities[j]) &&
if ((entities[i] == predict_entities[j]) &&
(entities_tags[i] == predict_entities_tags[j])) {
if (entities_tags[i] != OTHER) {
++ ret;
Expand Down
31 changes: 28 additions & 3 deletions src/ner/ner_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ NamedEntityRecognizerFrontend::NamedEntityRecognizerFrontend(

NamedEntityRecognizerFrontend::~NamedEntityRecognizerFrontend() {
if (glob_con) { delete glob_con; glob_con = 0; }

for (size_t i = 0; i < train_dat.size(); ++ i) {
if (train_dat[i]) { delete train_dat[i]; train_dat[i] = 0; }
}
Expand Down Expand Up @@ -230,7 +230,7 @@ void NamedEntityRecognizerFrontend::train(void) {
updated_features.add(ctx.correct_features, 1.);
updated_features.add(ctx.predict_features, -1.);

learn(train_opt.algorithm, updated_features,
learn(train_opt.algorithm, updated_features,
iter*train_dat.size() + i + 1, inst->num_errors(), model);

if (train_opt.rare_feature_threshold > 0) {
Expand Down Expand Up @@ -305,7 +305,7 @@ void NamedEntityRecognizerFrontend::evaluate(double& f_score) {
build_entities(inst, inst->predict_tagsidx, inst->predict_entities,
inst->predict_entities_tags);

num_recalled_entities += inst->num_recalled_entites();
num_recalled_entities += inst->num_recalled_entities();
num_predict_entities += inst->num_predict_entities();
num_gold_entities += inst->num_gold_entities();

Expand Down Expand Up @@ -350,6 +350,10 @@ void NamedEntityRecognizerFrontend::test(void) {
INFO_LOG("report: number of features %d", model->space.num_features());
INFO_LOG("report: number of dimension %d", model->space.dim());

size_t num_recalled_entities = 0;
size_t num_predict_entities = 0;
size_t num_gold_entities = 0;

const char* test_file = test_opt.test_file.c_str();
std::ifstream ifs(test_file);

Expand Down Expand Up @@ -382,10 +386,31 @@ void NamedEntityRecognizerFrontend::test(void) {
for(size_t i = 0; i < len; ++i) {
inst->predict_tags[i] = model->labels.at(inst->predict_tagsidx[i]);
}

if (test_opt.evaluate) {
build_entities(inst, inst->tagsidx, inst->entities,
inst->entities_tags);
build_entities(inst, inst->predict_tagsidx, inst->predict_entities,
inst->predict_entities_tags);
num_recalled_entities += inst->num_recalled_entities();
num_predict_entities += inst->num_predict_entities();
num_gold_entities += inst->num_gold_entities();
}

writer.write(inst);
delete inst;
}

if (test_opt.evaluate) {
double p = (double)num_recalled_entities / num_predict_entities;
double r = (double)num_recalled_entities / num_gold_entities;
double f_score = 2*p*r / (p + r);

INFO_LOG("P: %lf ( %d / %d )", p, num_recalled_entities, num_predict_entities);
INFO_LOG("R: %lf ( %d / %d )", r, num_recalled_entities, num_gold_entities);
INFO_LOG("F: %lf" , f_score);
}

INFO_LOG("Elapsed time %lf", t.elapsed());
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/postagger/postagger_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,11 @@ void PostaggerFrontend::test(void) {
delete inst;
}

double p = (double)num_recalled_tags / num_tags;
if (test_opt.evaluate) {
double p = (double)num_recalled_tags / num_tags;
INFO_LOG("P: %lf ( %d / %d )", p, num_recalled_tags, num_tags);
}

INFO_LOG("Elapsed time %lf", t.elapsed());

//sleep(1000000);
Expand Down

0 comments on commit 3014c08

Please sign in to comment.