diff options
author | Chris Dyer <cdyer@Chriss-MacBook-Air.local> | 2013-03-14 23:38:41 -0400 |
---|---|---|
committer | Chris Dyer <cdyer@Chriss-MacBook-Air.local> | 2013-03-14 23:38:41 -0400 |
commit | 68fbe21c181d6804ebce52058ebccd1a0d77444c (patch) | |
tree | 402d4d92ca51d6f6415d9f09bc2ce9efde72fddc /decoder/ff_source_path.cc | |
parent | 9b252a03a40b17608cbc8c4a75ce37d748855574 (diff) |
source path features
Diffstat (limited to 'decoder/ff_source_path.cc')
-rw-r--r-- | decoder/ff_source_path.cc | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/decoder/ff_source_path.cc b/decoder/ff_source_path.cc new file mode 100644 index 00000000..d5fa6bb3 --- /dev/null +++ b/decoder/ff_source_path.cc @@ -0,0 +1,40 @@ +#include "ff_source_path.h" + +#include "hg.h" + +using namespace std; + +SourcePathFeatures::SourcePathFeatures(const string& param) : FeatureFunction(4) {} + +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; + for (unsigned i = 0; i < f.size(); ++i) { + int cur = f[i]; + if (cur <= 0) + cur = *reinterpret_cast<const WordID*>(ant_contexts[cur]); + else + FireUnigramFeature(cur, features); + if (prev) FireBigramFeature(prev, cur, features); + prev = cur; + } + *res = prev; +} |