Newer
Older
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};
}
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))));
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))));
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};
}
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
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};
}
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
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)));
util::upper(res[inIndex]);
addHypothesis(col, lineIndex, fmt::format("{}", res)).apply(config, a);
};
auto undo = [col, obj, index](Config & config, Action &)
{
int lineIndex = config.getRelativeWordIndex(obj, index);
auto & value = config.getLastNotEmptyHyp(col, lineIndex);
auto res = util::splitAsUtf8(std::string(value));
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)
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
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)));
util::lower(res[inIndex]);
addHypothesis(col, lineIndex, fmt::format("{}", res)).apply(config, a);
};
auto undo = [col, obj, index](Config & config, Action &)
{
int lineIndex = config.getRelativeWordIndex(obj, index);
auto & value = config.getLastNotEmptyHyp(col, lineIndex);
auto res = util::splitAsUtf8(std::string(value));
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)
return false;
return addHypothesis(col, lineIndex, "").appliable(config, a);
};
return {Type::Write, apply, undo, appliable};
}
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();
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
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};
}