diff options
author | Paul Baltescu <pauldb89@gmail.com> | 2013-04-24 17:18:10 +0100 |
---|---|---|
committer | Paul Baltescu <pauldb89@gmail.com> | 2013-04-24 17:18:10 +0100 |
commit | ba206aaac1d95e76126443c9e7ccc5941e879849 (patch) | |
tree | 13a918da3f3983fd8e4cb74e7cdc3f5e1fc01cd1 /decoder/ff_source_path.cc | |
parent | c2aede0f19b7a5e43581768b8c4fbfae8b92c68c (diff) | |
parent | db960a8bba81df3217660ec5a96d73e0d6baa01b (diff) |
Merge branch 'master' of https://github.com/redpony/cdec
Diffstat (limited to 'decoder/ff_source_path.cc')
-rw-r--r-- | decoder/ff_source_path.cc | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/decoder/ff_source_path.cc b/decoder/ff_source_path.cc new file mode 100644 index 00000000..2a3bee2e --- /dev/null +++ b/decoder/ff_source_path.cc @@ -0,0 +1,42 @@ +#include "ff_source_path.h" + +#include "hg.h" + +using namespace std; + +SourcePathFeatures::SourcePathFeatures(const string& param) : FeatureFunction(sizeof(int)) {} + +void SourcePathFeatures::FireBigramFeature(WordID prev, WordID cur, SparseVector<double>* features) const { + int& fid = bigram_fids[prev][cur]; + if (!fid) fid = FD::Convert("SB:"+TD::Convert(prev) + "_" + TD::Convert(cur)); + if (fid) features->add_value(fid, 1.0); +} + +void SourcePathFeatures::FireUnigramFeature(WordID cur, SparseVector<double>* features) const { + int& fid = unigram_fids[cur]; + if (!fid) fid = FD::Convert("SU:" + TD::Convert(cur)); + if (fid) features->add_value(fid, 1.0); +} + +void SourcePathFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, + const HG::Edge& edge, + const vector<const void*>& ant_contexts, + SparseVector<double>* features, + SparseVector<double>* estimated_features, + void* context) const { + WordID* res = reinterpret_cast<WordID*>(context); + const vector<int>& f = edge.rule_->f(); + int prev = 0; + unsigned ntc = 0; + for (unsigned i = 0; i < f.size(); ++i) { + int cur = f[i]; + if (cur < 0) + cur = *reinterpret_cast<const WordID*>(ant_contexts[ntc++]); + else + FireUnigramFeature(cur, features); + if (prev) FireBigramFeature(prev, cur, features); + prev = cur; + } + *res = prev; +} + |