-
Notifications
You must be signed in to change notification settings - Fork 11
/
spell-checker-middleware.js
163 lines (126 loc) · 4.85 KB
/
spell-checker-middleware.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
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
/* SPELL CHECKER MIDDLEWARE */
var nspell = require('nspell')
const fs = require('fs');
var request = require('request');
var http = require('http');
var enDIC = fs.readFileSync("./node_modules/dictionary-en-us/index.dic", 'utf-8')
var enAFF = fs.readFileSync("./node_modules/dictionary-en-us/index.aff", 'utf-8')
var spellEN = nspell(enAFF, enDIC)
var frDIC = fs.readFileSync("./node_modules/dictionary-fr/index.dic", 'utf-8')
var frAFF = fs.readFileSync("./node_modules/dictionary-fr/index.aff", 'utf-8')
var spellFR = nspell(frAFF, frDIC)
var speller = spellEN;
var currentLang = "en";
// FIXED GREETINGS
var greetings = {};
greetings["hello"] = true;
greetings["hi"] = true;
greetings["good morning"] = true;
greetings["good evening"] = true;
greetings["hey"] = true;
greetings["bonjour"] = true;
greetings["bonsoir"] = true;
// FUNCTION TO DETECT IF MESSAGE IS GREETING
var isGreetings = function(message) {
var lowerCaseMessage = message.text.toLowerCase().replace(/[.,\/#!?$%\^&\*;:{}=_`~()]/g,'');
if (greetings[lowerCaseMessage]) return true;
return false;
}
// FUNCTION TO PERFORM THE SPELL CHECK
var performMisspellingCheck = function(message) {
// empty string where to append corrected words
var messageMisspelledFree = "";
var words = message.split(" ");
for (var i = 0; i < words.length; i++) {
// check for each word if is it misspelled
if (speller.correct(words[i]) == false && isNaN(words[i])) {
var corrections = speller.suggest(words[i])
if (corrections.length > 0) {
// if it is and at least a correction exists append the first one
messageMisspelledFree += corrections[0];
} else {
// otherwise append the original word
messageMisspelledFree += words[i];
}
} else {
// otherwise append the original word
messageMisspelledFree += words[i];
}
if (i != words.length - 1) {
messageMisspelledFree += ' ';
}
}
return messageMisspelledFree;
}
// SPELL-CHECKER MIDDLEWARE FUNCTION
module.exports = function() {
var middleware = {}
middleware.receive = function(bot, message, next) {
if (!message.text) {
next();
return;
}
if (message.is_echo || message.type === 'self_message') {
next();
return;
}
// trigger language detection in case of long sentences or greetings
if (message.text.split(" ").length > 1 || isGreetings(message)) {
// LANGUAGE CHECK
// prepare arguments for the request to Google Translate API
var url = "https://translate.googleapis.com/translate_a/single"
var parameters = {
q: message.text,
dt: 't',
tl: 'it',
sl: 'auto',
client: 'gtx',
hl: 'it'
};
request({
url: url,
qs: parameters
}, function(err, response, body) {
if (err) {
console.error("Error during language detection!");
next(err);
}
// get language from json
var res = JSON.parse(body);
var lang = res[2];
// update accordingly the speller and the global var
if (lang == "fr") {
speller = spellFR;
currentLang = "fr";
} else if (lang == "en") {
speller = spellEN;
currentLang = "en";
}
// otherwise don't change anything
// SPELL CHECKING
// perform the misspelling with the (potentially) updated speller
var strippedMessage = message.text.replace(/[.,\/#!?$%\^&\*;:{}=_`~()]/g,'');
var cleanMessage = performMisspellingCheck(strippedMessage);
message.old = strippedMessage;
message.text = cleanMessage;
// fill the language field in order to send it to dialogflow api
message.lang = currentLang;
next()
});
} else {
// perform the misspelling with the same speller as before
var strippedMessage = message.text.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,'');
var cleanMessage = performMisspellingCheck(strippedMessage);
message.old = strippedMessage;
message.text = cleanMessage;
// fill the language field in order to send it to dialogflow api
message.lang = currentLang;
next();
}
return;
}
middleware.currentLang = function() {
return currentLang;
}
return middleware;
}