From 2c6c1d2c312e13d5434d961d486e86e630c1aefc Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@lis-lab.fr>
Date: Mon, 16 Dec 2019 22:08:40 +0100
Subject: [PATCH] Improved printForDebug

---
 common/include/util.hpp            | 12 +++++++
 dev/src/dev.cpp                    |  3 ++
 reading_machine/include/Config.hpp |  4 ++-
 reading_machine/src/Config.cpp     | 54 +++++++++++++++++++++++++-----
 4 files changed, 63 insertions(+), 10 deletions(-)

diff --git a/common/include/util.hpp b/common/include/util.hpp
index 1363383..a207f25 100644
--- a/common/include/util.hpp
+++ b/common/include/util.hpp
@@ -54,6 +54,18 @@ bool isEmpty(const std::basic_string<T> & s)
   return s.empty();
 }
 
+template <typename T>
+std::size_t getSize(const std::vector<T> & s)
+{
+  return s.size();
+}
+
+template <typename T>
+std::size_t getSize(const boost::flyweight<T> & s)
+{
+  return getSize(s.get());
+}
+
 template <typename T>
 bool isEmpty(const boost::flyweight<T> & s)
 {
diff --git a/dev/src/dev.cpp b/dev/src/dev.cpp
index 4604fba..12f9f77 100644
--- a/dev/src/dev.cpp
+++ b/dev/src/dev.cpp
@@ -16,6 +16,9 @@ int main(int argc, char * argv[])
   for (int i = 0; i < 2; i++)
     configs.emplace_back(config);
 
+  configs[0].addToHistory("LEFT");
+  configs[0].addToHistory("RIGHT");
+
   configs[0].wordIndex = 2000;
   configs[0].update();
   configs[0].printForDebug(stdout);
diff --git a/reading_machine/include/Config.hpp b/reading_machine/include/Config.hpp
index dee137d..58b2fc8 100644
--- a/reading_machine/include/Config.hpp
+++ b/reading_machine/include/Config.hpp
@@ -71,7 +71,9 @@ class Config
   const String & getConst(const std::string & colName, int lineIndex, int hypothesisIndex) const;
   String & getLastNotEmpty(const std::string & colName, int lineIndex);
   const String & getLastNotEmptyConst(const std::string & colName, int lineIndex) const;
-  void addToHistory(const String & transition);
+  bool hasLetter(int letterIndex) const;
+  util::utf8char getLetter(int letterIndex) const;
+  void addToHistory(const std::string & transition);
 };
 
 #endif
diff --git a/reading_machine/src/Config.cpp b/reading_machine/src/Config.cpp
index 297b286..6e84e06 100644
--- a/reading_machine/src/Config.cpp
+++ b/reading_machine/src/Config.cpp
@@ -75,6 +75,8 @@ void Config::print(FILE * dest) const
 void Config::printForDebug(FILE * dest) const
 {
   static constexpr int windowSize = 5;
+  static constexpr int lettersWindowSize = 40;
+
   int firstLineToPrint = wordIndex;
   int lastLineToPrint = wordIndex;
   while (wordIndex-firstLineToPrint < windowSize and has(0, firstLineToPrint, 0))
@@ -84,6 +86,11 @@ void Config::printForDebug(FILE * dest) const
 
   std::vector<std::vector<std::string>> toPrint;
 
+  toPrint.emplace_back();
+  toPrint.back().emplace_back("");
+  for (unsigned int i = 0; i < getNbColumns(); i++)
+    toPrint.back().emplace_back(getColName(i));
+
   for (int line = firstLineToPrint; line <= lastLineToPrint; line++)
   {
     toPrint.emplace_back();
@@ -97,14 +104,33 @@ void Config::printForDebug(FILE * dest) const
     for (unsigned int col = 0; col < line.size()-1; col++)
       colLength[col] = std::max((int)colLength[col], util::printedLength(line[col]));
 
-  for (auto & line : toPrint)
+  int lengthSum = 2*getNbColumns();
+  for (auto & val : colLength)
+    lengthSum += val;
+  std::string longLine = fmt::format("{:-<{}}", "", lengthSum);
+
+  std::string historyStr = "";
+  for (auto & h : history)
   {
-    for (unsigned int col = 0; col < line.size()-1; col++)
-      if (col == 0)
-        fmt::print(dest, "{:>{}}", line[col], colLength[col]);
-      else
-        fmt::print(dest, "{:<{}}{}", line[col], colLength[col], col == line.size()-2 ? "\n" : "\t");
-    if (line.back() == EOSSymbol1)
+    historyStr += h;
+    historyStr += ",";
+  }
+  if (!historyStr.empty())
+    historyStr.pop_back();
+  fmt::print(dest, "{}\n", longLine);
+  for (std::size_t index = characterIndex; index < util::getSize(rawInput) and index - characterIndex < lettersWindowSize; index++)
+    fmt::print(dest, "{}", getLetter(index));
+  fmt::print(dest, "\n{}\n", longLine);
+  fmt::print(dest, "wordIndex={} characterIndex={}\nhistory=({})\n", wordIndex, characterIndex, historyStr);
+  fmt::print(dest, "{}\n", longLine);
+
+  for (unsigned int line = 0; line < toPrint.size(); line++)
+  {
+    if (line == 1)
+      fmt::print(dest, "{}\n", longLine);
+    for (unsigned int col = 0; col < toPrint[line].size()-1; col++)
+      fmt::print(dest, "{}{:>{}}{}", toPrint[line][col], "", colLength[col]-util::printedLength(toPrint[line][col]), col == toPrint[line].size()-2 ? "\n" : "\t");
+    if (toPrint[line].back() == EOSSymbol1)
       fmt::print(dest, "\n");
   }
 }
@@ -151,8 +177,18 @@ Config::ConstValueIterator Config::getConstIterator(int colIndex, int lineIndex,
   return lines.begin() + getIndexOfLine(lineIndex-getFirstLineIndex()) + getIndexOfCol(colIndex) + hypothesisIndex;
 }
 
-void Config::addToHistory(const Config::String & transition)
+void Config::addToHistory(const std::string & transition)
+{
+  history.push_back(String(transition));
+}
+
+bool Config::hasLetter(int letterIndex) const
+{
+  return letterIndex >= 0 and letterIndex < (int)util::getSize(rawInput);
+}
+
+util::utf8char Config::getLetter(int letterIndex) const
 {
-  history.push_back(transition);
+  return rawInput.get()[letterIndex];
 }
 
-- 
GitLab