File size: 4,476 Bytes
284253a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
// 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);
} |