From a9844dce3af515e7d04145eb962947efe9b9b441 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')
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