From 68fbe21c181d6804ebce52058ebccd1a0d77444c Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Thu, 14 Mar 2013 23:38:41 -0400 Subject: source path features --- decoder/ff_source_path.cc | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 decoder/ff_source_path.cc (limited to 'decoder/ff_source_path.cc') 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* 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* 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& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const { + WordID* res = reinterpret_cast(context); + const vector& 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(ant_contexts[cur]); + else + FireUnigramFeature(cur, features); + if (prev) FireBigramFeature(prev, cur, features); + prev = cur; + } + *res = prev; +} -- cgit v1.2.3 From 37162522a07551b261c445a92245445c5458f759 Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Thu, 14 Mar 2013 23:46:46 -0400 Subject: fix source path --- decoder/cdec_ff.cc | 2 +- decoder/ff_source_path.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'decoder/ff_source_path.cc') diff --git a/decoder/cdec_ff.cc b/decoder/cdec_ff.cc index a60f2c33..0bf441d4 100644 --- a/decoder/cdec_ff.cc +++ b/decoder/cdec_ff.cc @@ -71,7 +71,7 @@ void register_feature_functions() { ff_registry.Register("InputIndicator", new FFFactory); ff_registry.Register("LexicalTranslationTrigger", new FFFactory); ff_registry.Register("WordPairFeatures", new FFFactory); - ff_registry.Register("SourthPathFeatures", new FFFactory); + ff_registry.Register("SourcePathFeatures", new FFFactory); ff_registry.Register("WordSet", new FFFactory); ff_registry.Register("Dwarf", new FFFactory); ff_registry.Register("External", new FFFactory); diff --git a/decoder/ff_source_path.cc b/decoder/ff_source_path.cc index d5fa6bb3..56cbfc48 100644 --- a/decoder/ff_source_path.cc +++ b/decoder/ff_source_path.cc @@ -30,7 +30,7 @@ void SourcePathFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, for (unsigned i = 0; i < f.size(); ++i) { int cur = f[i]; if (cur <= 0) - cur = *reinterpret_cast(ant_contexts[cur]); + cur = *reinterpret_cast(ant_contexts[-cur]); else FireUnigramFeature(cur, features); if (prev) FireBigramFeature(prev, cur, features); -- cgit v1.2.3 From cba324bee086f4dd0fc9df204d33ff7eb9b6c323 Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Fri, 15 Mar 2013 00:17:55 -0400 Subject: fix bug --- decoder/ff_source_path.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'decoder/ff_source_path.cc') diff --git a/decoder/ff_source_path.cc b/decoder/ff_source_path.cc index 56cbfc48..2a3bee2e 100644 --- a/decoder/ff_source_path.cc +++ b/decoder/ff_source_path.cc @@ -4,7 +4,7 @@ using namespace std; -SourcePathFeatures::SourcePathFeatures(const string& param) : FeatureFunction(4) {} +SourcePathFeatures::SourcePathFeatures(const string& param) : FeatureFunction(sizeof(int)) {} void SourcePathFeatures::FireBigramFeature(WordID prev, WordID cur, SparseVector* features) const { int& fid = bigram_fids[prev][cur]; @@ -27,10 +27,11 @@ void SourcePathFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, WordID* res = reinterpret_cast(context); const vector& 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(ant_contexts[-cur]); + if (cur < 0) + cur = *reinterpret_cast(ant_contexts[ntc++]); else FireUnigramFeature(cur, features); if (prev) FireBigramFeature(prev, cur, features); @@ -38,3 +39,4 @@ void SourcePathFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, } *res = prev; } + -- cgit v1.2.3