Skip to content
Snippets Groups Projects
Action.cpp 38.4 KiB
Newer Older
Franck Dary's avatar
Franck Dary committed

Action Action::deprel(std::string value)
{
  auto apply = [value](Config & config, Action & a)
  {
    addHypothesis(Config::deprelColName, config.getLastAttached(), value).apply(config, a);
  };

  auto undo = [](Config & config, Action & a)
  {
    addHypothesis(Config::deprelColName, config.getLastAttached(), "").undo(config, a);
  };

  auto appliable = [](const Config & config, const Action &)
  {
    return config.has(0,config.getLastAttached(),0);
  };

  return {Type::Write, apply, undo, appliable}; 
}

Franck Dary's avatar
Franck Dary committed
Action Action::transformSuffix(std::string fromCol, Config::Object fromObj, int fromIndex, std::string toCol, Config::Object toObj, int toIndex, util::utf8string toRemove, util::utf8string toAdd)
{
  auto apply = [fromCol, fromObj, fromIndex, toCol, toObj, toIndex, toRemove, toAdd](Config & config, Action & a)
  {
    int fromLineIndex = config.getRelativeWordIndex(fromObj, fromIndex);
    int toLineIndex = config.getRelativeWordIndex(toObj, toIndex);

    if (toRemove.empty() and toAdd.empty())
    {
      addHypothesis(toCol, toLineIndex, std::string(config.getAsFeature(fromCol, fromLineIndex))).apply(config, a);
    util::utf8string res = util::splitAsUtf8(util::lower(std::string(config.getAsFeature(fromCol, fromLineIndex))));
Franck Dary's avatar
Franck Dary committed
    for (unsigned int i = 0; i < toRemove.size(); i++)
      res.pop_back();
    for (auto & letter : toAdd)
      res.push_back(letter);
    addHypothesis(toCol, toLineIndex, fmt::format("{}", res)).apply(config, a);
  };

  auto undo = [toCol, toObj, toIndex](Config & config, Action & a)
  {
    int toLineIndex = config.getRelativeWordIndex(toObj, toIndex);
    addHypothesis(toCol, toLineIndex, "").undo(config, a);
  };

  auto appliable = [fromCol, fromObj, fromIndex, toCol, toObj, toIndex, toRemove, toAdd](const Config & config, const Action & a)
  {
    if (!config.hasRelativeWordIndex(fromObj, fromIndex) or !config.hasRelativeWordIndex(toObj, toIndex))
      return false;

    int fromLineIndex = config.getRelativeWordIndex(fromObj, fromIndex);
    int toLineIndex = config.getRelativeWordIndex(toObj, toIndex);
    util::utf8string res = util::splitAsUtf8(util::lower(std::string(config.getAsFeature(fromCol, fromLineIndex))));
Franck Dary's avatar
Franck Dary committed
    if (res.size() < toRemove.size())
      return false;

    for (unsigned int i = 0; i < toRemove.size(); i++)
    {
      if (res.back() != toRemove[toRemove.size()-1-i])
        return false;
      res.pop_back();
    }

    for (auto & letter : toAdd)
      res.push_back(letter);

    return addHypothesis(toCol, toLineIndex, fmt::format("{}", res)).appliable(config, a);
  };

  return {Type::Write, apply, undo, appliable}; 
}

Franck Dary's avatar
Franck Dary committed
Action Action::copyContent(std::string fromCol, Config::Object fromObj, int fromIndex, std::string toCol, Config::Object toObj, int toIndex)
{
  auto apply = [fromCol, fromObj, fromIndex, toCol, toObj, toIndex](Config & config, Action & a)
  {
    auto empty = util::utf8string();
    transformSuffix(fromCol, fromObj, fromIndex, toCol, toObj, toIndex, empty, empty).apply(config, a);
  };

  auto undo = [toCol, toObj, toIndex, fromCol, fromObj, fromIndex](Config & config, Action & a)
  {
    auto empty = util::utf8string();
    transformSuffix(fromCol, fromObj, fromIndex, toCol, toObj, toIndex, empty, empty).undo(config, a);
  };

  auto appliable = [](const Config &, const Action &)
  {
    return true;
  };

  return {Type::Write, apply, undo, appliable}; 
}

Franck Dary's avatar
Franck Dary committed
Action Action::uppercase(std::string col, Config::Object obj, int index)
{
  auto apply = [col, obj, index](Config & config, Action & a)
  {
    int lineIndex = config.getRelativeWordIndex(obj, index);
    auto res = util::upper(config.getAsFeature(col, lineIndex));

    addHypothesis(col, lineIndex, res).apply(config, a);
  };

  auto undo = [col, obj, index](Config & config, Action & a)
  {
    int lineIndex = config.getRelativeWordIndex(obj, index);
    addHypothesis(col, lineIndex, "").undo(config, a);
  };

  auto appliable = [col, obj, index](const Config & config, const Action & a)
  {
    if (!config.hasRelativeWordIndex(obj, index))
      return false;

    int lineIndex = config.getRelativeWordIndex(obj, index);

    return addHypothesis(col, lineIndex, "").appliable(config, a);
  };

  return {Type::Write, apply, undo, appliable}; 
}

Action Action::uppercaseIndex(std::string col, Config::Object obj, int index, int inIndex)
{
  auto apply = [col, obj, index, inIndex](Config & config, Action & a)
  {
    int lineIndex = config.getRelativeWordIndex(obj, index);
    auto res = util::splitAsUtf8(std::string(config.getAsFeature(col, lineIndex)));
Franck Dary's avatar
Franck Dary committed
    util::upper(res[inIndex]);

    addHypothesis(col, lineIndex, fmt::format("{}", res)).apply(config, a);
  };

  auto undo = [col, obj, index](Config & config, Action &)
Franck Dary's avatar
Franck Dary committed
  {
    int lineIndex = config.getRelativeWordIndex(obj, index);
    auto & value = config.getLastNotEmptyHyp(col, lineIndex);
    auto res = util::splitAsUtf8(std::string(value));
Franck Dary's avatar
Franck Dary committed
    value = fmt::format("{}", res);
  };

  auto appliable = [col, obj, index, inIndex](const Config & config, const Action & a)
  {
    if (!config.hasRelativeWordIndex(obj, index))
      return false;

    int lineIndex = config.getRelativeWordIndex(obj, index);

    if ((int)util::splitAsUtf8(std::string(config.getAsFeature(col, lineIndex))).size() <= inIndex)
Franck Dary's avatar
Franck Dary committed
      return false;

    return addHypothesis(col, lineIndex, "").appliable(config, a);
  };

  return {Type::Write, apply, undo, appliable}; 
}

Action Action::lowercase(std::string col, Config::Object obj, int index)
{
  auto apply = [col, obj, index](Config & config, Action & a)
  {
    int lineIndex = config.getRelativeWordIndex(obj, index);
    auto res = util::lower(config.getAsFeature(col, lineIndex));

    addHypothesis(col, lineIndex, res).apply(config, a);
  };

  auto undo = [col, obj, index](Config & config, Action & a)
  {
    int lineIndex = config.getRelativeWordIndex(obj, index);
    addHypothesis(col, lineIndex, "").undo(config, a);
  };

  auto appliable = [col, obj, index](const Config & config, const Action & a)
  {
    if (!config.hasRelativeWordIndex(obj, index))
      return false;

    int lineIndex = config.getRelativeWordIndex(obj, index);

    return addHypothesis(col, lineIndex, "").appliable(config, a);
  };

  return {Type::Write, apply, undo, appliable}; 
}

Action Action::lowercaseIndex(std::string col, Config::Object obj, int index, int inIndex)
{
  auto apply = [col, obj, index, inIndex](Config & config, Action & a)
  {
    int lineIndex = config.getRelativeWordIndex(obj, index);
    auto res = util::splitAsUtf8(std::string(config.getAsFeature(col, lineIndex)));
Franck Dary's avatar
Franck Dary committed
    util::lower(res[inIndex]);

    addHypothesis(col, lineIndex, fmt::format("{}", res)).apply(config, a);
  };

  auto undo = [col, obj, index](Config & config, Action &)
Franck Dary's avatar
Franck Dary committed
  {
    int lineIndex = config.getRelativeWordIndex(obj, index);
    auto & value = config.getLastNotEmptyHyp(col, lineIndex);
    auto res = util::splitAsUtf8(std::string(value));
Franck Dary's avatar
Franck Dary committed
    value = fmt::format("{}", res);
  };

  auto appliable = [col, obj, index, inIndex](const Config & config, const Action & a)
  {
    if (!config.hasRelativeWordIndex(obj, index))
      return false;

    int lineIndex = config.getRelativeWordIndex(obj, index);

    if ((int)util::splitAsUtf8(std::string(config.getAsFeature(col, lineIndex))).size() <= inIndex)
Franck Dary's avatar
Franck Dary committed
      return false;

    return addHypothesis(col, lineIndex, "").appliable(config, a);
  };

  return {Type::Write, apply, undo, appliable}; 
}
Franck Dary's avatar
Franck Dary committed

Action Action::writeScore(const std::string & colName, Config::Object object, int relativeIndex)
{
  auto apply = [colName, object, relativeIndex](Config & config, Action & a)
  {
    int lineIndex = config.getRelativeWordIndex(object, relativeIndex);

    float score = config.getChosenActionScore();
Franck Dary's avatar
Franck Dary committed
    if (-score != std::numeric_limits<float>::max())
Franck Dary's avatar
Franck Dary committed
      return addHypothesis(colName, lineIndex, fmt::format("{}", score)).apply(config, a);
    else
      return addHypothesis(colName, lineIndex, config.getConst(colName, lineIndex, 0)).apply(config, a);
  };

  auto undo = [colName, object, relativeIndex](Config & config, Action & a)
  {
    int lineIndex = config.getRelativeWordIndex(object, relativeIndex);

    return addHypothesis(colName, lineIndex, "").undo(config, a);
  };

  auto appliable = [colName, object, relativeIndex](const Config & config, const Action & a)
  {
    if (!config.hasRelativeWordIndex(object, relativeIndex))
      return false;

    int lineIndex = config.getRelativeWordIndex(object, relativeIndex);

    return addHypothesis(colName, lineIndex, "").appliable(config, a);
  };

  return {Type::Write, apply, undo, appliable}; 

}