From 5cba65baf55b821cbc22b0ee0e3ae8dc9946ca0f Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Tue, 5 Nov 2013 16:29:03 +0100 Subject: cleaning up syntax features --- decoder/ff_soft_syntax_mindist.cc | 235 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 decoder/ff_soft_syntax_mindist.cc (limited to 'decoder/ff_soft_syntax_mindist.cc') diff --git a/decoder/ff_soft_syntax_mindist.cc b/decoder/ff_soft_syntax_mindist.cc new file mode 100644 index 00000000..3f531986 --- /dev/null +++ b/decoder/ff_soft_syntax_mindist.cc @@ -0,0 +1,235 @@ +#include "ff_soft_syntax_mindist.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))). +// +// This variant accepts fuzzy matches, choosing the constituent with +// minimum distance. + +struct SoftSyntaxFeaturesMindistImpl { + SoftSyntaxFeaturesMindistImpl(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_ef.clear(); + src_tree.clear(); + 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; +}; + +SoftSyntaxFeaturesMindist::SoftSyntaxFeaturesMindist(const string& param) : + FeatureFunction(sizeof(WordID)) { + impl = new SoftSyntaxFeaturesMindistImpl(param); +} + +SoftSyntaxFeaturesMindist::~SoftSyntaxFeaturesMindist() { + delete impl; + impl = NULL; +} + +void SoftSyntaxFeaturesMindist::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 SoftSyntaxFeaturesMindist::PrepareForInput(const SentenceMetadata& smeta) { + impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); +} + -- cgit v1.2.3 From decd2c4b1d4fb42a73a3217f347ea8f317e50869 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Tue, 5 Nov 2013 18:15:18 +0100 Subject: syntax features now read trees from files -- no more escaping! --- decoder/ff_parse_match.cc | 5 ++++- decoder/ff_soft_syntax.cc | 15 +++++++++------ decoder/ff_soft_syntax_mindist.cc | 15 +++++++++------ decoder/ff_source_syntax.cc | 7 +++++-- decoder/ff_source_syntax2.cc | 7 +++++-- utils/filelib.h | 5 ++++- 6 files changed, 36 insertions(+), 18 deletions(-) (limited to 'decoder/ff_soft_syntax_mindist.cc') diff --git a/decoder/ff_parse_match.cc b/decoder/ff_parse_match.cc index 7c79302b..58026975 100644 --- a/decoder/ff_parse_match.cc +++ b/decoder/ff_parse_match.cc @@ -212,6 +212,9 @@ void ParseMatchFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, } void ParseMatchFeatures::PrepareForInput(const SentenceMetadata& smeta) { - impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); + ReadFile f = ReadFile(smeta.GetSGMLValue("src_tree")); + string tree; + f.ReadAll(tree); + impl->InitializeGrids(tree, smeta.GetSourceLength()); } diff --git a/decoder/ff_soft_syntax.cc b/decoder/ff_soft_syntax.cc index a3d26135..23fe87bd 100644 --- a/decoder/ff_soft_syntax.cc +++ b/decoder/ff_soft_syntax.cc @@ -107,10 +107,10 @@ struct SoftSyntaxFeaturesImpl { switch(feat_type) { case '2': if (lhs_str.compare(label) == 0) { - os << "SYN:" << label << "_conform"; + os << "SOFT:" << label << "_conform"; } else { - os << "SYN:" << label << "_cross"; + os << "SOFT:" << label << "_cross"; } fid_ef = FD::Convert(os.str()); if (fid_ef > 0) { @@ -119,7 +119,7 @@ struct SoftSyntaxFeaturesImpl { } break; case '_': - os << "SYN:" << label; + os << "SOFT:" << label; fid_ef = FD::Convert(os.str()); if (lhs_str.compare(label) == 0) { if (fid_ef > 0) { @@ -136,7 +136,7 @@ struct SoftSyntaxFeaturesImpl { break; case '+': if (lhs_str.compare(label) == 0) { - os << "SYN:" << label << "_conform"; + os << "SOFT:" << label << "_conform"; fid_ef = FD::Convert(os.str()); if (fid_ef > 0) { //cerr << "Feature: " << os.str() << endl; @@ -147,7 +147,7 @@ struct SoftSyntaxFeaturesImpl { case '-': //cerr << "-" << endl; if (lhs_str.compare(label) != 0) { - os << "SYN:" << label << "_cross"; + os << "SOFT:" << label << "_cross"; fid_ef = FD::Convert(os.str()); if (fid_ef > 0) { //cerr << "Feature :" << os.str() << endl; @@ -194,6 +194,9 @@ void SoftSyntaxFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, } void SoftSyntaxFeatures::PrepareForInput(const SentenceMetadata& smeta) { - impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); + ReadFile f = ReadFile(smeta.GetSGMLValue("src_tree")); + string tree; + f.ReadAll(tree); + impl->InitializeGrids(tree, smeta.GetSourceLength()); } diff --git a/decoder/ff_soft_syntax_mindist.cc b/decoder/ff_soft_syntax_mindist.cc index 3f531986..a23f70f8 100644 --- a/decoder/ff_soft_syntax_mindist.cc +++ b/decoder/ff_soft_syntax_mindist.cc @@ -146,10 +146,10 @@ struct SoftSyntaxFeaturesMindistImpl { case '2': if (min_dist_label.compare(label) == 0) { if (min_dist == 0) { - os << "SYN:" << label << "_conform"; + os << "SOFTM:" << label << "_conform"; } else { - os << "SYN:" << label << "_cross"; + os << "SOFTM:" << label << "_cross"; } fid_ef = FD::Convert(os.str()); //cerr << "Feature :" << os.str() << endl; @@ -157,7 +157,7 @@ struct SoftSyntaxFeaturesMindistImpl { } break; case '_': - os << "SYN:" << label; + os << "SOFTM:" << label; fid_ef = FD::Convert(os.str()); if (min_dist_label.compare(label) == 0) { //cerr << "Feature: " << os.str() << endl; @@ -172,7 +172,7 @@ struct SoftSyntaxFeaturesMindistImpl { break; case '+': if (min_dist_label.compare(label) == 0) { - os << "SYN:" << label << "_conform"; + os << "SOFTM:" << label << "_conform"; fid_ef = FD::Convert(os.str()); if (min_dist == 0) { //cerr << "Feature: " << os.str() << endl; @@ -183,7 +183,7 @@ struct SoftSyntaxFeaturesMindistImpl { case '-': //cerr << "-" << endl; if (min_dist_label.compare(label) != 0) { - os << "SYN:" << label << "_cross"; + os << "SOFTM:" << label << "_cross"; fid_ef = FD::Convert(os.str()); if (min_dist > 0) { //cerr << "Feature :" << os.str() << endl; @@ -230,6 +230,9 @@ void SoftSyntaxFeaturesMindist::TraversalFeaturesImpl(const SentenceMetadata& sm } void SoftSyntaxFeaturesMindist::PrepareForInput(const SentenceMetadata& smeta) { - impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); + ReadFile f = ReadFile(smeta.GetSGMLValue("src_tree")); + string tree; + f.ReadAll(tree); + impl->InitializeGrids(tree, smeta.GetSourceLength()); } diff --git a/decoder/ff_source_syntax.cc b/decoder/ff_source_syntax.cc index 34e7ab69..4879ca1d 100644 --- a/decoder/ff_source_syntax.cc +++ b/decoder/ff_source_syntax.cc @@ -104,7 +104,7 @@ struct SourceSyntaxFeaturesImpl { if (fid_ef <= 0) { ostringstream os; //ostringstream os2; - os << "SYN:" << TD::Convert(lhs); + os << "SSYN:" << TD::Convert(lhs); //os2 << "SYN:" << TD::Convert(lhs) << '_' << SpanSizeTransform(j - i); //fid_cat = FD::Convert(os2.str()); os << ':'; @@ -173,7 +173,10 @@ void SourceSyntaxFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta, } void SourceSyntaxFeatures::PrepareForInput(const SentenceMetadata& smeta) { - impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); + ReadFile f = ReadFile(smeta.GetSGMLValue("src_tree")); + string tree; + f.ReadAll(tree); + impl->InitializeGrids(tree, smeta.GetSourceLength()); } struct SourceSpanSizeFeaturesImpl { diff --git a/decoder/ff_source_syntax2.cc b/decoder/ff_source_syntax2.cc index 63736342..9d0bc33f 100644 --- a/decoder/ff_source_syntax2.cc +++ b/decoder/ff_source_syntax2.cc @@ -90,7 +90,7 @@ struct SourceSyntaxFeatures2Impl { const WordID lhs = src_tree(i,j); int& fid_ef = fids_ef(i,j)[&rule]; ostringstream os; - os << "SYN:" << TD::Convert(lhs); + os << "SSYN2:" << TD::Convert(lhs); os << ':'; unsigned ntc = 0; for (unsigned k = 0; k < rule.f_.size(); ++k) { @@ -159,6 +159,9 @@ void SourceSyntaxFeatures2::TraversalFeaturesImpl(const SentenceMetadata& smeta, } void SourceSyntaxFeatures2::PrepareForInput(const SentenceMetadata& smeta) { - impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength()); + ReadFile f = ReadFile(smeta.GetSGMLValue("src_tree")); + string tree; + f.ReadAll(tree); + impl->InitializeGrids(tree, smeta.GetSourceLength()); } diff --git a/utils/filelib.h b/utils/filelib.h index b9ea3940..4fa69760 100644 --- a/utils/filelib.h +++ b/utils/filelib.h @@ -75,7 +75,10 @@ class ReadFile : public BaseFile { } } } - + void ReadAll(std::string& s) { + getline(*stream(), s, (char) EOF); + if (s.size() > 0) s.resize(s.size()-1); + } }; class WriteFile : public BaseFile { -- cgit v1.2.3