<commit_before>#include "dataparser.h"

DataParser::DataParser()
{
    pkg_main_path=get_path_to_pkg();
    algorithms_2D_path=pkg_main_path+"/data/algorithms/2D/";

    algorithms_3D_path=pkg_main_path+"/data/algorithms/3D/";
    templates_path=pkg_main_path+"/data/templates/";
    temp_path=pkg_main_path+"/data/temp.txt";

    //create the templates folder
    std::string mkdir="mkdir ";
    std::string dummy=mkdir+templates_path;
    system(dummy.c_str());

    //create the algorithms folder
    dummy=mkdir+algorithms_2D_path;
    dummy.erase(dummy.end()-3, dummy.end());
    system(dummy.c_str());

    //create the algorithms 2D folder
    dummy=mkdir+algorithms_2D_path;
    system(dummy.c_str());

    //create the algorithms 3D folder
    dummy=mkdir+algorithms_3D_path;
    system(dummy.c_str());


}


std::string DataParser::get_path_to_pkg()
{
    //search for a certain library in order to obtain the full path
    std::string sys_command="find . -print | grep 'sandbox/TFG/src/libraries/libraries/dataparser.h' > dummy.txt";
    system(sys_command.c_str());

    std::string path;
    std::ifstream myfile;
    myfile.open ("dummy.txt");
    std::getline( myfile, path);
    myfile.close();

    system ("rm dummy.txt");

    //remove the last part (/src/libraries/libraries/dataparser.h)
    path.erase(path.end()-37, path.end());

    //remove the beginning (./)
    path.erase(path.begin(), path.begin()+2);
    return path;
}

std::vector <std::string> DataParser::get_file_names (std::string path)
{
    //Store the names of the files in the string path in the file specified by temp_path
    std::string sys_command="ls "+path +"> "+temp_path;
    system(sys_command.c_str());

    //Open the temp file
    std::ifstream file(temp_path.c_str());
    std::string dummy;
    std::vector <std::string> templates;

    //Get each line and store the names in a vector
    while (file.good())
    {
        getline(file,dummy);
        templates.push_back(path+dummy);
    }
    //Close the temp file
    file.close();

    //Remove the temp file
    std::string remove_temp_files="rm "+temp_path;
    system(remove_temp_files.c_str());

    //Remove the last element of the vector which is not complete [due to getline function]
    templates.erase(templates.end());

    //Return the vector of strings with the file names
    return templates;
}


void DataParser::save_template_2D(std::vector<cv::Mat> & descriptors, int number_object)
{
    //NAME CODE:
    std::stringstream path;

    //The name of the file will be a number [the position of the object in the vector of descriptors]
    path <<templates_path<<number_object;

    //create a folder in the templates_path with the number of the position of the object in the vector
    std::stringstream command;
    command<<"mkdir "<<path;
    system(command.str().c_str());

    std::stringstream filename;
    //Store each matrix of descriptors corresponding to different views of the object in a different yml file inside the same object's folder
    for (unsigned int i=0; i<descriptors.size(); i++)
    {
        //add the path to the filename
        filename<<path;
        //add the name of the file depending on the number of view and also the extension
        filename<<"/"<<"view_"<< i<<"_d.yml";
        //create the filestorage
        cv::FileStorage fs(filename.str(), cv::FileStorage::WRITE);
        //write the descriptors to the filestorage
        fs <<"descriptors"<< descriptors[i];
        //remove the contents of the stringstream filename for the next iteration
        filename.str(std::string());
    }
}



void DataParser::save_algorithm_2D(cv::FlannBasedMatcher & alg2D, int object_number)
{

    std::stringstream filename;
    filename<<algorithms_2D_path<<object_number<<".yml";

    cv::FileStorage fs(filename.str(), cv::FileStorage::WRITE);
    alg2D.write(fs);

}


std::vector<cv::FlannBasedMatcher > DataParser::load_algorithms_2D()
{
    //Get the file names in the algorithms2D path
    std::vector <std::string> algorithms=this->get_file_names(algorithms_2D_path);

    if (algorithms.size()>0)
    {
        std::vector<cv::FlannBasedMatcher >alg2D;
        for (unsigned int i=0; i<algorithms.size(); i++)
        {
            cv::FileStorage fr(algorithms[i], cv::FileStorage::READ);
            cv::FileNode fn;
            fn.fs=*fr;
            alg2D[i].read(fn);

        }

        return alg2D;
    }
}


cv::Mat DataParser::load_descriptor (std::string filename)
{
    cv::FileStorage fr (filename, cv::FileStorage::READ);<commit_msg>updated dataparser<commit_after>#include "dataparser.h"

DataParser::DataParser()
{
    pkg_main_path=get_path_to_pkg();
    algorithms_2D_path=pkg_main_path+"/data/algorithms/2D/";

    algorithms_3D_path=pkg_main_path+"/data/algorithms/3D/";
    templates_path=pkg_main_path+"/data/templates/";
    temp_path=pkg_main_path+"/data/temp.txt";

    //create the templates folder
    std::string mkdir="mkdir ";
    std::string dummy=mkdir+templates_path;
    system(dummy.c_str());

    //create the algorithms folder
    dummy=mkdir+algorithms_2D_path;
    dummy.erase(dummy.end()-3, dummy.end());
    system(dummy.c_str());

    //create the algorithms 2D folder
    dummy=mkdir+algorithms_2D_path;
    system(dummy.c_str());

    //create the algorithms 3D folder
    dummy=mkdir+algorithms_3D_path;
    system(dummy.c_str());


}


std::string DataParser::get_path_to_pkg()
{
    //search for a certain library in order to obtain the full path
    std::string sys_command="find . -print | grep 'sandbox/TFG/src/libraries/libraries/dataparser.h' > dummy.txt";
    system(sys_command.c_str());

    std::string path;
    std::ifstream myfile;
    myfile.open ("dummy.txt");
    std::getline( myfile, path);
    myfile.close();

    system ("rm dummy.txt");

    //remove the last part (/src/libraries/libraries/dataparser.h)
    path.erase(path.end()-37, path.end());

    //remove the beginning (./)
    path.erase(path.begin(), path.begin()+2);
    return path;
}

std::vector <std::string> DataParser::get_file_names (std::string path)
{
    //Store the names of the files in the string path in the file specified by temp_path
    std::string sys_command="ls "+path +"> "+temp_path;
    system(sys_command.c_str());

    //Open the temp file
    std::ifstream file(temp_path.c_str());
    std::string dummy;
    std::vector <std::string> templates;

    //Get each line and store the names in a vector
    while (file.good())
    {
        getline(file,dummy);
        templates.push_back(path+dummy);
    }
    //Close the temp file
    file.close();

    //Remove the temp file
    std::string remove_temp_files="rm "+temp_path;
    system(remove_temp_files.c_str());

    //Remove the last element of the vector which is not complete [due to getline function]
    templates.erase(templates.end());

    //Return the vector of strings with the file names
    return templates;
}


void DataParser::save_template_2D(std::vector<cv::Mat> & descriptors, int number_object)
{
    //NAME CODE:
    std::stringstream path;

    //The name of the file will be a number [the position of the object in the vector of descriptors]
    path <<templates_path<<number_object<<"/";

    //create a folder in the templates_path with the number of the position of the object in the vector
    std::stringstream command;
    command<<"mkdir "<<path;
    system(command.str().c_str());

    std::stringstream filename;
    //Store each matrix of descriptors corresponding to different views of the object in a different yml file inside the same object's folder
    for (unsigned int i=0; i<descriptors.size(); i++)
    {
        //add the path to the filename
        filename<<path;
        //add the name of the file depending on the number of view and also the extension
        filename<<"view_"<< i<<"_d.yml";
        //create the filestorage
        cv::FileStorage fs(filename.str(), cv::FileStorage::WRITE);
        //write the descriptors to the filestorage
        fs <<"descriptors"<< descriptors[i];
        //remove the contents of the stringstream filename for the next iteration
        filename.str(std::string());
    }
}

void DataParser::save_algorithm_2D(cv::FlannBasedMatcher & alg2D, int object_number)
{
    std::stringstream filename;
    filename<<algorithms_2D_path<<object_number<<".yml";

    cv::FileStorage fs(filename.str(), cv::FileStorage::WRITE);
    alg2D.write(fs);
}


std::vector<cv::FlannBasedMatcher > DataParser::load_algorithms_2D()
{
    //Get the file names in the algorithms2D path
    std::vector <std::string> algorithms=this->get_file_names(algorithms_2D_path);

    if (algorithms.size()>0)
    {
        std::vector<cv::FlannBasedMatcher >alg2D;
        for (unsigned int i=0; i<algorithms.size(); i++)
        {
            cv::FileStorage fr(algorithms[i], cv::FileStorage::READ);
            cv::FileNode fn;
            fn.fs=*fr;
            alg2D[i].read(fn);

        }

        return alg2D;
    }
}


cv::Mat DataParser::load_descriptor (std::string filename)
{
    cv::FileStorage fr (filename, cv::FileStorage::READ);
{'fraction_non_alphanumeric': 0.13013473545842918, 'fraction_numerical': 0.005915215248110417, 'mean_word_length': 3.3852065321805958, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6640938', 'n_tokens_mistral': 2921, 'n_tokens_neox': 2825, 'n_words': 730}