Skip to content

Instantly share code, notes, and snippets.

@Shudouken
Created May 17, 2014 15:11
Show Gist options
  • Save Shudouken/85b33e4855394b272be2 to your computer and use it in GitHub Desktop.
Save Shudouken/85b33e4855394b272be2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/algorithm/string.hpp>
using namespace std;
void usage()
{
cout << "Usage: cvstoss file.csv" << endl;
cout << endl;
cout << "readings and meanings can have up to 2 entries" << endl;
cout << "leave reading empty for words like する - to be" << endl;
cout << endl;
cout << "example file.csv" << endl;
cout << " 評決,ひょうけつ,\"decision, verdict\"" << endl;
cout << " ガチ,,\"serious, diligent\"" << endl;
cout << " 悪口,\"わるくち,わるぐち\",bad mouth" << endl;
}
int main(int argc, char* argv[])
{
if(argc != 2)
usage();
else
{
ifstream csv;
csv.open(argv[1]);
if(csv.is_open())
{
string line;
vector<string> items;
int i;
bool first = true;
while(!csv.eof())
{
getline(csv, line);
boost::split(items, line, boost::is_any_of(","));
i = 0;
if(first == true)
{
cout << "[";
first = false;
}
else
cout << ",";
boost::algorithm::trim(items[i]);
cout << "{\"kanji\":\"" << items[i] << "\",";
i++;
if(items[i].find('"') != string::npos)
{
items[i].erase(items[i].begin(),items[i].begin()+1);
boost::algorithm::trim(items[i]);
cout << "\"reading\":[\"" << items[i] << "\",\"";
i++;
items[i].erase(items[i].end()-1,items[i].end());
boost::algorithm::trim(items[i]);
cout << items[i] << "\"],";
i++;
}
else
{
cout << "\"reading\":[\"" << items[i] << "\"],";
boost::algorithm::trim(items[i]);
i++;
}
if(items[i].find('"') != string::npos)
{
items[i].erase(items[i].begin(),items[i].begin()+1);
boost::algorithm::trim(items[i]);
cout << "\"meaning\":[\"" << items[i] << "\",\"";
i++;
items[i].erase(items[i].end()-1,items[i].end());
boost::algorithm::trim(items[i]);
cout << items[i] << "\"]}";
}
else
{
boost::algorithm::trim(items[i]);
cout << "\"meaning\":[\"" << items[i] << "\"]}";
}
}
cout << "]" << endl;
}
else
cout << "Cannot read csv file" << endl;
csv.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment