From fb515d6fe01c65c924d0806619fa938688335579 Mon Sep 17 00:00:00 2001 From: redpony Date: Tue, 29 Jun 2010 21:28:11 +0000 Subject: add joshua visualization support git-svn-id: https://ws10smt.googlecode.com/svn/trunk@69 ec762483-ff6d-05da-a07a-a48fb63a330f --- decoder/cdec.cc | 9 +++++++-- decoder/viterbi.cc | 31 +++++++++++++++++++++++++++++++ decoder/viterbi.h | 1 + 3 files changed, 39 insertions(+), 2 deletions(-) (limited to 'decoder') diff --git a/decoder/cdec.cc b/decoder/cdec.cc index 1d7b43f0..2353cab3 100644 --- a/decoder/cdec.cc +++ b/decoder/cdec.cc @@ -75,7 +75,8 @@ void InitCommandLine(int argc, char** argv, po::variables_map* conf) { ("scfg_no_hiero_glue_grammar,n", "No Hiero glue grammar (nb. by default the SCFG decoder adds Hiero glue rules)") ("scfg_default_nt,d",po::value()->default_value("X"),"Default non-terminal symbol in SCFG") ("scfg_max_span_limit,S",po::value()->default_value(10),"Maximum non-terminal span limit (except \"glue\" grammar)") - ("show_tree_structure", "Show the Viterbi derivation structure") + ("show_joshua_visualization,J", "Produce output compatible with the Joshua visualization tools") + ("show_tree_structure", "Show the Viterbi derivation structure") ("show_expected_length", "Show the expected translation length under the model") ("show_partition,z", "Compute and show the partition (inside score)") ("show_cfg_search_space", "Show the search space as a CFG") @@ -326,6 +327,7 @@ int main(int argc, char** argv) { const bool aligner_mode = conf.count("aligner"); const bool minimal_forests = conf.count("minimal_forests"); const bool graphviz = conf.count("graphviz"); + const bool joshua_viz = conf.count("show_joshua_visualization"); const bool encode_b64 = conf["vector_format"].as() == "b64"; const bool kbest = conf.count("k_best"); const bool unique_kbest = conf.count("unique_k_best"); @@ -466,9 +468,12 @@ int main(int argc, char** argv) { } else if (csplit_output_plf) { cout << HypergraphIO::AsPLF(forest, false) << endl; } else { - if (!graphviz && !has_ref) { + if (!graphviz && !has_ref && !joshua_viz) { cout << TD::GetString(trans) << endl << flush; } + if (joshua_viz) { + cout << sent_id << " ||| " << JoshuaVisualizationString(forest) << " ||| 1.0 ||| " << -1.0 << endl << flush; + } } } 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 #include #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*>& ants, + std::vector* result) const { + std::vector 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 tmp; + const prob_t p = Viterbi, JoshuaVisTraversal, prob_t, EdgeProb>(hg, &tmp); + return TD::GetString(tmp); +} + diff --git a/decoder/viterbi.h b/decoder/viterbi.h index 8f7534a9..dd54752a 100644 --- a/decoder/viterbi.h +++ b/decoder/viterbi.h @@ -132,6 +132,7 @@ struct ViterbiPathTraversal { } }; +std::string JoshuaVisualizationString(const Hypergraph& hg); prob_t ViterbiESentence(const Hypergraph& hg, std::vector* result); std::string ViterbiETree(const Hypergraph& hg); prob_t ViterbiFSentence(const Hypergraph& hg, std::vector* result); -- cgit v1.2.3