Skip to content

Commit

Permalink
formalized postagger code
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneplus committed Jan 14, 2014
1 parent 0bcc4c3 commit 63bfb2c
Show file tree
Hide file tree
Showing 13 changed files with 1,268 additions and 1,351 deletions.
108 changes: 56 additions & 52 deletions src/postagger/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,78 @@
namespace ltp {
namespace postagger {


void Decoder::decode(Instance * inst) {
init_lattice(inst);
viterbi_decode(inst);
get_result(inst);
free_lattice();
void
Decoder::decode(Instance * inst) {
init_lattice(inst);
viterbi_decode(inst);
get_result(inst);
free_lattice();
}

void Decoder::init_lattice(const Instance * inst) {
int len = inst->size();
lattice.resize(len, L);
lattice = NULL;
void
Decoder::init_lattice(const Instance * inst) {
int len = inst->size();
lattice.resize(len, L);
lattice = NULL;
}

void Decoder::viterbi_decode(const Instance * inst) {
int len = inst->size();
for (int i = 0; i < len; ++ i) {
for (int l = 0; l < L; ++ l) {
if (i == 0) {
LatticeItem * item = new LatticeItem(i, l, inst->uni_scores[i][l], NULL);
lattice_insert(lattice[i][l], item);
} else {
for (int pl = 0; pl < L; ++ pl) {
double score = 0.;
const LatticeItem * prev = lattice[i-1][pl];
void
Decoder::viterbi_decode(const Instance * inst) {
int len = inst->size();
for (int i = 0; i < len; ++ i) {
for (int l = 0; l < L; ++ l) {
if (i == 0) {
LatticeItem * item = new LatticeItem(i, l, inst->uni_scores[i][l], NULL);
lattice_insert(lattice[i][l], item);
} else {
for (int pl = 0; pl < L; ++ pl) {
double score = 0.;
const LatticeItem * prev = lattice[i-1][pl];

if (!prev) {
continue;
}
if (!prev) {
continue;
}

score = inst->uni_scores[i][l] + inst->bi_scores[pl][l] + prev->score;
const LatticeItem * item = new LatticeItem(i, l, score, prev);
lattice_insert(lattice[i][l], item);
}
} // end for if i == 0
score = inst->uni_scores[i][l] + inst->bi_scores[pl][l] + prev->score;
const LatticeItem * item = new LatticeItem(i, l, score, prev);
lattice_insert(lattice[i][l], item);
}
} // end for if i == 0
}
}
}

void Decoder::get_result(Instance * inst) {
int len = inst->size();
const LatticeItem * best_item = NULL;
for (int l = 0; l < L; ++ l) {
if (best_item == NULL || lattice[len - 1][l]->score > best_item->score) {
best_item = lattice[len - 1][l];
}
void
Decoder::get_result(Instance * inst) {
int len = inst->size();
const LatticeItem * best_item = NULL;
for (int l = 0; l < L; ++ l) {
if (best_item == NULL || lattice[len - 1][l]->score > best_item->score) {
best_item = lattice[len - 1][l];
}
}

const LatticeItem * item = best_item;
inst->predicted_tagsidx.resize(len);
const LatticeItem * item = best_item;
inst->predicted_tagsidx.resize(len);

while (item) {
inst->predicted_tagsidx[item->i] = item->l;
// std::cout << item->i << " " << item->l << std::endl;
item = item->prev;
}
while (item) {
inst->predicted_tagsidx[item->i] = item->l;
// std::cout << item->i << " " << item->l << std::endl;
item = item->prev;
}
}

void Decoder::free_lattice() {
int len = lattice.total_size();
const LatticeItem ** p = lattice.c_buf();
for (int i = 0; i < len; ++ i) {
if (p[i]) {
delete p[i];
}
void
Decoder::free_lattice() {
int len = lattice.total_size();
const LatticeItem ** p = lattice.c_buf();
for (int i = 0; i < len; ++ i) {
if (p[i]) {
delete p[i];
}
}
}

} // end for namespace postagger
} // end for namespace ltp
} // end for namespace postagger
} // end for namespace ltp

68 changes: 34 additions & 34 deletions src/postagger/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,53 @@ namespace postagger {
// data structure for lattice item
class LatticeItem {
public:
LatticeItem (int _i, int _l, double _score, const LatticeItem * _prev) :
i(_i),
l(_l),
score(_score),
prev(_prev) {}
LatticeItem (int _i, int _l, double _score, const LatticeItem * _prev)
: i(_i),
l(_l),
score(_score),
prev(_prev) {}

LatticeItem (int _l, double _score) :
i(0),
l(_l),
score(_score),
prev(0) {}
LatticeItem (int _l, double _score)
: i(0),
l(_l),
score(_score),
prev(0) {}

public:
int i;
int l;
double score;
const LatticeItem * prev;
int i;
int l;
double score;
const LatticeItem * prev;
};

class Decoder {
public:
Decoder (int _L) : L(_L) {}
void decode(Instance * inst);
Decoder (int _L) : L(_L) {}
void decode(Instance * inst);

private:
void init_lattice(const Instance * inst);
void viterbi_decode(const Instance * inst);
void get_result(Instance * inst);
void free_lattice();
void init_lattice(const Instance * inst);
void viterbi_decode(const Instance * inst);
void get_result(Instance * inst);
void free_lattice();

private:
int L;
int L;

math::Mat< const LatticeItem * > lattice;
math::Mat< const LatticeItem * > lattice;

void lattice_insert(const LatticeItem * &position, const LatticeItem * const item) {
if (position == NULL) {
position = item;
} else if (position->score < item->score) {
delete position;
position = item;
} else {
delete item;
}
void lattice_insert(const LatticeItem * &position, const LatticeItem * const item) {
if (position == NULL) {
position = item;
} else if (position->score < item->score) {
delete position;
position = item;
} else {
delete item;
}
}
};

} // end for namespace postagger
} // end for namespace ltp
#endif // end for __LTP_POSTAGGER_DECODER_H__
} // end for namespace postagger
} // end for namespace ltp
#endif // end for __LTP_POSTAGGER_DECODER_H__
Loading

0 comments on commit 63bfb2c

Please sign in to comment.