Skip to content

Commit ec8fb30

Browse files
committedSep 28, 2019
change: input files to json format
1 parent f9c2a59 commit ec8fb30

File tree

8 files changed

+18
-122009
lines changed

8 files changed

+18
-122009
lines changed
 

‎README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
This is an open database anyone can use to find Sinhala definitions for English words and vice versa. This project has a build script you can use to generate databases in different formats (see "Build" section for more information).
66

77
## Files
8-
- Main database files are in ```comma-separated values (.csv)``` format and they are located inside the ```inputs``` directory.
9-
- You can open them using a spreadsheet program such as LibreOffice Calc.
10-
- ```en2sn``` database contains English-Sinhala definitions.
11-
- ```sn2en``` database contains Sinhala-English definitions.
8+
- Main input files are in ```JavaScript Object Notation (JSON)``` format and they are located inside the ```inputs``` directory.
9+
- You can open them using a text editor such as VSCode.
10+
- ```en2sn.json``` database contains English-Sinhala definitions.
11+
- ```sn2en.json``` database contains Sinhala-English definitions.
1212
- These are the main files used by build script to generate databases.
1313
- Do not rename them if you are planning on using the build script.
1414

@@ -17,7 +17,7 @@ You can use the build script in this project to automatically generate databases
1717

1818
### Currently supported formats,
1919
- SQLite
20-
- JSON
20+
- JSON (Structure is different than input files)
2121

2222
### Building Instructions
2323
1. Install Nodejs.

‎build.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
const csv = require("csvtojson");
21
const argio = require('argio');
32
const parser = argio();
43

5-
const init = async () => {
6-
const data = await readCSV().catch(e => console.log(e));
4+
const init = () => {
5+
const data = readInputs();
76
if (parser.get("f")) {
87
let formats = parser.params.f;
98
build(data, formats);
109
} else {
1110
console.log("Usage: node build.js -f [formats]\n");
1211
console.log("Options:\n\t-f\t\tDefine output formats")
1312
}
14-
1513
}
1614

1715
const build = (data, formats) => {
@@ -25,13 +23,13 @@ const build = (data, formats) => {
2523
})
2624
}
2725

28-
const readCSV = async () => {
26+
const readInputs = () => {
2927
try {
30-
const en2sn = await csv().fromFile("./inputs/en2sn.csv");
31-
const sn2en = await csv().fromFile("./inputs/sn2en.csv");
28+
const en2sn = require("./inputs/en2sn.json");
29+
const sn2en = require("./inputs/sn2en.json");
3230
return { en2sn, sn2en };
3331
} catch (e) {
34-
throw (`Unable to read csv files. : ${e}`);
32+
console.log(`Unable to read input files. : ${e}`);
3533
}
3634
}
3735

‎formats/json.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const build = (data) => {
55
const jsonPath = `${__dirname}/../outputs/json`;
66
const en2sn = {};
77
const sn2en = {};
8-
data.en2sn.forEach(row => { en2sn[row.word] = row.definitions.split(",") });
9-
data.sn2en.forEach(row => { sn2en[row.word] = row.definitions.split(",") });
8+
data.en2sn.forEach(row => { en2sn[row.word] = row.definitions });
9+
data.sn2en.forEach(row => { sn2en[row.word] = row.definitions });
1010
fs.writeFileSync(`${jsonPath}/en2sn.json`, JSON.stringify(en2sn));
1111
fs.writeFileSync(`${jsonPath}/sn2en.json`, JSON.stringify(sn2en));
1212
console.log(`Success: JSON files have been generated!.`);

‎formats/sqlite.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ const save = async (table, data) => {
2222
let rowCount = data.length;
2323
let values = "";
2424
let count = 1;
25-
let limit = 3000;
25+
let limit = 1000;
2626

2727
try {
2828
for (let row of data) {
29-
values += `('${row.word}', '${row.definitions}')`;
29+
values += `("${row.word}", "${row.definitions}")`;
3030
if (count < limit) values += ",";
3131
if (count == limit) {
3232
let sql = `INSERT INTO ${table}(word, definitions) VALUES${values}`;
@@ -55,6 +55,7 @@ const insert = (sql) => {
5555
return new Promise((resolve, reject) => {
5656
osdb.run(sql, [], (err) => {
5757
if (err) {
58+
console.log(sql);
5859
console.log(`Error: Unable to insert data to the database! ${err}`);
5960
reject();
6061
}

‎inputs/en2sn.csv

-49,024
This file was deleted.

‎inputs/en2sn.json

+1
Large diffs are not rendered by default.

‎inputs/sn2en.csv

-72,968
This file was deleted.

‎inputs/sn2en.json

+1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.