-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateData.js
62 lines (56 loc) · 1.63 KB
/
createData.js
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
const fs = require('fs');
var yaml = require('js-yaml');
var levenshtein = require('fast-levenshtein');
// Declare document and messagePairs
let doc;
let messagePairs = {};
// Parse yaml document of 30309 text messages (896249 words; two years of data) and convert into json object
try {
doc = yaml.safeLoad(fs.readFileSync('./data/textData.yaml', 'utf8'));
doc = doc.sms.slice(0, 30309);
} catch (e) {
console.log(e);
}
let newKey = ''; // key is the message
let newValue = ''; // value is response
let prevType = 0;
// Get rid of strings that are similar to size down data set
for (var i = 0; i < doc.length; i++) {
let currType = doc[i]._type;
let currBody = doc[i]._body;
if (currType === 1) {
//if doris just finished replying
if (prevType === 2) {
//add key + value to your dict
let add = true;
let keys = Object.keys(messagePairs);
for (var j = 0; j < keys.length; j++) {
let distance = levenshtein.get(keys[j], newKey);
let level = distance / Math.max(keys[j].length, newKey.length);
if (level < 0.25) {
add = false;
break;
}
}
if (add) {
messagePairs[newKey] = newValue;
}
//reset newKey and value
newKey = '';
newValue = '';
}
newKey = `${newKey} ${currBody}`;
}
if (currType === 2) {
newValue = `${newValue} ${currBody}`;
}
prevType = currType;
}
// write results into file with json format
fs.writeFile('./data/messagePairs.json', JSON.stringify(messagePairs, null, 4), (err) => {
if (err) {
console.error(err);
return;
}
console.log('Yay! Successfully created file');
});