summaryrefslogtreecommitdiff
path: root/decoder/lazy.cc
diff options
context:
space:
mode:
authorKenneth Heafield <github@kheafield.com>2012-09-13 04:04:17 -0700
committerKenneth Heafield <github@kheafield.com>2012-09-13 04:04:17 -0700
commit8d0baa1dfb5c18f5be45b4d12b3f925dc04afc48 (patch)
treeaf96b81bfa50321d36a02bc036eabfeb29c32538 /decoder/lazy.cc
parenta548c46f3d636093d26f44d5b7642c98def6f351 (diff)
parent816f33174b2c2938a3df9c75b2834da6f5184a3e (diff)
Merge branch 'master' of github.com:kpu/cdec
Diffstat (limited to 'decoder/lazy.cc')
-rw-r--r--decoder/lazy.cc78
1 files changed, 57 insertions, 21 deletions
diff --git a/decoder/lazy.cc b/decoder/lazy.cc
index f5b61c75..4776c1b8 100644
--- a/decoder/lazy.cc
+++ b/decoder/lazy.cc
@@ -1,15 +1,23 @@
#include "hg.h"
#include "lazy.h"
+#include "fdict.h"
#include "tdict.h"
#include "lm/enumerate_vocab.hh"
#include "lm/model.hh"
+#include "search/config.hh"
+#include "search/context.hh"
#include "search/edge.hh"
#include "search/vertex.hh"
+#include "search/vertex_generator.hh"
#include "util/exception.hh"
+#include <boost/scoped_ptr.hpp>
#include <boost/scoped_array.hpp>
+#include <iostream>
+#include <vector>
+
namespace {
struct MapVocab : public lm::EnumerateVocab {
@@ -19,13 +27,13 @@ struct MapVocab : public lm::EnumerateVocab {
// Do not call after Lookup.
void Add(lm::WordIndex index, const StringPiece &str) {
const WordID cdec_id = TD::Convert(str.as_string());
- if (cdec_id >= out_->size()) out_.resize(cdec_id + 1);
+ if (cdec_id >= out_.size()) out_.resize(cdec_id + 1);
out_[cdec_id] = index;
}
// Assumes Add has been called and will never be called again.
lm::WordIndex FromCDec(WordID id) const {
- return out_[out.size() > id ? id : 0];
+ return out_[out_.size() > id ? id : 0];
}
private:
@@ -34,44 +42,50 @@ struct MapVocab : public lm::EnumerateVocab {
class LazyBase {
public:
- LazyBase() {}
+ LazyBase(const std::vector<weight_t> &weights) :
+ cdec_weights_(weights),
+ config_(search::Weights(weights[FD::Convert("KLanguageModel")], weights[FD::Convert("KLanguageModel_OOV")], weights[FD::Convert("WordPenalty")]), 1000) {}
virtual ~LazyBase() {}
virtual void Search(const Hypergraph &hg) const = 0;
- static LazyBase *Load(const char *model_file);
+ static LazyBase *Load(const char *model_file, const std::vector<weight_t> &weights);
protected:
- lm::ngram::Config GetConfig() const {
+ lm::ngram::Config GetConfig() {
lm::ngram::Config ret;
ret.enumerate_vocab = &vocab_;
return ret;
}
MapVocab vocab_;
+
+ const std::vector<weight_t> &cdec_weights_;
+
+ const search::Config config_;
};
template <class Model> class Lazy : public LazyBase {
public:
- explicit Lazy(const char *model_file) : m_(model_file, GetConfig()) {}
+ Lazy(const char *model_file, const std::vector<weight_t> &weights) : LazyBase(weights), m_(model_file, GetConfig()) {}
void Search(const Hypergraph &hg) const;
private:
- void ConvertEdge(const Context<Model> &context, bool final, search::Vertex *vertices, const Hypergraph::Edge &in, search::Edge &out) const;
+ void ConvertEdge(const search::Context<Model> &context, bool final, search::Vertex *vertices, const Hypergraph::Edge &in, search::Edge &out) const;
const Model m_;
};
-static LazyBase *LazyBase::Load(const char *model_file) {
+LazyBase *LazyBase::Load(const char *model_file, const std::vector<weight_t> &weights) {
lm::ngram::ModelType model_type;
- if (!lm::ngram::RecognizeBinary(lm_name, model_type)) model_type = lm::ngram::PROBING;
+ if (!lm::ngram::RecognizeBinary(model_file, model_type)) model_type = lm::ngram::PROBING;
switch (model_type) {
case lm::ngram::PROBING:
- return new Lazy<lm::ngram::ProbingModel>(model_file);
+ return new Lazy<lm::ngram::ProbingModel>(model_file, weights);
case lm::ngram::REST_PROBING:
- return new Lazy<lm::ngram::RestProbingModel>(model_file);
+ return new Lazy<lm::ngram::RestProbingModel>(model_file, weights);
default:
UTIL_THROW(util::Exception, "Sorry this lm type isn't supported yet.");
}
@@ -80,25 +94,41 @@ static LazyBase *LazyBase::Load(const char *model_file) {
template <class Model> void Lazy<Model>::Search(const Hypergraph &hg) const {
boost::scoped_array<search::Vertex> out_vertices(new search::Vertex[hg.nodes_.size()]);
boost::scoped_array<search::Edge> out_edges(new search::Edge[hg.edges_.size()]);
+
+ search::Context<Model> context(config_, m_);
+
for (unsigned int i = 0; i < hg.nodes_.size(); ++i) {
- search::Vertex *out_vertex = out_vertices[i];
+ search::Vertex &out_vertex = out_vertices[i];
const Hypergraph::EdgesVector &down_edges = hg.nodes_[i].in_edges_;
- for (unsigned int j = 0; j < edges.size(); ++j) {
+ for (unsigned int j = 0; j < down_edges.size(); ++j) {
unsigned int edge_index = down_edges[j];
- const Hypergraph::Edge &in_edge = hg.edges_[edge_index];
- search::Edge &out_edge = out_edges[edge_index];
+ ConvertEdge(context, i == hg.nodes_.size() - 1, out_vertices.get(), hg.edges_[edge_index], out_edges[edge_index]);
+ out_vertex.Add(out_edges[edge_index]);
}
+ out_vertex.FinishedAdding();
+ search::VertexGenerator(context, out_vertex);
+ }
+ search::PartialVertex top = out_vertices[hg.nodes_.size() - 1].RootPartial();
+ if (top.Empty()) {
+ std::cout << "NO PATH FOUND";
+ } else {
+ search::PartialVertex continuation;
+ while (!top.Complete()) {
+ top.Split(continuation);
+ top = continuation;
+ }
+ std::cout << top.End().Bound() << std::endl;
}
}
// TODO: get weights into here somehow.
-template <class Model> void Lazy<Model>::ConvertEdge(const Context<Model> &context, bool final, search::Vertices *vertices, const Hypergraph::Edge &in, search::Edge &out) const {
- const std::vector<WordID> &e = in_edge.rule_->e();
+template <class Model> void Lazy<Model>::ConvertEdge(const search::Context<Model> &context, bool final, search::Vertex *vertices, const Hypergraph::Edge &in, search::Edge &out) const {
+ const std::vector<WordID> &e = in.rule_->e();
std::vector<lm::WordIndex> words;
unsigned int terminals = 0;
for (std::vector<WordID>::const_iterator word = e.begin(); word != e.end(); ++word) {
if (*word <= 0) {
- out.Add(vertices[edge.tail_nodes_[-*word]]);
+ out.Add(vertices[in.tail_nodes_[-*word]]);
words.push_back(lm::kMaxWordIndex);
} else {
++terminals;
@@ -110,13 +140,19 @@ template <class Model> void Lazy<Model>::ConvertEdge(const Context<Model> &conte
words.push_back(m_.GetVocabulary().EndSentence());
}
- float additive = edge.rule_->GetFeatureValues().dot(weight_vector);
+ float additive = in.rule_->GetFeatureValues().dot(cdec_weights_);
+ additive -= terminals * context.GetWeights().WordPenalty() * static_cast<float>(terminals) / M_LN10;
out.InitRule().Init(context, additive, words, final);
}
-} // namespace
+boost::scoped_ptr<LazyBase> AwfulGlobalLazy;
-void PassToLazy(const Hypergraph &hg) {
+} // namespace
+void PassToLazy(const Hypergraph &hg, const std::vector<weight_t> &weights) {
+ if (!AwfulGlobalLazy.get()) {
+ AwfulGlobalLazy.reset(LazyBase::Load("lm", weights));
+ }
+ AwfulGlobalLazy->Search(hg);
}