summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--decoder/cdec.cc9
-rw-r--r--decoder/viterbi.cc31
-rw-r--r--decoder/viterbi.h1
3 files changed, 39 insertions, 2 deletions
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<string>()->default_value("X"),"Default non-terminal symbol in SCFG")
("scfg_max_span_limit,S",po::value<int>()->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<string>() == "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 <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);
+}
+
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<WordID>* result);
std::string ViterbiETree(const Hypergraph& hg);
prob_t ViterbiFSentence(const Hypergraph& hg, std::vector<WordID>* result);