From c43918ed976098cd758a56c90dec66ed615cd8f9 Mon Sep 17 00:00:00 2001
From: Franck Dary <franck.dary@lis-lab.fr>
Date: Wed, 23 Oct 2019 13:33:51 +0200
Subject: [PATCH] Fixed EOS when it's ref and predicted at the same time

---
 transition_machine/include/Config.hpp |  4 ++++
 transition_machine/src/ActionBank.cpp |  1 +
 transition_machine/src/Config.cpp     | 12 +++++++++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/transition_machine/include/Config.hpp b/transition_machine/include/Config.hpp
index bf7327a..88ed7f7 100644
--- a/transition_machine/include/Config.hpp
+++ b/transition_machine/include/Config.hpp
@@ -157,6 +157,8 @@ class Config
   private :
 
   const unsigned int HISTORY_SIZE = 2000;
+  /// @brief True if eos tape have been modified by an action.
+  bool eosTouched;
   /// @brief The name of the current state of the TransitionMachine.
   std::string currentStateName;
   /// @brief For each state of the TransitionMachine, an history of the Action that have been applied to this Config.
@@ -362,6 +364,8 @@ class Config
   ///
   /// @return The head of the multi-tapes buffer.
   int getHead() const;
+  /// @brief set eosTouched to true.
+  void setEosTouched();
   /// @brief Return true if the head is at the end of the tapes.
   ///
   /// @return True if the head is at the end of the tapes.
diff --git a/transition_machine/src/ActionBank.cpp b/transition_machine/src/ActionBank.cpp
index b8affab..6977f3a 100644
--- a/transition_machine/src/ActionBank.cpp
+++ b/transition_machine/src/ActionBank.cpp
@@ -685,6 +685,7 @@ std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & na
           int b0 = c.getHead();
           int s0 = c.stackTop();
           simpleBufferWrite(c, ProgramParameters::sequenceDelimiterTape, ProgramParameters::sequenceDelimiter, s0-b0);
+          c.setEosTouched();
         };
       auto undo = [](Config & c, Action::BasicAction)
         {
diff --git a/transition_machine/src/Config.cpp b/transition_machine/src/Config.cpp
index 254da76..63f1c0c 100644
--- a/transition_machine/src/Config.cpp
+++ b/transition_machine/src/Config.cpp
@@ -16,6 +16,7 @@
 Config::Config(BD & bd, const std::string inputFilename) : bd(bd), hashHistory(HISTORY_SIZE), pastActions(HISTORY_SIZE)
 {
   this->outputFile = nullptr;
+  this->eosTouched = false;
   this->stackHistory = -1;
   this->lastIndexPrinted = -1;
   this->inputFilename = inputFilename;
@@ -39,6 +40,7 @@ Config::Config(const Config & other) : bd(other.bd), hashHistory(other.hashHisto
   this->stackHistory = other.stackHistory;
   this->head = other.head;
   this->outputFile = other.outputFile;
+  this->eosTouched = other.eosTouched;
   this->lastIndexPrinted = other.lastIndexPrinted;
   this->tapes = other.tapes;
   this->totalEntropy = other.totalEntropy;
@@ -94,8 +96,6 @@ void Config::readInput()
       tape.addToHyp("");
     }
 
-    fprintf(stderr, "rawInputHeadIndex=%d rawInputSize=%lu\n", rawInputHeadIndex, rawInput.size());
-
     return;
   }
 
@@ -395,7 +395,7 @@ void Config::printAsOutput(FILE * output, int dataIndex, int realIndex, bool for
     }
     else
     {
-      if (eos.getKnown())
+      if (eos.getKnown() && !eosTouched)
         eosStr = eos.getRef(dataIndex-head);
       else
         eosStr = eos.getHyp(dataIndex-head);
@@ -494,6 +494,7 @@ void Config::reset()
   stackHistory = -1;
 
   head = 0;
+  eosTouched = false;
   rawInputHead = 0;
   rawInputHeadIndex = 0;
   currentWordIndex = 1;
@@ -985,3 +986,8 @@ void Config::updateIdsInSequence()
     fprintf(stderr, "Done updating IDS.\n");
 }
 
+void Config::setEosTouched()
+{
+  eosTouched = true;
+}
+
-- 
GitLab