diff options
author | redpony <redpony@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-06-29 21:28:11 +0000 |
---|---|---|
committer | redpony <redpony@ec762483-ff6d-05da-a07a-a48fb63a330f> | 2010-06-29 21:28:11 +0000 |
commit | 9f211fce859163cd26b1d8a0b7632cb300b0f67f (patch) | |
tree | febdc1cefc3aa072f94cd0af1d8e127cd31b2b75 /decoder/viterbi.cc | |
parent | 6dc4c9ee176b565280b6e6c51a909fea5d1d5ca6 (diff) |
add joshua visualization support
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@69 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'decoder/viterbi.cc')
-rw-r--r-- | decoder/viterbi.cc | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/decoder/viterbi.cc b/decoder/viterbi.cc index 82b2ce6d..582dc5b2 100644 --- a/decoder/viterbi.cc +++ b/decoder/viterbi.cc @@ -1,5 +1,6 @@ #include "viterbi.h" +#include <sstream> #include <vector> #include "hg.h" @@ -37,3 +38,33 @@ int ViterbiPathLength(const Hypergraph& hg) { return len; } +// create a strings of the form (S (X the man) (X said (X he (X would (X go))))) +struct JoshuaVisTraversal { + JoshuaVisTraversal() : left("("), space(" "), right(")") {} + const std::string left; + const std::string space; + const std::string right; + void operator()(const Hypergraph::Edge& edge, + const std::vector<const std::vector<WordID>*>& ants, + std::vector<WordID>* result) const { + std::vector<WordID> tmp; + edge.rule_->ESubstitute(ants, &tmp); + const std::string cat = TD::Convert(edge.rule_->GetLHS() * -1); + if (cat == "Goal") + result->swap(tmp); + else { + ostringstream os; + os << left << cat << '{' << edge.i_ << '-' << edge.j_ << '}' + << space << TD::GetString(tmp) << right; + TD::ConvertSentence(os.str(), + result); + } + } +}; + +string JoshuaVisualizationString(const Hypergraph& hg) { + vector<WordID> tmp; + const prob_t p = Viterbi<vector<WordID>, JoshuaVisTraversal, prob_t, EdgeProb>(hg, &tmp); + return TD::GetString(tmp); +} + |