tsv-parse.js
Spring 2023
const fs = require("fs");
tsv_files = []
tsv_files.push(fs.readFileSync("../TSV Files/008-034.tsv"))
tsv_files.push(fs.readFileSync("../TSV Files/035-040.tsv"))
tsv_files.push(fs.readFileSync("../TSV Files/041-053.tsv"))
tsv_files.push(fs.readFileSync("../TSV Files/054-076.tsv"))
tsv_files.push(fs.readFileSync("../TSV Files/077-083.tsv"))
tsv_files.push(fs.readFileSync("../TSV Files/084-093.tsv"))
tsv_files.push(fs.readFileSync("../TSV Files/094-098.tsv"))
tsv_files.push(fs.readFileSync("../TSV Files/099-110.tsv"))
tsv_files.push(fs.readFileSync("../TSV Files/111-118.tsv"))
tsv_files.push(fs.readFileSync("../TSV Files/119-128.tsv"))
var wordlists = {}
for (tsv of tsv_files) {
let lines = tsv.toString().split("\r\n");
let headers = lines[0].split("\t")
let subheadings = lines[1].split("\t")
let wordArray = [];
for (let i = 2; i < lines.length; i++) {
wordArray.push(lines[i].split("\t"))
}
let currHeaderIndex = 0;
let nextHeaderIndex = 1;
while (currHeaderIndex < headers.length) {
let currHeader = headers[currHeaderIndex];
let nextHeader = headers[nextHeaderIndex];
while (nextHeader == "") {
nextHeader = headers[++nextHeaderIndex];
}
wordlists[`lesson${currHeader}`] = {
default: [],
}
for (let i = currHeaderIndex; i < nextHeaderIndex; i++) {
let subheading = subheadings[i]
if (subheading) {
wordlists[`lesson${currHeader}`][subheading] = [];
for (let wordRow of wordArray) {
if (wordRow[i]) {
wordlists[`lesson${currHeader}`][subheading].push(wordRow[i]);
}
}
}
else {
for (let wordRow of wordArray) {
if (wordRow[i]) {
wordlists[`lesson${currHeader}`].default.push(wordRow[i]);
}
}
}
}
if (wordlists[`lesson${currHeader}`].default.length === 0) {
delete wordlists[`lesson${currHeader}`].default
}
currHeaderIndex = nextHeaderIndex++;
}
}
{
heartWords = fs.readFileSync("../TSV Files/Heart Words.tsv")
let lines = heartWords.toString().split("\n");
for (line of lines) {
let [lessonNum, heartWord] = line.split("\t")
if (!wordlists[`lesson${lessonNum}`]) {
wordlists[`lesson${lessonNum}`] = {};
}
if (!wordlists[`lesson${lessonNum}`].heart) {
wordlists[`lesson${lessonNum}`].heart = [heartWord]
}
else {
wordlists[`lesson${lessonNum}`].heart.push(heartWord)
}
}
delete wordlists.lesson
}
console.log(wordlists)
let json = JSON.stringify(wordlists);
fs.writeFileSync('../wordlists.json', json);