|
|
|
const fs = require('fs'); |
|
|
|
const filePath = 'bluemoon_roleplay_300k_vicuna.json'; |
|
const csvPath = 'data/rawData.csv'; |
|
|
|
cleanLineEndings(); |
|
validate(); |
|
|
|
function cleanLineEndings() |
|
{ |
|
fs.readFile(filePath, 'utf8', (err, data) => |
|
{ |
|
const json = JSON.parse(data); |
|
for (const entry of json) |
|
{ |
|
for (const conversation of entry["conversations"]) |
|
{ |
|
conversation["value"] = conversation["value"].replace("\"\r", ""); |
|
} |
|
} |
|
|
|
const output = JSON.stringify(json, null, 2); |
|
fs.writeFile('./cleanedup.json', output, 'utf8', (err) => |
|
{ |
|
}); |
|
}); |
|
} |
|
|
|
function convertToJson() |
|
{ |
|
fs.readFile(csvPath, "utf-8", (err, data) => |
|
{ |
|
if (err) |
|
{ |
|
console.error(err); |
|
return; |
|
} |
|
|
|
const json = csvToJson(data); |
|
fs.writeFile("data/rawData.json", json, (err) => |
|
{ |
|
if (err) |
|
{ |
|
console.error(err); |
|
return; |
|
} |
|
console.log("File saved successfully"); |
|
}); |
|
|
|
|
|
}); |
|
} |
|
|
|
|
|
function validate() |
|
{ |
|
fs.readFile(filePath, 'utf8', (err, data) => |
|
{ |
|
if (err) |
|
{ |
|
console.error(err); |
|
return; |
|
} |
|
const json = JSON.parse(data); |
|
|
|
|
|
const result = {}; |
|
|
|
|
|
for (const entry of json) |
|
{ |
|
|
|
const url = entry.thread_href; |
|
const message_username = entry.message_username; |
|
|
|
|
|
if (!result[url]) |
|
{ |
|
result[url] = new Set(); |
|
} |
|
|
|
|
|
result[url].add(message_username); |
|
} |
|
|
|
|
|
let foundThreads = 0; |
|
for (const url in result) |
|
{ |
|
|
|
const usernames = result[url]; |
|
|
|
|
|
const count = usernames.size; |
|
|
|
|
|
if (count > 2) |
|
{ |
|
foundThreads++; |
|
console.log(`${url}: ${count} `); |
|
} |
|
} |
|
console.log("Threads non-two chatters: " + foundThreads); |
|
}); |
|
} |
|
|
|
function removedNonTwoConvos() |
|
{ |
|
let removedCount = 0; |
|
fs.readFile(filePath, 'utf8', (err, data) => |
|
{ |
|
if (err) |
|
{ |
|
console.error(err); |
|
return; |
|
} |
|
const obj = JSON.parse(data); |
|
|
|
|
|
const map = new Map(); |
|
for (const entry of obj) |
|
{ |
|
const key = entry.thread_href; |
|
const value = map.get(key) || []; |
|
value.push(entry); |
|
map.set(key, value); |
|
} |
|
|
|
|
|
const result = []; |
|
for (const [key, value] of map) |
|
{ |
|
const usernames = new Set(); |
|
for (const entry of value) |
|
{ |
|
usernames.add(entry.message_username); |
|
} |
|
if (usernames.size == 2) |
|
{ |
|
result.push(...value); |
|
} |
|
else |
|
{ |
|
removedCount++; |
|
} |
|
} |
|
|
|
|
|
const output = JSON.stringify(result, null, 2); |
|
fs.writeFile('./two-chatter-only.json', output, 'utf8', (err) => |
|
{ |
|
if (err) |
|
{ |
|
console.error(err); |
|
return; |
|
} |
|
console.log('Output written to output.json | Removed Threads: ' + removedCount); |
|
}); |
|
}); |
|
} |
|
|
|
|
|
function csvToJson(csv) { |
|
|
|
const rows = csv.split("\n"); |
|
|
|
const headers = rows[0].replace("\r", "").split(","); |
|
|
|
|
|
const json = []; |
|
|
|
|
|
for (let i = 1; i < rows.length; i++) { |
|
|
|
const values = rows[i].split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/); |
|
|
|
|
|
const obj = {}; |
|
|
|
|
|
for (let j = 0; j < headers.length; j++) |
|
{ |
|
if (headers[j] !== "id" && values[j] != undefined) |
|
{ |
|
obj[headers[j]] = values[j].replace(/^"|"$/g, "").replace(/""/g, "\"").replace("\"\r", ""); |
|
} |
|
} |
|
|
|
|
|
json.push(obj); |
|
} |
|
|
|
|
|
return JSON.stringify(json, null, 2); |
|
} |