diff options
author | Patrick Simianer <p@simianer.de> | 2013-04-26 15:30:44 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2013-04-26 15:30:44 +0200 |
commit | 3ea6bc5eadc05b9f052e16a33a5eb96823048e88 (patch) | |
tree | 2b957717ef47691cab0e1e43e8dffa49452a728c /decoder | |
parent | 4b8a716f3024a2e5c7c80518113f76c61b588bc0 (diff) |
added Gesa's alternative source syntax features
Diffstat (limited to 'decoder')
-rw-r--r-- | decoder/Makefile.am | 8 | ||||
-rw-r--r-- | decoder/cdec_ff.cc | 3 | ||||
-rw-r--r-- | decoder/ff_source_syntax2.cc | 159 | ||||
-rw-r--r-- | decoder/ff_source_syntax2.h | 25 | ||||
-rw-r--r-- | decoder/ff_source_syntax2_p.cc | 166 | ||||
-rw-r--r-- | decoder/ff_source_syntax2_p.h | 25 |
6 files changed, 384 insertions, 2 deletions
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<SoftSyntacticFeatures2>); ff_registry.Register("SourceSyntaxFeatures", new FFFactory<SourceSyntaxFeatures>); //ff_registry.Register("PSourceSyntaxFeatures", new FFFactory<PSourceSyntaxFeatures>); + ff_registry.Register("SourceSyntaxFeatures2", new FFFactory<SourceSyntaxFeatures2>); ff_registry.Register("ParseMatchFeatures", new FFFactory<ParseMatchFeatures>); ff_registry.Register("SourceSpanSizeFeatures", new FFFactory<SourceSpanSizeFeatures>); //ff_registry.Register("PSourceSpanSizeFeatures", new FFFactory<PSourceSpanSizeFeatures>); 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 <sstream> +#include <stack> +#include <string> +#include <tr1/unordered_set> + +#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<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) { + 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<double>* 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<WordID> src_tree; // src_tree(i,j) NT = type + mutable Array2D<map<const TRule*, int> > fids_ef; // fires for fully lexicalized + tr1::unordered_set<int> 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<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 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<const void*>& ant_contexts, + SparseVector<double>* features, + SparseVector<double>* 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 <sstream> +#include <stack> +#include <string> +#include <tr1/unordered_set> + +#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<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) { + 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<double>* 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<WordID> src_tree; // src_tree(i,j) NT = type + mutable Array2D<map<const TRule*, int> > fids_ef; // fires for fully lexicalized + tr1::unordered_set<int> 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<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 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<const void*>& ant_contexts, + SparseVector<double>* features, + SparseVector<double>* estimated_features, + void* context) const; + virtual void PrepareForInput(const SentenceMetadata& smeta); + private: + SourceSyntaxFeatures2Impl* impl; +}; + +#endif |