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

Deleted error correction as a different executable

parent 4d4c6ba3
Branches
No related tags found
No related merge requests found
FILE(GLOB SOURCES src/*.cpp)
add_executable(macaon_error_correction src/macaon_error_correction.cpp)
target_link_libraries(macaon_error_correction errors)
target_link_libraries(macaon_error_correction transition_machine)
target_link_libraries(macaon_error_correction ${Boost_PROGRAM_OPTIONS_LIBRARY})
install(TARGETS macaon_error_correction DESTINATION bin)
add_executable(macaon_train_error_detector src/macaon_train_error_detector.cpp)
target_link_libraries(macaon_train_error_detector transition_machine)
target_link_libraries(macaon_train_error_detector ${Boost_PROGRAM_OPTIONS_LIBRARY})
install(TARGETS macaon_train_error_detector DESTINATION bin)
add_executable(macaon_decode_error_detector src/macaon_decode_error_detector.cpp)
target_link_libraries(macaon_decode_error_detector transition_machine)
target_link_libraries(macaon_decode_error_detector ${Boost_PROGRAM_OPTIONS_LIBRARY})
install(TARGETS macaon_decode_error_detector DESTINATION bin)
#compiling library
add_library(errors STATIC ${SOURCES})
/// @file macaon_decode_error_detector.cpp
/// @author Franck Dary
/// @version 1.0
/// @date 2018-12-03
#include <cstdio>
#include <cstdlib>
#include <boost/program_options.hpp>
#include "BD.hpp"
#include "Config.hpp"
#include "TransitionMachine.hpp"
namespace po = boost::program_options;
/// @brief Get the list of mandatory and optional program arguments.
///
/// @return The lists.
po::options_description getOptionsDescription()
{
po::options_description desc("Command-Line Arguments ");
po::options_description req("Required");
req.add_options()
("expName", po::value<std::string>()->required(),
"Name of this experiment")
("tm", po::value<std::string>()->required(),
"File describing the Tape Machine to use")
("bd", po::value<std::string>()->required(),
"BD file that describes the multi-tapes buffer")
("mcd", po::value<std::string>()->required(),
"MCD file that describes the input")
("input,I", po::value<std::string>()->required(),
"Input file formated according to the mcd");
po::options_description opt("Optional");
opt.add_options()
("help,h", "Produce this help message")
("debug,d", "Print infos on stderr")
("printEntropy", "Print entropy for each sequence")
("sequenceDelimiterTape", po::value<std::string>()->default_value("EOS"),
"The name of the buffer's tape that contains the delimiter token for a sequence")
("sequenceDelimiter", po::value<std::string>()->default_value("1"),
"The value of the token that act as a delimiter for sequences")
("showFeatureRepresentation", po::value<int>()->default_value(0),
"For each state of the Config, show its feature representation")
("lang", po::value<std::string>()->default_value("fr"),
"Language you are working with");
desc.add(req).add(opt);
return desc;
}
/// @brief Store the program arguments inside a variables_map
///
/// @param od The description of all the possible options.
/// @param argc The number of arguments given to this program.
/// @param argv The values of arguments given to this program.
///
/// @return The variables map
po::variables_map checkOptions(po::options_description & od, int argc, char ** argv)
{
po::variables_map vm;
try {po::store(po::parse_command_line(argc, argv, od), vm);}
catch(std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
od.print(std::cerr);
exit(1);
}
if (vm.count("help"))
{
std::cout << od << "\n";
exit(0);
}
try {po::notify(vm);}
catch(std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
od.print(std::cerr);
exit(1);
}
return vm;
}
/// @brief Uses a pre-trained TransitionMachine to predict and add information to a structured input file.
///
/// @param argc The number of arguments given to this program.
/// @param argv[] Array of arguments given to this program.
///
/// @return 0 if there was no crash.
int main(int argc, char * argv[])
{
auto od = getOptionsDescription();
po::variables_map vm = checkOptions(od, argc, argv);
ProgramParameters::expName = vm["expName"].as<std::string>();
ProgramParameters::tmName = vm["tm"].as<std::string>();
ProgramParameters::bdName = vm["bd"].as<std::string>();
ProgramParameters::input = vm["input"].as<std::string>();
ProgramParameters::mcdName = vm["mcd"].as<std::string>();
ProgramParameters::debug = vm.count("debug") == 0 ? false : true;
ProgramParameters::printEntropy = vm.count("printEntropy") == 0 ? false : true;
ProgramParameters::lang = vm["lang"].as<std::string>();
ProgramParameters::sequenceDelimiterTape = vm["sequenceDelimiterTape"].as<std::string>();
ProgramParameters::sequenceDelimiter = vm["sequenceDelimiter"].as<std::string>();
ProgramParameters::showFeatureRepresentation = vm["showFeatureRepresentation"].as<int>();
const char * MACAON_DIR = std::getenv("MACAON_DIR");
std::string slash = "/";
ProgramParameters::expPath = MACAON_DIR + slash + ProgramParameters::lang + slash + "bin/" + ProgramParameters::expName + slash;
ProgramParameters::tmFilename = ProgramParameters::expPath + ProgramParameters::tmName;
ProgramParameters::bdFilename = ProgramParameters::expPath + ProgramParameters::bdName;
ProgramParameters::mcdFilename = ProgramParameters::mcdName;
TransitionMachine tm(false);
BD bd(ProgramParameters::bdFilename, ProgramParameters::mcdFilename);
Config config(bd);
File input(ProgramParameters::input, "r");
FILE * inputPtr = input.getDescriptor();
int isError, errorIndex;
while (fscanf(inputPtr, "%d\t%d\n", &isError, &errorIndex) == 2)
{
config.loadFromFile(input);
TransitionMachine::State * currentState = tm.getCurrentState();
Classifier * classifier = currentState->classifier;
config.setCurrentStateName(&currentState->name);
Dict::currentClassifierName = classifier->name;
classifier->initClassifier(config);
auto weightedActions = classifier->weightActions(config);
std::string pAction = "";
for (auto & it : weightedActions)
if (it.first)
if (pAction == "")
{
pAction = it.second.second;
break;
}
Action * action = classifier->getAction(pAction);
action->apply(config);
}
return 0;
}
/// @file macaon_error_correction.cpp
/// @author Franck Dary
/// @version 1.0
/// @date 2018-11-27
#include <cstdio>
#include <cstdlib>
#include <boost/program_options.hpp>
#include "BD.hpp"
#include "Config.hpp"
#include "TransitionMachine.hpp"
#include "util.hpp"
#include "Error.hpp"
#include "ActionBank.hpp"
namespace po = boost::program_options;
/// @brief Get the list of mandatory and optional program arguments.
///
/// @return The lists.
po::options_description getOptionsDescription()
{
po::options_description desc("Command-Line Arguments ");
po::options_description req("Required");
req.add_options()
("expName", po::value<std::string>()->required(),
"Name of this experiment")
("classifier", po::value<std::string>()->required(),
"Name of the monitored classifier")
("tm", po::value<std::string>()->required(),
"File describing the Tape Machine to use")
("bd", po::value<std::string>()->required(),
"BD file that describes the multi-tapes buffer")
("mcd", po::value<std::string>()->required(),
"MCD file that describes the input")
("input,I", po::value<std::string>()->required(),
"Input file formated according to the mcd");
po::options_description opt("Optional");
opt.add_options()
("help,h", "Produce this help message")
("debug,d", "Print infos on stderr")
("sequenceDelimiterTape", po::value<std::string>()->default_value("EOS"),
"The name of the buffer's tape that contains the delimiter token for a sequence")
("sequenceDelimiter", po::value<std::string>()->default_value("1"),
"The value of the token that act as a delimiter for sequences")
("lang", po::value<std::string>()->default_value("fr"),
"Language you are working with");
desc.add(req).add(opt);
return desc;
}
/// @brief Store the program arguments inside a variables_map
///
/// @param od The description of all the possible options.
/// @param argc The number of arguments given to this program.
/// @param argv The values of arguments given to this program.
///
/// @return The variables map
po::variables_map checkOptions(po::options_description & od, int argc, char ** argv)
{
po::variables_map vm;
try {po::store(po::parse_command_line(argc, argv, od), vm);}
catch(std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
od.print(std::cerr);
exit(1);
}
if (vm.count("help"))
{
std::cout << od << "\n";
exit(0);
}
try {po::notify(vm);}
catch(std::exception& e)
{
std::cerr << "Error: " << e.what() << "\n";
od.print(std::cerr);
exit(1);
}
return vm;
}
/// @brief Uses a pre-trained TransitionMachine to output a pair of Config - labels. That can be used as a corpus for error detection.
///
/// @param argc The number of arguments given to this program.
/// @param argv[] Array of arguments given to this program.
///
/// @return 0 if there was no crash.
int main(int argc, char * argv[])
{
auto od = getOptionsDescription();
po::variables_map vm = checkOptions(od, argc, argv);
ProgramParameters::expName = vm["expName"].as<std::string>();
ProgramParameters::tmName = vm["tm"].as<std::string>();
ProgramParameters::bdName = vm["bd"].as<std::string>();
ProgramParameters::input = vm["input"].as<std::string>();
ProgramParameters::mcdName = vm["mcd"].as<std::string>();
ProgramParameters::debug = vm.count("debug") == 0 ? false : true;
ProgramParameters::lang = vm["lang"].as<std::string>();
ProgramParameters::sequenceDelimiterTape = vm["sequenceDelimiterTape"].as<std::string>();
ProgramParameters::sequenceDelimiter = vm["sequenceDelimiter"].as<std::string>();
ProgramParameters::classifierName = vm["classifier"].as<std::string>();
const char * MACAON_DIR = std::getenv("MACAON_DIR");
std::string slash = "/";
ProgramParameters::expPath = MACAON_DIR + slash + ProgramParameters::lang + slash + "bin/" + ProgramParameters::expName + slash;
ProgramParameters::tmFilename = ProgramParameters::expPath + ProgramParameters::tmName;
ProgramParameters::bdFilename = ProgramParameters::expPath + ProgramParameters::bdName;
ProgramParameters::mcdFilename = ProgramParameters::mcdName;
TransitionMachine tm(false);
BD bd(ProgramParameters::bdFilename, ProgramParameters::mcdFilename);
Config config(bd);
config.readInput(ProgramParameters::input);
float entropyAccumulator = 0.0;
int nbActionsInSequence = 0;
bool justFlipped = false;
bool configIsError = false;
int actionIndex = 0;
int errorIndex = 0;
Errors errors;
errors.newSequence();
while (!config.isFinal())
{
TransitionMachine::State * currentState = tm.getCurrentState();
Classifier * classifier = currentState->classifier;
config.setCurrentStateName(&currentState->name);
Dict::currentClassifierName = classifier->name;
if (ProgramParameters::debug)
{
config.printForDebug(stderr);
fprintf(stderr, "State : \'%s\'\n", currentState->name.c_str());
}
auto weightedActions = classifier->weightActions(config);
if (ProgramParameters::debug)
{
Classifier::printWeightedActions(stderr, weightedActions);
fprintf(stderr, "\n");
}
std::string & predictedAction = weightedActions[0].second.second;
Action * action = classifier->getAction(predictedAction);
for(unsigned int i = 0; i < weightedActions.size(); i++)
{
predictedAction = weightedActions[i].second.second;
action = classifier->getAction(predictedAction);
if(weightedActions[i].first)
break;
}
if(!action->appliable(config))
{
// First case the analysis is finished but without an empty stack
if (config.head == (int)config.tapes[0].ref.size()-1)
{
while (!config.stackEmpty())
config.stackPop();
continue;
}
else
{
fprintf(stderr, "ERROR (%s) : action \'%s\' is not appliable. Aborting\n", ERRINFO, predictedAction.c_str());
exit(1);
}
}
if (classifier->name == ProgramParameters::classifierName)
{
//fprintf(stderr, "%d\t%d\n", configIsError ? 1 : 0, errorIndex - actionIndex);
//config.printAsExample(stderr);
actionIndex++;
auto zeroCostActions = classifier->getZeroCostActions(config);
bool pActionIsZeroCost = false;
for (auto & s : zeroCostActions)
if (s == action->name)
{
pActionIsZeroCost = true;
break;
}
int windowSize = 5;
if (!pActionIsZeroCost)
{
if (!configIsError || (actionIndex - errorIndex > windowSize))
{
configIsError = true;
errorIndex = actionIndex-1;
}
}
else if (configIsError && (actionIndex - errorIndex > windowSize))
{
configIsError = false;
errorIndex = 0;
}
if (configIsError)
{
errors.add({action->name, zeroCostActions[0], weightedActions, classifier->getActionCost(config, action->name), ActionBank::getLinkLength(config, action->name), ActionBank::getLinkLength(config, zeroCostActions[0])});
}
}
action->apply(config);
TransitionMachine::Transition * transition = tm.getTransition(predictedAction);
tm.takeTransition(transition);
config.moveHead(transition->headMvt);
if (true)
{
nbActionsInSequence++;
float entropy = Classifier::computeEntropy(weightedActions);
config.addToEntropyHistory(entropy);
entropyAccumulator += entropy;
if (config.head >= 1 && config.getTape(ProgramParameters::sequenceDelimiterTape)[config.head-1] != ProgramParameters::sequenceDelimiter)
justFlipped = false;
if ((config.head >= 1 && config.getTape(ProgramParameters::sequenceDelimiterTape)[config.head-1] == ProgramParameters::sequenceDelimiter && !justFlipped))
{
justFlipped = true;
entropyAccumulator = 0.0;
errors.newSequence();
configIsError = false;
errorIndex = 0;
}
}
}
errors.printStats();
return 0;
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment