File size: 856 Bytes
499003f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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!');
});