all-scam-spam / spam_assassin.js
FredZhang7's picture
add examples of data preprocessing
499003f
raw
history blame
856 Bytes
const fs = require('fs');
const corpus = require('@stdlib/datasets-spam-assassin');
const data = corpus();
let csvData = "text,is_spam\n";
function cleanText(str) {
if (str.startsWith('""') && str.endsWith('""')) str = str.substring(2, str.length - 2)
else if (str.startsWith('"') && str.endsWith('"')) str = str.substring(1, str.length - 1)
str = str.replace(/["]/g, '""')
if (str.includes('\n') || str.includes(',') || str.includes(`""`)) str = '"' + str + '"'
return str
}
for (let i = 0; i < data.length; i++) {
const text = data[i].text;
const group = data[i].group;
let isSpam = 0;
if (group.includes('spam')) {
isSpam = 1;
}
csvData += cleanText(text) + ',' + isSpam + '\n';
}
fs.writeFile('spam_data.csv', csvData, (err) => {
if (err) throw err;
console.log('CSV file saved!');
});