From 6da0534d0335e60e349582590e1aa60907c84171 Mon Sep 17 00:00:00 2001
From: Kenneth Heafield <github@kheafield.com>
Date: Tue, 8 May 2012 14:57:45 -0400
Subject: Output separate files for target hypergraphs.

---
 decoder/decoder.cc |  4 ++--
 decoder/hg_io.cc   | 17 +++++++++++------
 decoder/hg_io.h    |  3 ++-
 3 files changed, 15 insertions(+), 9 deletions(-)

(limited to 'decoder')

diff --git a/decoder/decoder.cc b/decoder/decoder.cc
index ec6f75f7..4ce2ba86 100644
--- a/decoder/decoder.cc
+++ b/decoder/decoder.cc
@@ -407,7 +407,7 @@ DecoderImpl::DecoderImpl(po::variables_map& conf, int argc, char** argv, istream
         ("show_partition,z", "Compute and show the partition (inside score)")
         ("show_conditional_prob", "Output the conditional log prob to STDOUT instead of a translation")
         ("show_cfg_search_space", "Show the search space as a CFG")
-        ("show_target_graph", "Output the target hypergraph")
+        ("show_target_graph", po::value<string>(), "Directory to write the target hypergraphs to")
         ("coarse_to_fine_beam_prune", po::value<double>(), "Prune paths from coarse parse forest before fine parse, keeping paths within exp(alpha>=0)")
         ("ctf_beam_widen", po::value<double>()->default_value(2.0), "Expand coarse pass beam by this factor if no fine parse is found")
         ("ctf_num_widenings", po::value<int>()->default_value(2), "Widen coarse beam this many times before backing off to full parse")
@@ -816,7 +816,7 @@ bool DecoderImpl::Decode(const string& input, DecoderObserver* o) {
   }
 
   if (conf.count("show_target_graph"))
-    HypergraphIO::WriteTarget(forest);
+    HypergraphIO::WriteTarget(conf["show_target_graph"].as<string>(), sent_id, forest);
 
   for (int pass = 0; pass < rescoring_passes.size(); ++pass) {
     const RescoringPass& rp = rescoring_passes[pass];
diff --git a/decoder/hg_io.cc b/decoder/hg_io.cc
index d416dbf6..734c2ce8 100644
--- a/decoder/hg_io.cc
+++ b/decoder/hg_io.cc
@@ -1,5 +1,6 @@
 #include "hg_io.h"
 
+#include <fstream>
 #include <sstream>
 #include <iostream>
 
@@ -651,22 +652,26 @@ void HypergraphIO::WriteAsCFG(const Hypergraph& hg) {
  *   for each downward edge:
  *     RHS with [vertex_index] for NTs ||| scores
  */
-void HypergraphIO::WriteTarget(const Hypergraph& hg) {
-  cout << hg.nodes_.size() << ' ' << hg.edges_.size() << '\n';
+void HypergraphIO::WriteTarget(const std::string &base, unsigned int id, const Hypergraph& hg) {
+  std::string name(base);
+  name += '/';
+  name += boost::lexical_cast<std::string>(id);
+  std::fstream out(name.c_str(), std::fstream::out);
+  out << hg.nodes_.size() << ' ' << hg.edges_.size() << '\n';
   for (unsigned int i = 0; i < hg.nodes_.size(); ++i) {
     const Hypergraph::EdgesVector &edges = hg.nodes_[i].in_edges_;
-    cout << edges.size() << '\n';
+    out << edges.size() << '\n';
     for (unsigned int j = 0; j < edges.size(); ++j) {
       const Hypergraph::Edge &edge = hg.edges_[edges[j]];
       const std::vector<WordID> &e = edge.rule_->e();
       for (std::vector<WordID>::const_iterator word = e.begin(); word != e.end(); ++word) {
         if (*word <= 0) {
-          cout << '[' << edge.tail_nodes_[-*word] << "] ";
+          out << '[' << edge.tail_nodes_[-*word] << "] ";
         } else {
-          cout << TD::Convert(*word) << ' ';
+          out << TD::Convert(*word) << ' ';
         }
       }
-      cout << "||| " << edge.rule_->scores_ << '\n';
+      out << "||| " << edge.rule_->scores_ << '\n';
     }
   }
 }
diff --git a/decoder/hg_io.h b/decoder/hg_io.h
index 4e502a0c..58af8132 100644
--- a/decoder/hg_io.h
+++ b/decoder/hg_io.h
@@ -2,6 +2,7 @@
 #define _HG_IO_H_
 
 #include <iostream>
+#include <string>
 #include "lattice.h"
 
 class Hypergraph;
@@ -24,7 +25,7 @@ struct HypergraphIO {
   static void WriteAsCFG(const Hypergraph& hg);
 
   // Write only the target size information in bottom-up order.  
-  static void WriteTarget(const Hypergraph& hg);
+  static void WriteTarget(const std::string &base, unsigned int sent_id, const Hypergraph& hg);
 
   // serialization utils
   static void ReadFromPLF(const std::string& in, Hypergraph* out, int line = 0);
-- 
cgit v1.2.3