summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--decoder/Makefile.am1
-rw-r--r--decoder/cdec_ff.cc2
-rw-r--r--decoder/ff_source_syntax.cc157
-rw-r--r--decoder/ff_source_syntax.h24
-rw-r--r--utils/stringlib.cc7
5 files changed, 190 insertions, 1 deletions
diff --git a/decoder/Makefile.am b/decoder/Makefile.am
index e5f7505f..ede1cff0 100644
--- a/decoder/Makefile.am
+++ b/decoder/Makefile.am
@@ -72,6 +72,7 @@ libcdec_a_SOURCES = \
ff_wordalign.cc \
ff_csplit.cc \
ff_tagger.cc \
+ ff_source_syntax.cc \
ff_bleu.cc \
ff_factory.cc \
freqdict.cc \
diff --git a/decoder/cdec_ff.cc b/decoder/cdec_ff.cc
index 588842f1..d562bc3a 100644
--- a/decoder/cdec_ff.cc
+++ b/decoder/cdec_ff.cc
@@ -14,6 +14,7 @@
#include "ff_bleu.h"
#include "ff_lm_fsa.h"
#include "ff_sample_fsa.h"
+#include "ff_source_syntax.h"
#include "ff_register.h"
#include "ff_charset.h"
#include "ff_wordset.h"
@@ -55,6 +56,7 @@ void register_feature_functions() {
ff_registry.Register("SpanFeatures", new FFFactory<SpanFeatures>());
ff_registry.Register("NgramFeatures", new FFFactory<NgramDetector>());
ff_registry.Register("RuleIdentityFeatures", new FFFactory<RuleIdentityFeatures>());
+ ff_registry.Register("SourceSyntaxFeatures", new FFFactory<SourceSyntaxFeatures>);
ff_registry.Register("RuleNgramFeatures", new FFFactory<RuleNgramFeatures>());
ff_registry.Register("CMR2008ReorderingFeatures", new FFFactory<CMR2008ReorderingFeatures>());
ff_registry.Register("KLanguageModel", new KLanguageModelFactory());
diff --git a/decoder/ff_source_syntax.cc b/decoder/ff_source_syntax.cc
new file mode 100644
index 00000000..99acbd87
--- /dev/null
+++ b/decoder/ff_source_syntax.cc
@@ -0,0 +1,157 @@
+#include "ff_source_syntax.h"
+
+#include <sstream>
+#include <stack>
+
+#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 SourceSyntaxFeaturesImpl {
+ SourceSyntaxFeaturesImpl() {}
+
+ void InitializeGrids(const string& tree, unsigned src_len) {
+ assert(tree.size() > 0);
+ fids_cat.clear();
+ fids_fonly.clear();
+ fids_ef.clear();
+ src_tree.clear();
+ fids_cat.resize(src_len, src_len + 1);
+ fids_fonly.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<pair<int, WordID> > stk; // first = i, second = category
+ pair<int, WordID> 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<double>* 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_fonly = fids_fonly(i,j)[&rule];
+ int& fid_ef = fids_ef(i,j)[&rule];
+ if (fid_ef <= 0) {
+ ostringstream os;
+ os << "SYN:" << TD::Convert(lhs);
+ fid_cat = FD::Convert(os.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);
+ }
+ }
+ fid_fonly = FD::Convert(os.str());
+ 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_fonly > 0)
+ feats->set_value(fid_fonly, 1.0);
+ if (fid_ef > 0)
+ feats->set_value(fid_ef, 1.0);
+ return lhs;
+ }
+
+ Array2D<WordID> src_tree; // src_tree(i,j) NT = type
+ mutable Array2D<int> fids_cat; // fires for an LHS match
+ mutable Array2D<map<const TRule*, int> > fids_fonly; // fires for an f-string
+ mutable Array2D<map<const TRule*, int> > fids_ef; // fires for fully lexicalized
+};
+
+SourceSyntaxFeatures::SourceSyntaxFeatures(const string& param) :
+ FeatureFunction(sizeof(WordID)) {
+ impl = new SourceSyntaxFeaturesImpl;
+}
+
+SourceSyntaxFeatures::~SourceSyntaxFeatures() {
+ delete impl;
+ impl = NULL;
+}
+
+void SourceSyntaxFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta,
+ const Hypergraph::Edge& edge,
+ const vector<const void*>& ant_contexts,
+ SparseVector<double>* features,
+ SparseVector<double>* estimated_features,
+ void* context) const {
+ WordID ants[8];
+ for (unsigned i = 0; i < ant_contexts.size(); ++i)
+ ants[i] = *static_cast<const WordID*>(ant_contexts[i]);
+
+ *static_cast<WordID*>(context) =
+ impl->FireFeatures(*edge.rule_, edge.i_, edge.j_, ants, features);
+}
+
+void SourceSyntaxFeatures::PrepareForInput(const SentenceMetadata& smeta) {
+ impl->InitializeGrids(smeta.GetSGMLValue("src_tree"), smeta.GetSourceLength());
+}
+
diff --git a/decoder/ff_source_syntax.h b/decoder/ff_source_syntax.h
new file mode 100644
index 00000000..1e890736
--- /dev/null
+++ b/decoder/ff_source_syntax.h
@@ -0,0 +1,24 @@
+#ifndef _FF_SOURCE_TOOLS_H_
+#define _FF_SOURCE_TOOLS_H_
+
+#include "ff.h"
+
+struct SourceSyntaxFeaturesImpl;
+
+class SourceSyntaxFeatures : public FeatureFunction {
+ public:
+ SourceSyntaxFeatures(const std::string& param);
+ ~SourceSyntaxFeatures();
+ protected:
+ virtual void TraversalFeaturesImpl(const SentenceMetadata& smeta,
+ const Hypergraph::Edge& edge,
+ const std::vector<const void*>& ant_contexts,
+ SparseVector<double>* features,
+ SparseVector<double>* estimated_features,
+ void* context) const;
+ virtual void PrepareForInput(const SentenceMetadata& smeta);
+ private:
+ SourceSyntaxFeaturesImpl* impl;
+};
+
+#endif
diff --git a/utils/stringlib.cc b/utils/stringlib.cc
index 7aaee9f0..ade02ca9 100644
--- a/utils/stringlib.cc
+++ b/utils/stringlib.cc
@@ -32,7 +32,12 @@ void ParseTranslatorInput(const string& line, string* input, string* ref) {
void ProcessAndStripSGML(string* pline, map<string, string>* out) {
map<string, string>& meta = *out;
string& line = *pline;
- string lline = LowercaseString(line);
+ string lline = *pline;
+ if (lline.find("<SEG")==0 || lline.find("<Seg")==0) {
+ cerr << "Segment tags <seg> must be lowercase!\n";
+ cerr << " " << *pline << endl;
+ abort();
+ }
if (lline.find("<seg")!=0) return;
size_t close = lline.find(">");
if (close == string::npos) return; // error