Skip to content
Snippets Groups Projects
Commit 4484489e authored by Franck Dary's avatar Franck Dary
Browse files

Fixed multiple issues regarding EOS

parent 6a04a795
No related branches found
No related tags found
No related merge requests found
......@@ -54,6 +54,7 @@ class Action
static Action popStack();
static Action emptyStack();
static Action setRoot();
static Action updateIds();
static Action attach(Object governorObject, int governorIndex, Object dependentObject, int dependentIndex);
};
......
......@@ -8,7 +8,7 @@ class SubConfig : public Config
{
private :
static constexpr std::size_t spanSize = 200;
static constexpr std::size_t spanSize = 800;
private :
......
......@@ -240,7 +240,7 @@ Action Action::setRoot()
if (config.getLastNotEmptyHypConst(Config::EOSColName, i) == Config::EOSSymbol1)
break;
if (util::isEmpty(config.getLastNotEmptyHyp(Config::headColName, i)))
if (util::isEmpty(config.getLastNotEmptyHypConst(Config::headColName, i)))
{
if (i == rootIndex)
{
......@@ -273,6 +273,57 @@ Action Action::setRoot()
return {Type::Write, apply, undo, appliable};
}
Action Action::updateIds()
{
auto apply = [](Config & config, Action & a)
{
int firstIndexOfSentence = -1;
for (int i = config.getWordIndex()-1; true; --i)
{
if (!config.has(0, i, 0))
{
if (i < 0)
break;
util::myThrow("The current sentence is too long to be completly held by the data strucure. Consider increasing SubConfig::SpanSize");
}
if (!config.isToken(i))
continue;
if (config.getLastNotEmptyHypConst(Config::EOSColName, i) == Config::EOSSymbol1)
break;
firstIndexOfSentence = i;
}
if (firstIndexOfSentence < 0)
util::myThrow("could not find any token in current sentence");
for (unsigned int i = firstIndexOfSentence, currentId = 1; i < config.getWordIndex(); ++i)
{
if (!config.isToken(i))
continue;
if (config.getLastNotEmptyHypConst(Config::EOSColName, i) == Config::EOSSymbol1)
break;
config.getFirstEmpty(Config::idColName, i) = fmt::format("{}", currentId);
++currentId;
}
};
auto undo = [](Config & config, Action & a)
{
// TODO : undo this
};
auto appliable = [](const Config &, const Action &)
{
return true;
};
return {Type::Write, apply, undo, appliable};
}
Action Action::attach(Object governorObject, int governorIndex, Object dependentObject, int dependentIndex)
{
auto apply = [governorObject, governorIndex, dependentObject, dependentIndex](Config & config, Action & a)
......@@ -322,4 +373,3 @@ Action::Object Action::str2object(const std::string & s)
util::myThrow(fmt::format("Invalid object '{}'", s));
return Object::Buffer;
}
......@@ -62,11 +62,31 @@ std::size_t Config::getNbLines() const
void Config::print(FILE * dest) const
{
std::vector<std::string> currentSequence;
std::vector<std::string> currentSequenceComments;
auto flushCurrentSequence = [&dest, &currentSequence, &currentSequenceComments]()
{
if (currentSequence.empty() && currentSequenceComments.empty())
return;
for (auto & comment : currentSequenceComments)
fmt::print(dest, "{}", comment);
for (auto & line : currentSequence)
fmt::print(dest, "{}", line);
fmt::print(dest, "\n");
currentSequence.clear();
currentSequenceComments.clear();
};
for (unsigned int line = 0; line < getNbLines(); line++)
{
if (isComment(getFirstLineIndex()+line))
{
fmt::print(dest, "{}\n", getConst(0, getFirstLineIndex()+line, 0));
currentSequenceComments.emplace_back(fmt::format("{}\n", getConst(0, getFirstLineIndex()+line, 0)));
continue;
}
for (unsigned int i = 0; i < getNbColumns()-1; i++)
......@@ -76,11 +96,14 @@ void Config::print(FILE * dest) const
if (valueToPrint.empty())
valueToPrint = "_";
fmt::print(dest, "{}{}", valueToPrint, i < getNbColumns()-2 ? "\t" : "\n");
currentSequence.emplace_back(fmt::format("{}{}", valueToPrint, i < getNbColumns()-2 ? "\t" : "\n"));
}
if (getLastNotEmptyConst(EOSColName, getFirstLineIndex()+line) == EOSSymbol1)
fmt::print(dest, "\n");
auto & eosColContent = isPredicted(EOSColName) ? getLastNotEmptyHypConst(EOSColName, getFirstLineIndex()+line) : getLastNotEmptyConst(EOSColName, getFirstLineIndex()+line);
if (eosColContent == EOSSymbol1)
flushCurrentSequence();
}
flushCurrentSequence();
}
void Config::printForDebug(FILE * dest) const
......
......@@ -254,6 +254,7 @@ void Transition::initReduce()
void Transition::initEOS()
{
sequence.emplace_back(Action::setRoot());
sequence.emplace_back(Action::updateIds());
sequence.emplace_back(Action::addHypothesisRelative(Config::EOSColName, Action::Object::Stack, 0, Config::EOSSymbol1));
sequence.emplace_back(Action::emptyStack());
......@@ -268,10 +269,25 @@ void Transition::initEOS()
int cost = 0;
if (config.getConst(Config::EOSColName, config.getStack(0), 0) != Config::EOSSymbol1)
++cost;
cost += 100;
if (util::isEmpty(config.getLastNotEmptyHypConst(Config::headColName, config.getStack(0))))
++cost;
auto topStackIndex = config.getStack(0);
auto topStackGov = config.getConst(Config::headColName, topStackIndex, 0);
auto topStackGovPred = config.getLastNotEmptyHypConst(Config::headColName, topStackIndex);
--cost;
for (int i = 0; config.hasStack(i); ++i)
{
if (!config.has(0, config.getStack(i), 0))
continue;
auto otherStackIndex = config.getStack(i);
auto stackId = config.getConst(Config::idColName, otherStackIndex, 0);
auto stackGovPred = config.getLastNotEmptyHypConst(Config::headColName, otherStackIndex);
if (util::isEmpty(stackGovPred))
++cost;
}
return cost;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment