mosesdecoder/search/vertex_generator.hh

76 lines
1.9 KiB
C++
Raw Normal View History

2012-10-15 16:58:33 +04:00
#ifndef SEARCH_VERTEX_GENERATOR__
#define SEARCH_VERTEX_GENERATOR__
#include "search/edge.hh"
2012-11-15 22:04:07 +04:00
#include "search/types.hh"
2012-10-15 16:58:33 +04:00
#include "search/vertex.hh"
namespace lm {
namespace ngram {
class ChartState;
} // namespace ngram
} // namespace lm
namespace search {
class ContextBase;
2012-11-15 22:04:07 +04:00
// Output makes the single-best or n-best list.
template <class Output> class VertexGenerator {
2012-10-15 16:58:33 +04:00
public:
VertexGenerator(ContextBase &context, Vertex &gen, Output &nbest) : context_(context), gen_(gen), nbest_(nbest) {}
2012-10-15 16:58:33 +04:00
void NewHypothesis(PartialEdge partial) {
2012-11-15 22:04:07 +04:00
nbest_.Add(existing_[hash_value(partial.CompletedState())], partial);
2012-10-15 16:58:33 +04:00
}
2012-11-15 22:04:07 +04:00
void FinishedSearch() {
gen_.root_.InitRoot();
2012-11-15 22:04:07 +04:00
for (typename Existing::iterator i(existing_.begin()); i != existing_.end(); ++i) {
gen_.root_.AppendHypothesis(nbest_.Complete(i->second));
2012-11-15 22:04:07 +04:00
}
existing_.clear();
gen_.root_.FinishRoot();
2012-11-15 22:04:07 +04:00
}
Vertex &Generating() { return gen_; }
2012-10-15 16:58:33 +04:00
private:
ContextBase &context_;
Vertex &gen_;
2012-11-15 22:04:07 +04:00
typedef boost::unordered_map<uint64_t, typename Output::Combine> Existing;
2012-10-15 16:58:33 +04:00
Existing existing_;
2012-11-15 22:04:07 +04:00
Output &nbest_;
};
// Special case for root vertex: everything should come together into the root
// node. In theory, this should happen naturally due to state collapsing with
// <s> and </s>. If that's the case, VertexGenerator is fine, though it will
// make one connection.
template <class Output> class RootVertexGenerator {
public:
RootVertexGenerator(Vertex &gen, Output &out) : gen_(gen), out_(out) {}
void NewHypothesis(PartialEdge partial) {
out_.Add(combine_, partial);
}
void FinishedSearch() {
gen_.root_.InitRoot();
gen_.root_.AppendHypothesis(out_.Complete(combine_));
gen_.root_.FinishRoot();
2012-11-15 22:04:07 +04:00
}
private:
Vertex &gen_;
typename Output::Combine combine_;
Output &out_;
2012-10-15 16:58:33 +04:00
};
} // namespace search
#endif // SEARCH_VERTEX_GENERATOR__