summaryrefslogtreecommitdiff
path: root/decoder/ff_rules.cc
diff options
context:
space:
mode:
authorChris Dyer <cdyer@cs.cmu.edu>2012-11-05 21:34:22 -0500
committerChris Dyer <cdyer@cs.cmu.edu>2012-11-05 21:34:22 -0500
commit709bf16fbf7e88d15c9a9f3356c63e9ff38fa05d (patch)
tree3ec67e4084b6be65555d70766ebfe5d2b18cd410 /decoder/ff_rules.cc
parent782fb27af98ed98256cc25c832131c59c8e9ce9c (diff)
parent2eb77b77d28f6d9dc88ecb6ca999994bdb555445 (diff)
Merge branch 'master' of https://github.com/redpony/cdec
Diffstat (limited to 'decoder/ff_rules.cc')
-rw-r--r--decoder/ff_rules.cc48
1 files changed, 43 insertions, 5 deletions
diff --git a/decoder/ff_rules.cc b/decoder/ff_rules.cc
index 0aafb0ba..6716d3da 100644
--- a/decoder/ff_rules.cc
+++ b/decoder/ff_rules.cc
@@ -68,15 +68,15 @@ void RuleIdentityFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta,
features->add_value(it->second, 1);
}
-RuleNgramFeatures::RuleNgramFeatures(const std::string& param) {
+RuleSourceBigramFeatures::RuleSourceBigramFeatures(const std::string& param) {
}
-void RuleNgramFeatures::PrepareForInput(const SentenceMetadata& smeta) {
+void RuleSourceBigramFeatures::PrepareForInput(const SentenceMetadata& smeta) {
// std::map<const TRule*, SparseVector<double> >
rule2_feats_.clear();
}
-void RuleNgramFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta,
+void RuleSourceBigramFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta,
const Hypergraph::Edge& edge,
const vector<const void*>& ant_contexts,
SparseVector<double>* features,
@@ -94,14 +94,52 @@ void RuleNgramFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta,
assert(w > 0);
const string& cur = TD::Convert(w);
ostringstream os;
- os << "RB:" << prev << '_' << cur;
+ os << "RBS:" << prev << '_' << cur;
const int fid = FD::Convert(Escape(os.str()));
if (fid <= 0) return;
f.add_value(fid, 1.0);
prev = cur;
}
ostringstream os;
- os << "RB:" << prev << '_' << "</r>";
+ os << "RBS:" << prev << '_' << "</r>";
+ f.set_value(FD::Convert(Escape(os.str())), 1.0);
+ }
+ (*features) += it->second;
+}
+
+RuleTargetBigramFeatures::RuleTargetBigramFeatures(const std::string& param) {
+}
+
+void RuleTargetBigramFeatures::PrepareForInput(const SentenceMetadata& smeta) {
+ rule2_feats_.clear();
+}
+
+void RuleTargetBigramFeatures::TraversalFeaturesImpl(const SentenceMetadata& smeta,
+ const Hypergraph::Edge& edge,
+ const vector<const void*>& ant_contexts,
+ SparseVector<double>* features,
+ SparseVector<double>* estimated_features,
+ void* context) const {
+ map<const TRule*, SparseVector<double> >::iterator it = rule2_feats_.find(edge.rule_.get());
+ if (it == rule2_feats_.end()) {
+ const TRule& rule = *edge.rule_;
+ it = rule2_feats_.insert(make_pair(&rule, SparseVector<double>())).first;
+ SparseVector<double>& f = it->second;
+ string prev = "<r>";
+ for (int i = 0; i < rule.e_.size(); ++i) {
+ WordID w = rule.e_[i];
+ if (w < 0) w = -w;
+ if (w == 0) return;
+ const string& cur = TD::Convert(w);
+ ostringstream os;
+ os << "RBT:" << prev << '_' << cur;
+ const int fid = FD::Convert(Escape(os.str()));
+ if (fid <= 0) return;
+ f.add_value(fid, 1.0);
+ prev = cur;
+ }
+ ostringstream os;
+ os << "RBT:" << prev << '_' << "</r>";
f.set_value(FD::Convert(Escape(os.str())), 1.0);
}
(*features) += it->second;