Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dataset_utils/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dataset Utils

This directory contains utility functions related to Datasets.

Current Implemented features

* Convert CSV files to JSON([Issue](https://github.com/mlpack/models/issues/22))
* Convert CSV files to XML([Issue](https://github.com/mlpack/models/issues/22))
102 changes: 102 additions & 0 deletions dataset_utils/convert.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <iostream>
#include <unordered_map>
#include <boost/range/combine.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/tokenizer.hpp>
#include <string>
#include <fstream>
#include <unordered_map>


using namespace boost::property_tree;
using namespace boost;

class Convert
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, can we also add class description and usage here.

{
void csvxmlHelper(std::string path, std::string to)
{
//static int ctr;
static std::unordered_map<std::string, int> fileNames;
std::vector<std::string> tags;
std::vector<std::string> rows;
std::ifstream file(path);
std::string line;

auto tokenize = [&](std::string line)
{
std::vector<std::string> col_names;

tokenizer<escaped_list_separator<char> > tk(line, escaped_list_separator<char>());
for (tokenizer<escaped_list_separator<char> >::iterator i(tk.begin()); i != tk.end(); ++i)
col_names.push_back(*i);

return col_names;
};

auto create_XML = [&](std::vector<std::string>& tags, std::vector<std::string> rows)
{
static int ctr;
ptree XMLobjectL;
std::string tag, value;

for (auto i : boost::combine(tags, rows))
{
//tag contains tags, value contains corresponding values
boost::tie(tag, value) = i;
XMLobjectL.put("annotation.object." + tag, value);
}

write_xml(std::to_string(ctr) + ".xml", XMLobjectL, std::locale(),
xml_writer_make_settings<ptree::key_type>(' ', 1u));

ctr++;
};

auto create_JSON = [&](std::vector<std::string>& tags, std::vector<std::string> rows)
{
static int ctr;
ptree XMLobjectL;
std::string tag, value;

for (auto i : boost::combine(tags, rows))
{
//tag contains tags, value contains corresponding values
boost::tie(tag, value) = i;
XMLobjectL.put("annotation.object." + tag, value);
}

write_json(std::to_string(ctr) + ".json", XMLobjectL);
ctr++;
};

std::getline(file, line);
tags = tokenize(line);

if (to == "xml")
while (std::getline(file, line))
create_XML(tags, tokenize(line));

else if (to == "json")
while (std::getline(file, line))
create_JSON(tags, tokenize(line));

}

public:
void convert(std::string path, std::string to)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this function static. No need to declare an object for converting datasets. Also Capitalize the first letter of the function.

{
csvxmlHelper(path, to);
}
};

// How To use
/*int main() {
Convert foo;

foo.convert("path_to_csv_file.csv", "xml");
foo.convert("path_to_csv_file.csv", "json");

}*/