From 5b15010287fed1c960fe391e7ccd55976f77571c Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@lis-lab.fr>
Date: Sun, 23 Feb 2020 17:19:40 +0100
Subject: [PATCH] Made function getAsFeature to get element from hyp or ref
 depending on wether its column is predicted or not

---
 reading_machine/include/Config.hpp |  2 ++
 reading_machine/src/Config.cpp     | 27 ++++++++++++++++++++-------
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/reading_machine/include/Config.hpp b/reading_machine/include/Config.hpp
index 21dab0b..833a2f9 100644
--- a/reading_machine/include/Config.hpp
+++ b/reading_machine/include/Config.hpp
@@ -67,6 +67,7 @@ class Config
   String & getLastNotEmptyHyp(int colIndex, int lineIndex);
   const String & getLastNotEmptyHypConst(int colIndex, int lineIndex) const;
   const String & getLastNotEmptyConst(int colIndex, int lineIndex) const;
+  const String & getAsFeature(int colIndex, int lineIndex) const;
   ValueIterator getIterator(int colIndex, int lineIndex, int hypothesisIndex);
   ConstValueIterator getConstIterator(int colIndex, int lineIndex, int hypothesisIndex) const;
 
@@ -82,6 +83,7 @@ class Config
   const String & getLastNotEmptyConst(const std::string & colName, int lineIndex) const;
   String & getLastNotEmptyHyp(const std::string & colName, int lineIndex);
   const String & getLastNotEmptyHypConst(const std::string & colName, int lineIndex) const;
+  const String & getAsFeature(const std::string & colName, int lineIndex) const;
   String & getFirstEmpty(int colIndex, int lineIndex);
   String & getFirstEmpty(const std::string & colName, int lineIndex);
   bool hasCharacter(int letterIndex) const;
diff --git a/reading_machine/src/Config.cpp b/reading_machine/src/Config.cpp
index ca81d9e..8ced9da 100644
--- a/reading_machine/src/Config.cpp
+++ b/reading_machine/src/Config.cpp
@@ -91,20 +91,20 @@ void Config::print(FILE * dest) const
     }
     for (unsigned int i = 0; i < getNbColumns()-1; i++)
     {
-      auto & colContent = isPredicted(getColName(i)) ? getLastNotEmptyHypConst(i, getFirstLineIndex()+line) : getLastNotEmptyConst(i, getFirstLineIndex()+line);
+      auto & colContent = getAsFeature(i, getFirstLineIndex()+line);
       std::string valueToPrint = colContent;
       try
       {
         if (getColName(i) == headColName)
           if (valueToPrint != "0")
-            valueToPrint = getLastNotEmptyConst(idColName, std::stoi(valueToPrint));
+            valueToPrint = getAsFeature(idColName, std::stoi(valueToPrint));
       } catch(std::exception &) {}
       if (valueToPrint.empty())
         valueToPrint = "_";
 
       currentSequence.emplace_back(fmt::format("{}{}", valueToPrint, i < getNbColumns()-2 ? "\t" : "\n"));
     }
-    auto & eosColContent = isPredicted(EOSColName) ? getLastNotEmptyHypConst(EOSColName, getFirstLineIndex()+line) : getLastNotEmptyConst(EOSColName, getFirstLineIndex()+line);
+    auto & eosColContent = getAsFeature(EOSColName, getFirstLineIndex()+line);
     if (eosColContent == EOSSymbol1)
       flushCurrentSequence();
   }
@@ -142,14 +142,14 @@ void Config::printForDebug(FILE * dest) const
     toPrint.back().emplace_back(line == (int)wordIndex ? "=>" : "");
     for (unsigned int i = 0; i < getNbColumns(); i++)
     {
-      auto & colContent = isPredicted(getColName(i)) ? getLastNotEmptyHypConst(i, line) : getLastNotEmptyConst(i, line);
+      auto & colContent = getAsFeature(i, line);
       std::string toPrintCol = colContent;
       try
       {
         if (getColName(i) == headColName)
           if (toPrintCol != "0")
-            toPrintCol = getLastNotEmptyConst(idColName, std::stoi(toPrintCol));
-      } catch(std::exception &) {}
+            toPrintCol = getAsFeature(idColName, std::stoi(toPrintCol));
+      } catch(std::exception & e) {util::myThrow(fmt::format("toPrintCol='{}' {}", toPrintCol, e.what()));}
       toPrint.back().emplace_back(util::shrink(toPrintCol, maxWordLength));
     }
   }
@@ -178,7 +178,7 @@ void Config::printForDebug(FILE * dest) const
     if (hasColIndex(idColName))
     {
       if (has(idColName, s, 0))
-        stackStr += getLastNotEmptyConst(idColName, s);
+        stackStr += getAsFeature(idColName, s);
       else
         stackStr += "?";
     }
@@ -267,6 +267,19 @@ const Config::String & Config::getLastNotEmptyHypConst(int colIndex, int lineInd
   return lines[baseIndex+1];
 }
 
+const Config::String & Config::getAsFeature(int colIndex, int lineIndex) const
+{
+  if (isPredicted(getColName(colIndex)))
+    return getLastNotEmptyHypConst(colIndex, lineIndex);
+
+  return getLastNotEmptyConst(colIndex, lineIndex);
+}
+
+const Config::String & Config::getAsFeature(const std::string & colName, int lineIndex) const
+{
+  return getAsFeature(getColIndex(colName), lineIndex);
+}
+
 Config::String & Config::getLastNotEmpty(const std::string & colName, int lineIndex)
 {
   return getLastNotEmpty(getColIndex(colName), lineIndex);
-- 
GitLab