// Load the fs module to read files const fs = require('fs'); // Define the path to the json file 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; } // Use the csvToJson function to convert the data to json const json = csvToJson(data); fs.writeFile("data/rawData.json", json, (err) => { if (err) { console.error(err); return; } console.log("File saved successfully"); }); // Do something with the json }); } //Read the file asynchronously and parse it as json function validate() { fs.readFile(filePath, 'utf8', (err, data) => { if (err) { console.error(err); return; } const json = JSON.parse(data); // Create an empty object to store the results const result = {}; // Loop through each entry in the json array for (const entry of json) { // Get the url and message_username fields from the entry const url = entry.thread_href; const message_username = entry.message_username; // If the url is not already in the result object, create an empty set for it if (!result[url]) { result[url] = new Set(); } // Add the message_username to the set for the url result[url].add(message_username); } // Loop through each url in the result object and get the size of the set let foundThreads = 0; for (const url in result) { // Get the set of message_usernames for the url const usernames = result[url]; // Get the count of unique message_usernames by getting the size of the set const count = usernames.size; // Print the url and the count 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); // Group the entries by thread_href using a Map 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); } // Filter out the threads with more than two different message_username entries 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++; } } // Stringify the result and write it to a new file 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); }); }); } // Define a function that takes a csv string as an argument function csvToJson(csv) { // Split the csv string by newline characters to get an array of rows const rows = csv.split("\n"); // Get the first row as an array of headers const headers = rows[0].replace("\r", "").split(","); // Create an empty array to store the json objects const json = []; // Loop through the remaining rows for (let i = 1; i < rows.length; i++) { // Get the current row as an array of values const values = rows[i].split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/); // Create an empty object to store the key-value pairs const obj = {}; // Loop through the headers and values and assign them to the object 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", ""); } } // Push the object to the json array json.push(obj); } // Return the json array as a string return JSON.stringify(json, null, 2); }