-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Convert Function as discussed on Issue#22 #33
Open
heisenbuug
wants to merge
12
commits into
mlpack:master
Choose a base branch
from
heisenbuug:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fcc9010
Create convert.cpp
heisenbuug 41a1cbd
Update convert.cpp
heisenbuug 415c58e
Create Readme.md
heisenbuug 291eb42
Update Readme.md
heisenbuug 7a82b37
Update Readme.md
heisenbuug a84b331
Update convert.cpp
heisenbuug f5e7513
Update convert.cpp
heisenbuug a2dbf02
Delete convert.cpp
heisenbuug 196521f
Add files via upload
heisenbuug 93ddab1
Update convert.cpp
heisenbuug 164ded5
Update convert.cpp
heisenbuug 31f4899
Update dataset_utils/convert.cpp
heisenbuug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
void csvxmlHelper(std::string path, std::string to) | ||
{ | ||
heisenbuug marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//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++; | ||
}; | ||
heisenbuug marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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)); | ||
heisenbuug marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} | ||
|
||
public: | ||
void convert(std::string path, std::string to) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
||
}*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.