-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (74 loc) · 2.58 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var path = require('path');
var LineByLineReader = require('line-by-line')
String.prototype.replaceAll = function(search, replacement) {
var target = this
return target.split(search).join(replacement)
};
const tasks = [
path.join(__dirname, './data/systemessentials.txt'),
path.join(__dirname, './data/substitutes.txt'),
path.join(__dirname, './data/contractions.txt'),
path.join(__dirname, './data/interjections.txt'),
path.join(__dirname, './data/british.txt'),
path.join(__dirname, './data/spellfix.txt'),
path.join(__dirname, './data/texting.txt'),
path.join(__dirname, './data/numbers.txt')
]
function normalize(message) {
return new Promise((resolve, reject) => {
var cleanMessage = message.trim()
// Burst out . , & ?
cleanMessage = cleanMessage.replaceAll('.', ' .')
cleanMessage = cleanMessage.replaceAll('?', ' ?')
cleanMessage = cleanMessage.replaceAll(',', ' ,')
// Burst out operators
cleanMessage = cleanMessage.replaceAll('+', ' + ')
cleanMessage = cleanMessage.replaceAll('-', ' - ')
cleanMessage = cleanMessage.replaceAll('*', ' * ')
cleanMessage = cleanMessage.replaceAll('/', ' / ')
cleanMessage = ' ' + cleanMessage + ' '
cleanFromFile(tasks[0], cleanMessage)
.then(output => cleanFromFile(tasks[1], output))
.then(output => cleanFromFile(tasks[2], output))
.then(output => cleanFromFile(tasks[3], output))
.then(output => cleanFromFile(tasks[4], output))
.then(output => cleanFromFile(tasks[5], output))
.then(output => cleanFromFile(tasks[6], output))
.then(output => cleanFromFile(tasks[7], output))
.then(output => cleanOutput(output))
.then(output => {
resolve(output)
})
})
}
function cleanFromFile(path, msg) {
return new Promise((resolve, reject) => {
var lr = new LineByLineReader(path)
lr.on('line', function (line) {
var checker = line.split(' ')
if (checker.length >= 2 && checker[0] !== '') {
if (msg.indexOf(' ' + checker[0].replace('_', ' ') + ' ') > -1) {
if (checker[1]) {
// console.log(checker[0], ' ' + checker[1].replace('_', ' ') + ' ')
var replacement = checker[1]
if (replacement !== '+') {
replacement = replacement.replaceAll('+', ' ')
}
msg = msg.replace(checker[0], checker[1])
}
}
}
})
lr.on('end', function() {
resolve(msg)
})
})
}
function cleanOutput(msg) {
return new Promise(resolve => {
msg = msg.trim()
msg = msg.replaceAll(' ', ' ')
resolve(msg)
})
}
module.exports = normalize