From f332d338c8272a7d06692eebbfd6de3ce3d56935 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Thu, 25 Apr 2013 10:29:57 +0200 Subject: added Gesa's parse match feature function --- decoder/cdec_ff.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'decoder/cdec_ff.cc') diff --git a/decoder/cdec_ff.cc b/decoder/cdec_ff.cc index 0bf441d4..afa421ae 100644 --- a/decoder/cdec_ff.cc +++ b/decoder/cdec_ff.cc @@ -16,6 +16,7 @@ #include "ff_bleu.h" #include "ff_source_path.h" #include "ff_source_syntax.h" +#include "ff_parse_match.h" #include "ff_register.h" #include "ff_charset.h" #include "ff_wordset.h" @@ -49,6 +50,7 @@ void register_feature_functions() { ff_registry.Register("RuleContextFeatures", new FFFactory()); ff_registry.Register("RuleIdentityFeatures", new FFFactory()); ff_registry.Register("SourceSyntaxFeatures", new FFFactory); + ff_registry.Register("ParseMatchFeatures", new FFFactory); ff_registry.Register("SourceSpanSizeFeatures", new FFFactory); ff_registry.Register("CMR2008ReorderingFeatures", new FFFactory()); ff_registry.Register("RuleSourceBigramFeatures", new FFFactory()); -- cgit v1.2.3 From a39be8fb696f2c8ab495e12504d5c836b39835d3 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Fri, 26 Apr 2013 11:23:54 +0200 Subject: added Gesa's soft syntactic features --- decoder/Makefile.am | 4 + decoder/cdec_ff.cc | 6 +- decoder/ff_soft_syntax.cc | 201 ++++++++++++++++++++++++++++++++++++++ decoder/ff_soft_syntax.h | 27 ++++++ decoder/ff_soft_syntax2.cc | 234 +++++++++++++++++++++++++++++++++++++++++++++ decoder/ff_soft_syntax2.h | 27 ++++++ 6 files changed, 498 insertions(+), 1 deletion(-) create mode 100644 decoder/ff_soft_syntax.cc create mode 100644 decoder/ff_soft_syntax.h create mode 100644 decoder/ff_soft_syntax2.cc create mode 100644 decoder/ff_soft_syntax2.h (limited to 'decoder/cdec_ff.cc') diff --git a/decoder/Makefile.am b/decoder/Makefile.am index e075fe37..6ee7ef6a 100644 --- a/decoder/Makefile.am +++ b/decoder/Makefile.am @@ -145,6 +145,10 @@ libcdec_a_SOURCES = \ ff_source_syntax.cc \ ff_parse_match.cc \ ff_parse_match.h \ + ff_soft_syntax.cc \ + ff_soft_syntax.h \ + ff_soft_syntax2.cc \ + ff_soft_syntax2.h \ ff_bleu.cc \ ff_factory.cc \ incremental.cc \ diff --git a/decoder/cdec_ff.cc b/decoder/cdec_ff.cc index afa421ae..6d157840 100644 --- a/decoder/cdec_ff.cc +++ b/decoder/cdec_ff.cc @@ -14,6 +14,8 @@ #include "ff_rules.h" #include "ff_ruleshape.h" #include "ff_bleu.h" +#include "ff_soft_syntax.h" +#include "ff_soft_syntax2.h" #include "ff_source_path.h" #include "ff_source_syntax.h" #include "ff_parse_match.h" @@ -49,7 +51,9 @@ void register_feature_functions() { ff_registry.Register("NgramFeatures", new FFFactory()); ff_registry.Register("RuleContextFeatures", new FFFactory()); ff_registry.Register("RuleIdentityFeatures", new FFFactory()); - ff_registry.Register("SourceSyntaxFeatures", new FFFactory); + ff_registry.Register("SoftSyntactcFeatures", new FFFactory); + ff_registry.Register("SoftSyntcticFeatures2", new FFFactory); + ff_registry.Register("SoftSyntaxFeatures", new FFFactory); ff_registry.Register("ParseMatchFeatures", new FFFactory); ff_registry.Register("SourceSpanSizeFeatures", new FFFactory); ff_registry.Register("CMR2008ReorderingFeatures", new FFFactory()); diff --git a/decoder/ff_soft_syntax.cc b/decoder/ff_soft_syntax.cc new file mode 100644 index 00000000..9981fa45 --- /dev/null +++ b/decoder/ff_soft_syntax.cc @@ -0,0 +1,201 @@ +#include "ff_soft_syntax.h" + +#include +#include +#include +#include +#include + +#include "sentence_metadata.h" +#include "stringlib.h" +#include "array2d.h" +#include "filelib.h" + +using namespace std; + +// Implements the soft syntactic features described in +// Marton and Resnik (2008): "Soft Syntacitc Constraints for Hierarchical Phrase-Based Translation". +// Source trees must be represented in Penn Treebank format, +// e.g. (S (NP John) (VP (V left))). + +struct SoftSyntacticFeaturesImpl { + SoftSyntacticFeaturesImpl(const string& param) { + vector labels = SplitOnWhitespace(param); + for (unsigned int i = 0; i < labels.size(); i++) + //cerr << "Labels: " << labels.at(i) << endl; + for (unsigned int i = 0; i < labels.size(); i++) { + string label = labels.at(i); + pair feat_label; + feat_label.first = label.substr(0, label.size() - 1); + feat_label.second = label.at(label.size() - 1); + feat_labels.push_back(feat_label); + } +} + + void InitializeGrids(const string& tree, unsigned src_len) { + assert(tree.size() > 0); + //fids_cat.clear(); + fids_ef.clear(); + src_tree.clear(); + //fids_cat.resize(src_len, src_len + 1); + fids_ef.resize(src_len, src_len + 1); + src_tree.resize(src_len, src_len + 1, TD::Convert("XX")); + ParseTreeString(tree, src_len); + } + + void ParseTreeString(const string& tree, unsigned src_len) { + stack > stk; // first = i, second = category + pair cur_cat; cur_cat.first = -1; + unsigned i = 0; + unsigned p = 0; + //cerr << "String " << tree << endl; + while(p < tree.size()) { + const char cur = tree[p]; + if (cur == '(') { + stk.push(cur_cat); + ++p; + unsigned k = p + 1; + while (k < tree.size() && tree[k] != ' ') { ++k; } + cur_cat.first = i; + cur_cat.second = TD::Convert(tree.substr(p, k - p)); + //cerr << "NT: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + p = k + 1; + } else if (cur == ')') { + unsigned k = p; + while (k < tree.size() && tree[k] == ')') { ++k; } + const unsigned num_closes = k - p; + for (unsigned ci = 0; ci < num_closes; ++ci) { + // cur_cat.second spans from cur_cat.first to i + //cerr << TD::Convert(cur_cat.second) << " from " << cur_cat.first << " to " << i << endl; + // NOTE: unary rule chains end up being labeled with the top-most category + src_tree(cur_cat.first, i) = cur_cat.second; + cur_cat = stk.top(); + stk.pop(); + } + p = k; + while (p < tree.size() && (tree[p] == ' ' || tree[p] == '\t')) { ++p; } + } else if (cur == ' ' || cur == '\t') { + cerr << "Unexpected whitespace in: " << tree << endl; + abort(); + } else { // terminal symbol + unsigned k = p + 1; + do { + while (k < tree.size() && tree[k] != ')' && tree[k] != ' ') { ++k; } + // cerr << "TERM: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + ++i; + assert(i <= src_len); + while (k < tree.size() && tree[k] == ' ') { ++k; } + p = k; + } while (p < tree.size() && tree[p] != ')'); + } + } + //cerr << "i=" << i << " src_len=" << src_len << endl; + assert(i == src_len); // make sure tree specified in src_tree is + // the same length as the source sentence + } + + WordID FireFeatures(const TRule& rule, const int i, const int j, const WordID* ants, SparseVector* feats) { + //cerr << "fire features: " << rule.AsString() << " for " << i << "," << j << endl; + const WordID lhs = src_tree(i,j); + string lhs_str = TD::Convert(lhs); + //cerr << "LHS: " << lhs_str << " from " << i << " to " << j << endl; + //cerr << "RULE :"<< rule << endl; + int& fid_ef = fids_ef(i,j)[&rule]; + for (unsigned int i = 0; i < feat_labels.size(); i++) { + ostringstream os; + string label = feat_labels.at(i).first; + //cerr << "This Label: " << label << endl; + char feat_type = (char) feat_labels.at(i).second.c_str()[0]; + //cerr << "feat_type: " << feat_type << endl; + switch(feat_type) { + case '2': + if (lhs_str.compare(label) == 0) { + os << "SYN:" << label << "_conform"; + } + else { + os << "SYN:" << label << "_cross"; + } + fid_ef = FD::Convert(os.str()); + if (fid_ef > 0) { + //cerr << "Feature :" << os.str() << endl; + feats->set_value(fid_ef, 1.0); + } + break; + case '_': + os << "SYN:" << label; + fid_ef = FD::Convert(os.str()); + if (lhs_str.compare(label) == 0) { + if (fid_ef > 0) { + //cerr << "Feature: " << os.str() << endl; + feats->set_value(fid_ef, 1.0); + } + } + else { + if (fid_ef > 0) { + //cerr << "Feature: " << os.str() << endl; + feats->set_value(fid_ef, -1.0); + } + } + break; + case '+': + if (lhs_str.compare(label) == 0) { + os << "SYN:" << label << "_conform"; + fid_ef = FD::Convert(os.str()); + if (fid_ef > 0) { + //cerr << "Feature: " << os.str() << endl; + feats->set_value(fid_ef, 1.0); + } + } + break; + case '-': + //cerr << "-" << endl; + if (lhs_str.compare(label) != 0) { + os << "SYN:" << label << "_cross"; + fid_ef = FD::Convert(os.str()); + if (fid_ef > 0) { + //cerr << "Feature :" << os.str() << endl; + feats->set_value(fid_ef, 1.0); + } + } + break; + os.clear(); + os.str(""); + } + //cerr << "Feature: " << os.str() << endl; + //cerr << endl; + } + return lhs; + } + + Array2D src_tree; // src_tree(i,j) NT = type + mutable Array2D > fids_ef; // fires for fully lexicalized + vector > feat_labels; +}; + +SoftSyntacticFeatures::SoftSyntacticFeatures(const string& param) : + FeatureFunction(sizeof(WordID)) { + impl = new SoftSyntacticFeaturesImpl(param); +} + +SoftSyntacticFeatures::~SoftSyntacticFeatures() { + delete impl; + impl = NULL; +} + +void SoftSyntacticFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const { + WordID ants[8]; + for (unsigned i = 0; i < ant_contexts.size(); ++i) + ants[i] = *static_cast(ant_contexts[i]); + + *static_cast(context) = + impl->FireFeatures(*edge.rule_, edge.i_, edge.j_, ants, features); +} + +void SoftSyntacticFeatures::PrepareForInput(const SentenceMetadata& smeta) { + impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); +} diff --git a/decoder/ff_soft_syntax.h b/decoder/ff_soft_syntax.h new file mode 100644 index 00000000..79352f49 --- /dev/null +++ b/decoder/ff_soft_syntax.h @@ -0,0 +1,27 @@ +#ifndef _FF_SOFTSYNTAX_H_ +#define _FF_SOFTSYNTAX_H_ + +#include "ff.h" +#include "hg.h" + +struct SoftSyntacticFeaturesImpl; + +class SoftSyntacticFeatures : public FeatureFunction { + public: + SoftSyntacticFeatures(const std::string& param); + ~SoftSyntacticFeatures(); + protected: + virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const std::vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const; + virtual void PrepareForInput(const SentenceMetadata& smeta); + private: + SoftSyntacticFeaturesImpl* impl; +}; + + + +#endif diff --git a/decoder/ff_soft_syntax2.cc b/decoder/ff_soft_syntax2.cc new file mode 100644 index 00000000..121bc39b --- /dev/null +++ b/decoder/ff_soft_syntax2.cc @@ -0,0 +1,234 @@ +#include "ff_soft_syntax2.h" + +#include +#include +#include +#include +#include + +#include "sentence_metadata.h" +#include "stringlib.h" +#include "array2d.h" +#include "filelib.h" + +using namespace std; + +// Implements the soft syntactic features described in +// Marton and Resnik (2008): "Soft Syntacitc Constraints for Hierarchical Phrase-Based Translation". +// Source trees must be represented in Penn Treebank format, +// e.g. (S (NP John) (VP (V left))). + +struct SoftSyntacticFeatures2Impl { + SoftSyntacticFeatures2Impl(const string& param) { + vector labels = SplitOnWhitespace(param); + //for (unsigned int i = 0; i < labels.size(); i++) + //cerr << "Labels: " << labels.at(i) << endl; + for (unsigned int i = 0; i < labels.size(); i++) { + string label = labels.at(i); + pair feat_label; + feat_label.first = label.substr(0, label.size() - 1); + feat_label.second = label.at(label.size() - 1); + feat_labels.push_back(feat_label); + } + } + + void InitializeGrids(const string& tree, unsigned src_len) { + assert(tree.size() > 0); + //fids_cat.clear(); + fids_ef.clear(); + src_tree.clear(); + //fids_cat.resize(src_len, src_len + 1); + fids_ef.resize(src_len, src_len + 1); + src_tree.resize(src_len, src_len + 1, TD::Convert("XX")); + ParseTreeString(tree, src_len); + } + + void ParseTreeString(const string& tree, unsigned src_len) { + stack > stk; // first = i, second = category + pair cur_cat; cur_cat.first = -1; + unsigned i = 0; + unsigned p = 0; + //cerr << "String " << tree << endl; + while(p < tree.size()) { + const char cur = tree[p]; + if (cur == '(') { + stk.push(cur_cat); + ++p; + unsigned k = p + 1; + while (k < tree.size() && tree[k] != ' ') { ++k; } + cur_cat.first = i; + cur_cat.second = TD::Convert(tree.substr(p, k - p)); + //cerr << "NT: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + p = k + 1; + } else if (cur == ')') { + unsigned k = p; + while (k < tree.size() && tree[k] == ')') { ++k; } + const unsigned num_closes = k - p; + for (unsigned ci = 0; ci < num_closes; ++ci) { + // cur_cat.second spans from cur_cat.first to i + //cerr << TD::Convert(cur_cat.second) << " from " << cur_cat.first << " to " << i << endl; + // NOTE: unary rule chains end up being labeled with the top-most category + src_tree(cur_cat.first, i) = cur_cat.second; + cur_cat = stk.top(); + stk.pop(); + } + p = k; + while (p < tree.size() && (tree[p] == ' ' || tree[p] == '\t')) { ++p; } + } else if (cur == ' ' || cur == '\t') { + cerr << "Unexpected whitespace in: " << tree << endl; + abort(); + } else { // terminal symbol + unsigned k = p + 1; + do { + while (k < tree.size() && tree[k] != ')' && tree[k] != ' ') { ++k; } + // cerr << "TERM: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + ++i; + assert(i <= src_len); + while (k < tree.size() && tree[k] == ' ') { ++k; } + p = k; + } while (p < tree.size() && tree[p] != ')'); + } + } + //cerr << "i=" << i << " src_len=" << src_len << endl; + assert(i == src_len); // make sure tree specified in src_tree is + // the same length as the source sentence + } + + WordID FireFeatures(const TRule& rule, const int i, const int j, const WordID* ants, SparseVector* feats) { + //cerr << "fire features: " << rule.AsString() << " for " << i << "," << j << endl; + const WordID lhs = src_tree(i,j); + string lhs_str = TD::Convert(lhs); + //cerr << "LHS: " << lhs_str << " from " << i << " to " << j << endl; + //cerr << "RULE :"<< rule << endl; + int& fid_ef = fids_ef(i,j)[&rule]; + string lhs_to_str = TD::Convert(lhs); + int min_dist; + string min_dist_label; + if (lhs_to_str.compare("XX") != 0) { + min_dist = 0; + min_dist_label = lhs_to_str; + } + else { + int ok = 0; + for (unsigned int k = 1; k < (j - i); k++) { + min_dist = k; + for (unsigned int l = 0; l <= k; l++) { + int l_add = i-l; + int r_add = j+(k-l); + if ((l_add < src_tree.width() && r_add < src_tree.height()) && (TD::Convert(src_tree(l_add, r_add)).compare("XX") != 0)) { + ok = 1; + min_dist_label = (TD::Convert(src_tree(l_add, r_add))); + break; + } + else { + int l_rem= i+l; + int r_rem = j-(k-l); + if ((l_rem < src_tree.width() && r_rem < src_tree.height()) && TD::Convert(src_tree(l_rem, r_rem)).compare("XX") != 0) { + ok = 1; + min_dist_label = (TD::Convert(src_tree(l_rem, r_rem))); + break; + } + } + } + if (ok) break; + } + } + //cerr << "SPAN: " << i << " " << j << endl; + //cerr << "MINDIST: " << min_dist << endl; + //cerr << "MINDISTLABEL: " << min_dist_label << endl; + for (unsigned int i = 0; i < feat_labels.size(); i++) { + ostringstream os; + string label = feat_labels.at(i).first; + //cerr << "This Label: " << label << endl; + char feat_type = (char) feat_labels.at(i).second.c_str()[0]; + //cerr << "feat_type: " << feat_type << endl; + switch(feat_type) { + case '2': + if (min_dist_label.compare(label) == 0) { + if (min_dist == 0) { + os << "SYN:" << label << "_conform"; + } + else { + os << "SYN:" << label << "_cross"; + } + fid_ef = FD::Convert(os.str()); + //cerr << "Feature :" << os.str() << endl; + feats->set_value(fid_ef, 1.0); + } + break; + case '_': + os << "SYN:" << label; + fid_ef = FD::Convert(os.str()); + if (min_dist_label.compare(label) == 0) { + //cerr << "Feature: " << os.str() << endl; + if (min_dist == 0) { + feats->set_value(fid_ef, 1.0); + } + else { + //cerr << "Feature: " << os.str() << endl; + feats->set_value(fid_ef, -1.0); + } + } + break; + case '+': + if (min_dist_label.compare(label) == 0) { + os << "SYN:" << label << "_conform"; + fid_ef = FD::Convert(os.str()); + if (min_dist == 0) { + //cerr << "Feature: " << os.str() << endl; + feats->set_value(fid_ef, 1.0); + } + } + break; + case '-': + //cerr << "-" << endl; + if (min_dist_label.compare(label) != 0) { + os << "SYN:" << label << "_cross"; + fid_ef = FD::Convert(os.str()); + if (min_dist > 0) { + //cerr << "Feature :" << os.str() << endl; + feats->set_value(fid_ef, 1.0); + } + } + break; + os.clear(); + os.str(""); + } + //cerr << "FEATURE: " << os.str() << endl; + //cerr << endl; + } + return lhs; + } + + Array2D src_tree; // src_tree(i,j) NT = type + mutable Array2D > fids_ef; // fires for fully lexicalized + vector > feat_labels; +}; + +SoftSyntacticFeatures2::SoftSyntacticFeatures2(const string& param) : + FeatureFunction(sizeof(WordID)) { + impl = new SoftSyntacticFeatures2Impl(param); +} + +SoftSyntacticFeatures2::~SoftSyntacticFeatures2() { + delete impl; + impl = NULL; +} + +void SoftSyntacticFeatures2::TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const { + WordID ants[8]; + for (unsigned i = 0; i < ant_contexts.size(); ++i) + ants[i] = *static_cast(ant_contexts[i]); + + *static_cast(context) = + impl->FireFeatures(*edge.rule_, edge.i_, edge.j_, ants, features); +} + +void SoftSyntacticFeatures2::PrepareForInput(const SentenceMetadata& smeta) { + impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); +} diff --git a/decoder/ff_soft_syntax2.h b/decoder/ff_soft_syntax2.h new file mode 100644 index 00000000..4de91d86 --- /dev/null +++ b/decoder/ff_soft_syntax2.h @@ -0,0 +1,27 @@ +#ifndef _FF_SOFTSYNTAX2_H_ +#define _FF_SOFTSYNTAX2_H_ + +#include "ff.h" +#include "hg.h" + +struct SoftSyntacticFeatures2Impl; + +class SoftSyntacticFeatures2 : public FeatureFunction { + public: + SoftSyntacticFeatures2(const std::string& param); + ~SoftSyntacticFeatures2(); + protected: + virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const std::vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const; + virtual void PrepareForInput(const SentenceMetadata& smeta); + private: + SoftSyntacticFeatures2Impl* impl; +}; + + + +#endif -- cgit v1.2.3 From 4b8a716f3024a2e5c7c80518113f76c61b588bc0 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Fri, 26 Apr 2013 15:17:52 +0200 Subject: added (broken) feature function --- decoder/Makefile.am | 3 + decoder/cdec_ff.cc | 5 +- decoder/ff_source_syntax_p.cc | 245 ++++++++++++++++++++++++++++++++++++++++++ decoder/ff_source_syntax_p.h | 42 ++++++++ 4 files changed, 294 insertions(+), 1 deletion(-) create mode 100644 decoder/ff_source_syntax_p.cc create mode 100644 decoder/ff_source_syntax_p.h (limited to 'decoder/cdec_ff.cc') diff --git a/decoder/Makefile.am b/decoder/Makefile.am index 6ee7ef6a..db14afc8 100644 --- a/decoder/Makefile.am +++ b/decoder/Makefile.am @@ -142,6 +142,7 @@ libcdec_a_SOURCES = \ ff_csplit.cc \ ff_tagger.cc \ ff_source_path.cc \ + ff_source_syntax.h \ ff_source_syntax.cc \ ff_parse_match.cc \ ff_parse_match.h \ @@ -149,6 +150,8 @@ libcdec_a_SOURCES = \ ff_soft_syntax.h \ ff_soft_syntax2.cc \ ff_soft_syntax2.h \ + ff_source_syntax_p.h \ + ff_source_syntax_p.cc \ ff_bleu.cc \ ff_factory.cc \ incremental.cc \ diff --git a/decoder/cdec_ff.cc b/decoder/cdec_ff.cc index 6d157840..d1e326fe 100644 --- a/decoder/cdec_ff.cc +++ b/decoder/cdec_ff.cc @@ -18,6 +18,7 @@ #include "ff_soft_syntax2.h" #include "ff_source_path.h" #include "ff_source_syntax.h" +#include "ff_source_syntax_p.h" #include "ff_parse_match.h" #include "ff_register.h" #include "ff_charset.h" @@ -53,9 +54,11 @@ void register_feature_functions() { ff_registry.Register("RuleIdentityFeatures", new FFFactory()); ff_registry.Register("SoftSyntactcFeatures", new FFFactory); ff_registry.Register("SoftSyntcticFeatures2", new FFFactory); - ff_registry.Register("SoftSyntaxFeatures", new FFFactory); + ff_registry.Register("SourceSyntaxFeatures", new FFFactory); + //ff_registry.Register("PSourceSyntaxFeatures", new FFFactory); ff_registry.Register("ParseMatchFeatures", new FFFactory); ff_registry.Register("SourceSpanSizeFeatures", new FFFactory); + //ff_registry.Register("PSourceSpanSizeFeatures", new FFFactory); ff_registry.Register("CMR2008ReorderingFeatures", new FFFactory()); ff_registry.Register("RuleSourceBigramFeatures", new FFFactory()); ff_registry.Register("RuleTargetBigramFeatures", new FFFactory()); diff --git a/decoder/ff_source_syntax_p.cc b/decoder/ff_source_syntax_p.cc new file mode 100644 index 00000000..cd081544 --- /dev/null +++ b/decoder/ff_source_syntax_p.cc @@ -0,0 +1,245 @@ +#include "ff_source_syntax_p.h" + +#include +#include +#include + +#include "sentence_metadata.h" +#include "array2d.h" +#include "filelib.h" + +using namespace std; + +// implements the source side syntax features described in Blunsom et al. (EMNLP 2008) +// source trees must be represented in Penn Treebank format, e.g. +// (S (NP John) (VP (V left))) + +// log transform to make long spans cluster together +// but preserve differences +inline int SpanSizeTransform(unsigned span_size) { + if (!span_size) return 0; + return static_cast(log(span_size+1) / log(1.39)) - 1; +} + +struct PSourceSyntaxFeaturesImpl { + PSourceSyntaxFeaturesImpl() {} + + PSourceSyntaxFeaturesImpl(const string& param) { + if (!(param.compare("") == 0)) { + string triggered_features_fn = param; + ReadFile triggered_features(triggered_features_fn); + string in; + while(getline(*triggered_features, in)) { + feature_filter.insert(FD::Convert(in)); + } + } + } + + void InitializeGrids(const string& tree, unsigned src_len) { + assert(tree.size() > 0); + //fids_cat.clear(); + fids_ef.clear(); + src_tree.clear(); + //fids_cat.resize(src_len, src_len + 1); + fids_ef.resize(src_len, src_len + 1); + src_tree.resize(src_len, src_len + 1, TD::Convert("XX")); + ParseTreeString(tree, src_len); + } + + void ParseTreeString(const string& tree, unsigned src_len) { + stack > stk; // first = i, second = category + pair cur_cat; cur_cat.first = -1; + unsigned i = 0; + unsigned p = 0; + while(p < tree.size()) { + const char cur = tree[p]; + if (cur == '(') { + stk.push(cur_cat); + ++p; + unsigned k = p + 1; + while (k < tree.size() && tree[k] != ' ') { ++k; } + cur_cat.first = i; + cur_cat.second = TD::Convert(tree.substr(p, k - p)); + // cerr << "NT: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + p = k + 1; + } else if (cur == ')') { + unsigned k = p; + while (k < tree.size() && tree[k] == ')') { ++k; } + const unsigned num_closes = k - p; + for (unsigned ci = 0; ci < num_closes; ++ci) { + // cur_cat.second spans from cur_cat.first to i + // cerr << TD::Convert(cur_cat.second) << " from " << cur_cat.first << " to " << i << endl; + // NOTE: unary rule chains end up being labeled with the top-most category + src_tree(cur_cat.first, i) = cur_cat.second; + cur_cat = stk.top(); + stk.pop(); + } + p = k; + while (p < tree.size() && (tree[p] == ' ' || tree[p] == '\t')) { ++p; } + } else if (cur == ' ' || cur == '\t') { + cerr << "Unexpected whitespace in: " << tree << endl; + abort(); + } else { // terminal symbol + unsigned k = p + 1; + do { + while (k < tree.size() && tree[k] != ')' && tree[k] != ' ') { ++k; } + // cerr << "TERM: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + ++i; + assert(i <= src_len); + while (k < tree.size() && tree[k] == ' ') { ++k; } + p = k; + } while (p < tree.size() && tree[p] != ')'); + } + } + // cerr << "i=" << i << " src_len=" << src_len << endl; + assert(i == src_len); // make sure tree specified in src_tree is + // the same length as the source sentence + } + + WordID FireFeatures(const TRule& rule, const int i, const int j, const WordID* ants, SparseVector* feats) { + //cerr << "fire features: " << rule.AsString() << " for " << i << "," << j << endl; + const WordID lhs = src_tree(i,j); + //int& fid_cat = fids_cat(i,j); + int& fid_ef = fids_ef(i,j)[&rule]; + if (fid_ef <= 0) { + ostringstream os; + //ostringstream os2; + os << "SYN:" << TD::Convert(lhs); + //os2 << "SYN:" << TD::Convert(lhs) << '_' << SpanSizeTransform(j - i); + //fid_cat = FD::Convert(os2.str()); + os << ':'; + unsigned ntc = 0; + for (unsigned k = 0; k < rule.f_.size(); ++k) { + if (k > 0) os << '_'; + int fj = rule.f_[k]; + if (fj <= 0) { + os << '[' << TD::Convert(ants[ntc++]) << ']'; + } else { + os << TD::Convert(fj); + } + } + os << ':'; + for (unsigned k = 0; k < rule.e_.size(); ++k) { + const int ei = rule.e_[k]; + if (k > 0) os << '_'; + if (ei <= 0) + os << '[' << (1-ei) << ']'; + else + os << TD::Convert(ei); + } + fid_ef = FD::Convert(os.str()); + } + //if (fid_cat > 0) + // feats->set_value(fid_cat, 1.0); + if (fid_ef > 0 && (feature_filter.find(fid_ef) != feature_filter.end())) + feats->set_value(fid_ef, 1.0); + return lhs; + } + + Array2D src_tree; // src_tree(i,j) NT = type + // mutable Array2D fids_cat; // this tends to overfit baddly + mutable Array2D > fids_ef; // fires for fully lexicalized + tr1::unordered_set feature_filter; +}; + +PSourceSyntaxFeatures::PSourceSyntaxFeatures(const string& param) : + FeatureFunction(sizeof(WordID)) { + impl = new PSourceSyntaxFeaturesImpl(param); +} + +PSourceSyntaxFeatures::~PSourceSyntaxFeatures() { + delete impl; + impl = NULL; +} + +void PSourceSyntaxFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const { + WordID ants[8]; + for (unsigned i = 0; i < ant_contexts.size(); ++i) + ants[i] = *static_cast(ant_contexts[i]); + + *static_cast(context) = + impl->FireFeatures(*edge.rule_, edge.i_, edge.j_, ants, features); +} + +void PSourceSyntaxFeatures::PrepareForInput(const SentenceMetadata& smeta) { + impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); +} + +struct PSourceSpanSizeFeaturesImpl { + PSourceSpanSizeFeaturesImpl() {} + + void InitializeGrids(unsigned src_len) { + fids.clear(); + fids.resize(src_len, src_len + 1); + } + + int FireFeatures(const TRule& rule, const int i, const int j, const WordID* ants, SparseVector* feats) { + if (rule.Arity() > 0) { + int& fid = fids(i,j)[&rule]; + if (fid <= 0) { + ostringstream os; + os << "SSS:"; + unsigned ntc = 0; + for (unsigned k = 0; k < rule.f_.size(); ++k) { + if (k > 0) os << '_'; + int fj = rule.f_[k]; + if (fj <= 0) { + os << '[' << TD::Convert(-fj) << ants[ntc++] << ']'; + } else { + os << TD::Convert(fj); + } + } + os << ':'; + for (unsigned k = 0; k < rule.e_.size(); ++k) { + const int ei = rule.e_[k]; + if (k > 0) os << '_'; + if (ei <= 0) + os << '[' << (1-ei) << ']'; + else + os << TD::Convert(ei); + } + fid = FD::Convert(os.str()); + } + if (fid > 0) + feats->set_value(fid, 1.0); + } + return SpanSizeTransform(j - i); + } + + mutable Array2D > fids; +}; + +PSourceSpanSizeFeatures::PSourceSpanSizeFeatures(const string& param) : + FeatureFunction(sizeof(char)) { + impl = new PSourceSpanSizeFeaturesImpl; +} + +PSourceSpanSizeFeatures::~PSourceSpanSizeFeatures() { + delete impl; + impl = NULL; +} + +void PSourceSpanSizeFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const { + int ants[8]; + for (unsigned i = 0; i < ant_contexts.size(); ++i) + ants[i] = *static_cast(ant_contexts[i]); + + *static_cast(context) = + impl->FireFeatures(*edge.rule_, edge.i_, edge.j_, ants, features); +} + +void PSourceSpanSizeFeatures::PrepareForInput(const SentenceMetadata& smeta) { + impl->InitializeGrids(smeta.GetSourceLength()); +} + + diff --git a/decoder/ff_source_syntax_p.h b/decoder/ff_source_syntax_p.h new file mode 100644 index 00000000..2dd9094a --- /dev/null +++ b/decoder/ff_source_syntax_p.h @@ -0,0 +1,42 @@ +#ifndef _FF_SOURCE_TOOLS_H_ +#define _FF_SOURCE_TOOLS_H_ + +#include "ff.h" +#include "hg.h" + +struct PSourceSyntaxFeaturesImpl; + +class PSourceSyntaxFeatures : public FeatureFunction { + public: + PSourceSyntaxFeatures(const std::string& param); + ~PSourceSyntaxFeatures(); + protected: + virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const std::vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const; + virtual void PrepareForInput(const SentenceMetadata& smeta); + private: + PSourceSyntaxFeaturesImpl* impl; +}; + +struct PSourceSpanSizeFeaturesImpl; +class PSourceSpanSizeFeatures : public FeatureFunction { + public: + PSourceSpanSizeFeatures(const std::string& param); + ~PSourceSpanSizeFeatures(); + protected: + virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const std::vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const; + virtual void PrepareForInput(const SentenceMetadata& smeta); + private: + PSourceSpanSizeFeaturesImpl* impl; +}; + +#endif -- cgit v1.2.3 From 3ea6bc5eadc05b9f052e16a33a5eb96823048e88 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Fri, 26 Apr 2013 15:30:44 +0200 Subject: added Gesa's alternative source syntax features --- decoder/Makefile.am | 8 +- decoder/cdec_ff.cc | 3 + decoder/ff_source_syntax2.cc | 159 +++++++++++++++++++++++++++++++++++++++ decoder/ff_source_syntax2.h | 25 +++++++ decoder/ff_source_syntax2_p.cc | 166 +++++++++++++++++++++++++++++++++++++++++ decoder/ff_source_syntax2_p.h | 25 +++++++ 6 files changed, 384 insertions(+), 2 deletions(-) create mode 100644 decoder/ff_source_syntax2.cc create mode 100644 decoder/ff_source_syntax2.h create mode 100644 decoder/ff_source_syntax2_p.cc create mode 100644 decoder/ff_source_syntax2_p.h (limited to 'decoder/cdec_ff.cc') diff --git a/decoder/Makefile.am b/decoder/Makefile.am index db14afc8..a93a5dbe 100644 --- a/decoder/Makefile.am +++ b/decoder/Makefile.am @@ -144,14 +144,18 @@ libcdec_a_SOURCES = \ ff_source_path.cc \ ff_source_syntax.h \ ff_source_syntax.cc \ + ff_source_syntax_p.h \ + ff_source_syntax_p.cc \ + ff_source_syntax2.h \ + ff_source_syntax2.cc \ + ff_source_syntax2_p.h \ + ff_source_syntax2_p.cc \ ff_parse_match.cc \ ff_parse_match.h \ ff_soft_syntax.cc \ ff_soft_syntax.h \ ff_soft_syntax2.cc \ ff_soft_syntax2.h \ - ff_source_syntax_p.h \ - ff_source_syntax_p.cc \ ff_bleu.cc \ ff_factory.cc \ incremental.cc \ diff --git a/decoder/cdec_ff.cc b/decoder/cdec_ff.cc index d1e326fe..186c4de3 100644 --- a/decoder/cdec_ff.cc +++ b/decoder/cdec_ff.cc @@ -19,6 +19,8 @@ #include "ff_source_path.h" #include "ff_source_syntax.h" #include "ff_source_syntax_p.h" +#include "ff_source_syntax2.h" +#include "ff_source_syntax2_p.h" #include "ff_parse_match.h" #include "ff_register.h" #include "ff_charset.h" @@ -56,6 +58,7 @@ void register_feature_functions() { ff_registry.Register("SoftSyntcticFeatures2", new FFFactory); ff_registry.Register("SourceSyntaxFeatures", new FFFactory); //ff_registry.Register("PSourceSyntaxFeatures", new FFFactory); + ff_registry.Register("SourceSyntaxFeatures2", new FFFactory); ff_registry.Register("ParseMatchFeatures", new FFFactory); ff_registry.Register("SourceSpanSizeFeatures", new FFFactory); //ff_registry.Register("PSourceSpanSizeFeatures", new FFFactory); diff --git a/decoder/ff_source_syntax2.cc b/decoder/ff_source_syntax2.cc new file mode 100644 index 00000000..08ece917 --- /dev/null +++ b/decoder/ff_source_syntax2.cc @@ -0,0 +1,159 @@ +#include "ff_source_syntax2.h" + +#include +#include +#include +#include + +#include "sentence_metadata.h" +#include "array2d.h" +#include "filelib.h" + +using namespace std; + +// implements the source side syntax features described in Blunsom et al. (EMNLP 2008) +// source trees must be represented in Penn Treebank format, e.g. +// (S (NP John) (VP (V left))) + +struct SourceSyntaxFeatures2Impl { + SourceSyntaxFeatures2Impl(const string& param) { + if (!(param.compare("") == 0)) { + string triggered_features_fn = param; + ReadFile triggered_features(triggered_features_fn); + string in; + while(getline(*triggered_features, in)) { + feature_filter.insert(FD::Convert(in)); + } + } + } + + void InitializeGrids(const string& tree, unsigned src_len) { + assert(tree.size() > 0); + //fids_cat.clear(); + fids_ef.clear(); + src_tree.clear(); + //fids_cat.resize(src_len, src_len + 1); + fids_ef.resize(src_len, src_len + 1); + src_tree.resize(src_len, src_len + 1, TD::Convert("XX")); + ParseTreeString(tree, src_len); + } + + void ParseTreeString(const string& tree, unsigned src_len) { + //cerr << "TREE: " << tree << endl; + stack > stk; // first = i, second = category + pair cur_cat; cur_cat.first = -1; + unsigned i = 0; + unsigned p = 0; + while(p < tree.size()) { + const char cur = tree[p]; + if (cur == '(') { + stk.push(cur_cat); + ++p; + unsigned k = p + 1; + while (k < tree.size() && tree[k] != ' ') { ++k; } + cur_cat.first = i; + cur_cat.second = TD::Convert(tree.substr(p, k - p)); + // cerr << "NT: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + p = k + 1; + } else if (cur == ')') { + unsigned k = p; + while (k < tree.size() && tree[k] == ')') { ++k; } + const unsigned num_closes = k - p; + for (unsigned ci = 0; ci < num_closes; ++ci) { + src_tree(cur_cat.first, i) = cur_cat.second; + cur_cat = stk.top(); + stk.pop(); + } + p = k; + while (p < tree.size() && (tree[p] == ' ' || tree[p] == '\t')) { ++p; } + } else if (cur == ' ' || cur == '\t') { + cerr << "Unexpected whitespace in: " << tree << endl; + abort(); + } else { // terminal symbol + unsigned k = p + 1; + do { + while (k < tree.size() && tree[k] != ')' && tree[k] != ' ') { ++k; } + // cerr << "TERM: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + ++i; + assert(i <= src_len); + while (k < tree.size() && tree[k] == ' ') { ++k; } + p = k; + } while (p < tree.size() && tree[p] != ')'); + } + //cerr << "i=" << i << " src_len=" << src_len << endl; + } + //cerr << "i=" << i << " src_len=" << src_len << endl; + assert(i == src_len); // make sure tree specified in src_tree is + // the same length as the source sentence + } + + WordID FireFeatures(const TRule& rule, const int i, const int j, const WordID* ants, SparseVector* feats) { + //cerr << "fire features: " << rule.AsString() << " for " << i << "," << j << endl; + const WordID lhs = src_tree(i,j); + int& fid_ef = fids_ef(i,j)[&rule]; + ostringstream os; + os << "SYN:" << TD::Convert(lhs); + os << ':'; + unsigned ntc = 0; + for (unsigned k = 0; k < rule.f_.size(); ++k) { + int fj = rule.f_[k]; + if (k > 0 && fj <= 0) os << '_'; + if (fj <= 0) { + os << '[' << TD::Convert(ants[ntc++]) << ']'; + } /*else { + os << TD::Convert(fj); + }*/ + } + os << ':'; + for (unsigned k = 0; k < rule.e_.size(); ++k) { + const int ei = rule.e_[k]; + if (k > 0) os << '_'; + if (ei <= 0) + os << '[' << (1-ei) << ']'; + else + os << TD::Convert(ei); + } + fid_ef = FD::Convert(os.str()); + //cerr << "FEATURE: " << os.str() << endl; + //cerr << "FID_EF: " << fid_ef << endl; + if (feature_filter.find(fid_ef) != feature_filter.end()) { + cerr << "SYN-Feature was trigger more than once on training set." << endl; + feats->set_value(fid_ef, 1.0); + } + else cerr << "SYN-Feature was triggered less than once on training set." << endl; + return lhs; + } + + Array2D src_tree; // src_tree(i,j) NT = type + mutable Array2D > fids_ef; // fires for fully lexicalized + tr1::unordered_set feature_filter; + +}; + +SourceSyntaxFeatures2::SourceSyntaxFeatures2(const string& param) : + FeatureFunction(sizeof(WordID)) { + impl = new SourceSyntaxFeatures2Impl(param); +} + +SourceSyntaxFeatures2::~SourceSyntaxFeatures2() { + delete impl; + impl = NULL; +} + +void SourceSyntaxFeatures2::TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const { + WordID ants[8]; + for (unsigned i = 0; i < ant_contexts.size(); ++i) + ants[i] = *static_cast(ant_contexts[i]); + + *static_cast(context) = + impl->FireFeatures(*edge.rule_, edge.i_, edge.j_, ants, features); +} + +void SourceSyntaxFeatures2::PrepareForInput(const SentenceMetadata& smeta) { + impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); +} diff --git a/decoder/ff_source_syntax2.h b/decoder/ff_source_syntax2.h new file mode 100644 index 00000000..b6b7dc3d --- /dev/null +++ b/decoder/ff_source_syntax2.h @@ -0,0 +1,25 @@ +#ifndef _FF_SOURCE_TOOLS2_H_ +#define _FF_SOURCE_TOOLS2_H_ + +#include "ff.h" +#include "hg.h" + +struct SourceSyntaxFeatures2Impl; + +class SourceSyntaxFeatures2 : public FeatureFunction { + public: + SourceSyntaxFeatures2(const std::string& param); + ~SourceSyntaxFeatures2(); + protected: + virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const std::vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const; + virtual void PrepareForInput(const SentenceMetadata& smeta); + private: + SourceSyntaxFeatures2Impl* impl; +}; + +#endif diff --git a/decoder/ff_source_syntax2_p.cc b/decoder/ff_source_syntax2_p.cc new file mode 100644 index 00000000..3517b87f --- /dev/null +++ b/decoder/ff_source_syntax2_p.cc @@ -0,0 +1,166 @@ +#include "ff_source_syntax2_p.h" + +#include +#include +#include +#include + +#include "sentence_metadata.h" +#include "array2d.h" +#include "filelib.h" + +using namespace std; + +// implements the source side syntax features described in Blunsom et al. (EMNLP 2008) +// source trees must be represented in Penn Treebank format, e.g. +// (S (NP John) (VP (V left))) + +struct SourceSyntaxFeatures2Impl { + SourceSyntaxFeatures2Impl(const string& param) { + if (param.compare("") != 0) { + string triggered_features_fn = param; + ReadFile triggered_features(triggered_features_fn); + string in; + while(getline(*triggered_features, in)) { + feature_filter.insert(FD::Convert(in)); + } + } + /*cerr << "find(\"One\") == " << boolalpha << (table.find("One") != table.end()) << endl; + cerr << "find(\"Three\") == " << boolalpha << (table.find("Three") != table.end()) << endl;*/ + } + + void InitializeGrids(const string& tree, unsigned src_len) { + assert(tree.size() > 0); + //fids_cat.clear(); + fids_ef.clear(); + src_tree.clear(); + //fids_cat.resize(src_len, src_len + 1); + fids_ef.resize(src_len, src_len + 1); + src_tree.resize(src_len, src_len + 1, TD::Convert("XX")); + ParseTreeString(tree, src_len); + } + + void ParseTreeString(const string& tree, unsigned src_len) { + //cerr << "TREE: " << tree << endl; + stack > stk; // first = i, second = category + pair cur_cat; cur_cat.first = -1; + unsigned i = 0; + unsigned p = 0; + while(p < tree.size()) { + const char cur = tree[p]; + if (cur == '(') { + stk.push(cur_cat); + ++p; + unsigned k = p + 1; + while (k < tree.size() && tree[k] != ' ') { ++k; } + cur_cat.first = i; + cur_cat.second = TD::Convert(tree.substr(p, k - p)); + // cerr << "NT: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + p = k + 1; + } else if (cur == ')') { + unsigned k = p; + while (k < tree.size() && tree[k] == ')') { ++k; } + const unsigned num_closes = k - p; + for (unsigned ci = 0; ci < num_closes; ++ci) { + src_tree(cur_cat.first, i) = cur_cat.second; + cur_cat = stk.top(); + stk.pop(); + } + p = k; + while (p < tree.size() && (tree[p] == ' ' || tree[p] == '\t')) { ++p; } + } else if (cur == ' ' || cur == '\t') { + cerr << "Unexpected whitespace in: " << tree << endl; + abort(); + } else { // terminal symbol + unsigned k = p + 1; + do { + while (k < tree.size() && tree[k] != ')' && tree[k] != ' ') { ++k; } + // cerr << "TERM: '" << tree.substr(p, k-p) << "' (i=" << i << ")\n"; + ++i; + assert(i <= src_len); + while (k < tree.size() && tree[k] == ' ') { ++k; } + p = k; + } while (p < tree.size() && tree[p] != ')'); + } + //cerr << "i=" << i << " src_len=" << src_len << endl; + } + //cerr << "i=" << i << " src_len=" << src_len << endl; + assert(i == src_len); // make sure tree specified in src_tree is + // the same length as the source sentence + } + + WordID FireFeatures(const TRule& rule, const int i, const int j, const WordID* ants, SparseVector* feats) { + //cerr << "fire features: " << rule.AsString() << " for " << i << "," << j << endl; + const WordID lhs = src_tree(i,j); + int& fid_ef = fids_ef(i,j)[&rule]; + ostringstream os; + os << "SYN:" << TD::Convert(lhs); + os << ':'; + unsigned ntc = 0; + for (unsigned k = 0; k < rule.f_.size(); ++k) { + int fj = rule.f_[k]; + if (k > 0 && fj <= 0) os << '_'; + if (fj <= 0) { + os << '[' << TD::Convert(ants[ntc++]) << ']'; + } /*else { + os << TD::Convert(fj); + }*/ + } + os << ':'; + for (unsigned k = 0; k < rule.e_.size(); ++k) { + const int ei = rule.e_[k]; + if (k > 0) os << '_'; + if (ei <= 0) + os << '[' << (1-ei) << ']'; + else + os << TD::Convert(ei); + } + fid_ef = FD::Convert(os.str()); + //cerr << "FEATURE: " << os.str() << endl; + //cerr << "FID_EF: " << fid_ef << endl; + if (feature_filter.size() > 0) { + if (feature_filter.find(fid_ef) != feature_filter.end()) { + //cerr << "SYN-Feature was trigger more than once on training set." << endl; + feats->set_value(fid_ef, 1.0); + } + //else cerr << "SYN-Feature was triggered less than once on training set." << endli; + } + else { + feats->set_value(fid_ef, 1.0); + } + return lhs; + } + + Array2D src_tree; // src_tree(i,j) NT = type + mutable Array2D > fids_ef; // fires for fully lexicalized + tr1::unordered_set feature_filter; + +}; + +SourceSyntaxFeatures2::SourceSyntaxFeatures2(const string& param) : + FeatureFunction(sizeof(WordID)) { + impl = new SourceSyntaxFeatures2Impl(param); +} + +SourceSyntaxFeatures2::~SourceSyntaxFeatures2() { + delete impl; + impl = NULL; +} + +void SourceSyntaxFeatures2::TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const { + WordID ants[8]; + for (unsigned i = 0; i < ant_contexts.size(); ++i) + ants[i] = *static_cast(ant_contexts[i]); + + *static_cast(context) = + impl->FireFeatures(*edge.rule_, edge.i_, edge.j_, ants, features); +} + +void SourceSyntaxFeatures2::PrepareForInput(const SentenceMetadata& smeta) { + impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); +} diff --git a/decoder/ff_source_syntax2_p.h b/decoder/ff_source_syntax2_p.h new file mode 100644 index 00000000..b6b7dc3d --- /dev/null +++ b/decoder/ff_source_syntax2_p.h @@ -0,0 +1,25 @@ +#ifndef _FF_SOURCE_TOOLS2_H_ +#define _FF_SOURCE_TOOLS2_H_ + +#include "ff.h" +#include "hg.h" + +struct SourceSyntaxFeatures2Impl; + +class SourceSyntaxFeatures2 : public FeatureFunction { + public: + SourceSyntaxFeatures2(const std::string& param); + ~SourceSyntaxFeatures2(); + protected: + virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta, + const Hypergraph::Edge& edge, + const std::vector& ant_contexts, + SparseVector* features, + SparseVector* estimated_features, + void* context) const; + virtual void PrepareForInput(const SentenceMetadata& smeta); + private: + SourceSyntaxFeatures2Impl* impl; +}; + +#endif -- cgit v1.2.3 From 619daa3700303b55604781645b28aa702d96b91a Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Mon, 29 Apr 2013 11:01:38 +0200 Subject: added (broken) variant of a feature --- decoder/Makefile.am | 13 +++---------- decoder/cdec_ff.cc | 24 +++++++++++++++++++----- decoder/ff_source_syntax2_p.cc | 14 +++++++------- decoder/ff_source_syntax2_p.h | 10 +++++----- 4 files changed, 34 insertions(+), 27 deletions(-) (limited to 'decoder/cdec_ff.cc') diff --git a/decoder/Makefile.am b/decoder/Makefile.am index a93a5dbe..00f8b5f9 100644 --- a/decoder/Makefile.am +++ b/decoder/Makefile.am @@ -142,20 +142,13 @@ libcdec_a_SOURCES = \ ff_csplit.cc \ ff_tagger.cc \ ff_source_path.cc \ - ff_source_syntax.h \ + ff_parse_match.cc \ + ff_soft_syntax.cc \ + ff_soft_syntax2.cc \ ff_source_syntax.cc \ - ff_source_syntax_p.h \ ff_source_syntax_p.cc \ - ff_source_syntax2.h \ ff_source_syntax2.cc \ - ff_source_syntax2_p.h \ ff_source_syntax2_p.cc \ - ff_parse_match.cc \ - ff_parse_match.h \ - ff_soft_syntax.cc \ - ff_soft_syntax.h \ - ff_soft_syntax2.cc \ - ff_soft_syntax2.h \ ff_bleu.cc \ ff_factory.cc \ incremental.cc \ diff --git a/decoder/cdec_ff.cc b/decoder/cdec_ff.cc index 186c4de3..bbb83258 100644 --- a/decoder/cdec_ff.cc +++ b/decoder/cdec_ff.cc @@ -17,11 +17,15 @@ #include "ff_soft_syntax.h" #include "ff_soft_syntax2.h" #include "ff_source_path.h" + + +#include "ff_parse_match.h" #include "ff_source_syntax.h" #include "ff_source_syntax_p.h" #include "ff_source_syntax2.h" #include "ff_source_syntax2_p.h" -#include "ff_parse_match.h" + + #include "ff_register.h" #include "ff_charset.h" #include "ff_wordset.h" @@ -54,14 +58,24 @@ void register_feature_functions() { ff_registry.Register("NgramFeatures", new FFFactory()); ff_registry.Register("RuleContextFeatures", new FFFactory()); ff_registry.Register("RuleIdentityFeatures", new FFFactory()); - ff_registry.Register("SoftSyntactcFeatures", new FFFactory); - ff_registry.Register("SoftSyntcticFeatures2", new FFFactory); + + + ff_registry.Register("ParseMatchFeatures", new FFFactory); + + ff_registry.Register("SoftSyntacticFeatures", new FFFactory); + ff_registry.Register("SoftSyntacticFeatures2", new FFFactory); + ff_registry.Register("SourceSyntaxFeatures", new FFFactory); - //ff_registry.Register("PSourceSyntaxFeatures", new FFFactory); ff_registry.Register("SourceSyntaxFeatures2", new FFFactory); - ff_registry.Register("ParseMatchFeatures", new FFFactory); + ff_registry.Register("SourceSpanSizeFeatures", new FFFactory); + + //ff_registry.Register("PSourceSyntaxFeatures", new FFFactory); //ff_registry.Register("PSourceSpanSizeFeatures", new FFFactory); + ff_registry.Register("PSourceSyntaxFeatures2", new FFFactory); + + + ff_registry.Register("CMR2008ReorderingFeatures", new FFFactory()); ff_registry.Register("RuleSourceBigramFeatures", new FFFactory()); ff_registry.Register("RuleTargetBigramFeatures", new FFFactory()); diff --git a/decoder/ff_source_syntax2_p.cc b/decoder/ff_source_syntax2_p.cc index 3517b87f..dfa791ea 100644 --- a/decoder/ff_source_syntax2_p.cc +++ b/decoder/ff_source_syntax2_p.cc @@ -15,8 +15,8 @@ using namespace std; // source trees must be represented in Penn Treebank format, e.g. // (S (NP John) (VP (V left))) -struct SourceSyntaxFeatures2Impl { - SourceSyntaxFeatures2Impl(const string& param) { +struct PSourceSyntaxFeatures2Impl { + PSourceSyntaxFeatures2Impl(const string& param) { if (param.compare("") != 0) { string triggered_features_fn = param; ReadFile triggered_features(triggered_features_fn); @@ -137,17 +137,17 @@ struct SourceSyntaxFeatures2Impl { }; -SourceSyntaxFeatures2::SourceSyntaxFeatures2(const string& param) : +PSourceSyntaxFeatures2::PSourceSyntaxFeatures2(const string& param) : FeatureFunction(sizeof(WordID)) { - impl = new SourceSyntaxFeatures2Impl(param); + impl = new PSourceSyntaxFeatures2Impl(param); } -SourceSyntaxFeatures2::~SourceSyntaxFeatures2() { +PSourceSyntaxFeatures2::~PSourceSyntaxFeatures2() { delete impl; impl = NULL; } -void SourceSyntaxFeatures2::TraversalFeaturesImpl(const SentenceMetadata& smeta, +void PSourceSyntaxFeatures2::TraversalFeaturesImpl(const SentenceMetadata& smeta, const Hypergraph::Edge& edge, const vector& ant_contexts, SparseVector* features, @@ -161,6 +161,6 @@ void SourceSyntaxFeatures2::TraversalFeaturesImpl(const SentenceMetadata& smeta, impl->FireFeatures(*edge.rule_, edge.i_, edge.j_, ants, features); } -void SourceSyntaxFeatures2::PrepareForInput(const SentenceMetadata& smeta) { +void PSourceSyntaxFeatures2::PrepareForInput(const SentenceMetadata& smeta) { impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); } diff --git a/decoder/ff_source_syntax2_p.h b/decoder/ff_source_syntax2_p.h index b6b7dc3d..d56ecab0 100644 --- a/decoder/ff_source_syntax2_p.h +++ b/decoder/ff_source_syntax2_p.h @@ -4,12 +4,12 @@ #include "ff.h" #include "hg.h" -struct SourceSyntaxFeatures2Impl; +struct PSourceSyntaxFeatures2Impl; -class SourceSyntaxFeatures2 : public FeatureFunction { +class PSourceSyntaxFeatures2 : public FeatureFunction { public: - SourceSyntaxFeatures2(const std::string& param); - ~SourceSyntaxFeatures2(); + PSourceSyntaxFeatures2(const std::string& param); + ~PSourceSyntaxFeatures2(); protected: virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta, const Hypergraph::Edge& edge, @@ -19,7 +19,7 @@ class SourceSyntaxFeatures2 : public FeatureFunction { void* context) const; virtual void PrepareForInput(const SentenceMetadata& smeta); private: - SourceSyntaxFeatures2Impl* impl; + PSourceSyntaxFeatures2Impl* impl; }; #endif -- cgit v1.2.3 From 4b942c98e9b86507efe426b5eb36066151e8566b Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Mon, 29 Apr 2013 11:17:07 +0200 Subject: enabled build --- decoder/cdec_ff.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'decoder/cdec_ff.cc') diff --git a/decoder/cdec_ff.cc b/decoder/cdec_ff.cc index bbb83258..e7b31f50 100644 --- a/decoder/cdec_ff.cc +++ b/decoder/cdec_ff.cc @@ -72,8 +72,7 @@ void register_feature_functions() { //ff_registry.Register("PSourceSyntaxFeatures", new FFFactory); //ff_registry.Register("PSourceSpanSizeFeatures", new FFFactory); - ff_registry.Register("PSourceSyntaxFeatures2", new FFFactory); - + //ff_registry.Register("PSourceSyntaxFeatures2", new FFFactory); ff_registry.Register("CMR2008ReorderingFeatures", new FFFactory()); -- cgit v1.2.3