/
tagger.js
312 lines (224 loc) · 8.63 KB
/
tagger.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Tagger.js
//
// Breaks up speech into components and assists with
// classifying things such as the subject, ownership,
// and action for a statement
//
// Note: I am not a linguist, this is the result of
// blood, sweat, and tears!
//
// Please help me make this better:
// https://github.com/nhunzaker/nodebot
// -------------------------------------------------- //
var lev = require("levenshtein")
, pos = require('pos')
, lexer = new pos.Lexer()
, tagger = new pos.Tagger()
, fileEx = Nodebot.lexicon.file["regular expression"]
;
// Finds the closest match between a statement
// and a body of words
var closest = module.exports.closest = function(string, words) {
var shortest = words.toString().length
, bestFit = "";
if (typeof words === 'string') {
words = lexer.lex(words);
}
words.forEach(function(word) {
var distance = lev(string, word);
if (distance < shortest) {
bestFit = word;
shortest = distance;
}
});
return bestFit;
}
// Checks if a string is fileish
var isFile = module.exports.isFile = function(string) {
string = string || "";
return (string.replace(/\s/g, "").match(fileEx) !== null);
};
// Returns the part of speech for a particular word
var getType = module.exports.getType = function (string) {
if (string) {
return tagger.tag(lexer.lex(string))[0][1];
} else {
return undefined;
}
};
// Finds all words between the last of the first and last
// of two types
var getBetween = module.exports.getBetween = function(lex, type1, type2, form) {
var tagged = tagger.tag(lex)
, filter1 = filter2 = start = end = [];
form = form || "outside"
type1 = (typeof type1 === 'string') ? [type1] : type1;
type2 = (typeof type2 === 'string') ? [type2] : type2;
filter1 = tagged.filter(function(i) { return type1.indexOf(i[1]) !== -1 }) || [];
filter2 = tagged.filter(function(i) { return type2.indexOf(i[1]) !== -1 }) || [];
if (form === "outside") {
start = (filter1[0]) ? filter1[0][0] : undefined
} else {
start = (filter1.slice(-1)[0]) ? filter1.slice(-1)[0][0] : undefined
}
end = (filter2.slice(-1)[0]) ? filter2.slice(-1)[0][0] : undefined;
return (start || end) ? lex.slice(lex.indexOf(start) + 1, lex.indexOf(end) + 1) : [];
};
// Classifies all words in an array
var getTypes = module.exports.getTypes = function (array, string, strict) {
var type = array.filter(function(word) {
if (strict) {
return (word[1] === string);
} else {
return (word[1].slice(0,string.length) === string);
}
}).map(function(w) { return w[0] });
return type;
};
var classify = module.exports.classify = function(speech, debug) {
var text = speech || process.argv.slice(2).join(" ")
, words = lexer.lex(text)
, tagged = tagger.tag(words)
, action = subject = owner = false;
if (debug) {
console.log(tagged);
}
// Classify!
// -------------------------------------------------- //
var verbs = getTypes(tagged, "VB")
, nouns = getTypes(tagged, "NN")
, pronouns = getTypes(tagged, "PRP") // finds all posessive pronouns
, actions = getTypes(tagged, "W")
, adverbs = getTypes(tagged, "R")
, adjectives = getTypes(tagged, "JJ")
, websites = getTypes(tagged, "URL")
, preps = getTypes(tagged, "IN")
, determiners = getTypes(tagged, "DT")
;
// ACTION
// Answers : "What should the nodebot do after given a
// command"
// -------------------------------------------------- //
// Are there actions present?
if (actions.length > 0) {
action = actions[0];
} else if (getTypes(tagged, "VB", true).length > 0) {
// Are there base verbs present?
action = getTypes(tagged, "VB", true).slice(-1)[0];
// Are there at least any adjectives that might work?
} else {
action = adjectives[0];
}
// OWNERSHIP
// Answers : "Who is associated with the target of the
// action?"
// -------------------------------------------------- //
var posession = tagged.filter(function(i) { return i[1] === "PRP$" || i[1] === "PRP"; });
// If there is posession and we have an action, then
// the owner is the posessive word
if (posession.length > 0 && action) {
owner = posession[0][0];
// More bulletproofing, if the owner word is further
// in the sentence than the action, then we need to igore
// all of the verbs/posessives before the action
//
// ex: "Do you know what the current directory is?"
if (words.indexOf(owner) < words.indexOf(action)) {
owner = getBetween(words, ["DT"], "NN").join(" ");
}
}
// No ? Let's try between a preposition and
// determiners/nouns
else if (determiners.length > 0 && preps.length > 0) {
owner = getBetween(words, ["IN"], ["DT", "NN", "."]);
// Strip accidental determinates
if (getType(owner[0]) === "DT") owner = owner.slice(1);
// Strip accidental punctuation
if (getType(owner.slice(-1)[0]) === ".") owner = owner.slice(0, -1);
owner = owner.join(" ").trim();
}
// At this point, we can really only guess that
// the owner is between the verb and the end of the
// statement
else if (verbs.length > 0) {
owner = getBetween(words, ["VBZ", "VBP"], ".").slice(0, -1)
// Strip accidental determinates
if (getType(owner[0]) === "DT") owner = owner.slice(1);
// Strip accidental puncuation
if (getType(owner[0]) === ".") owner = owner.slice(1);
owner = owner.join(" ").trim();
}
// SUBJECT
// Answers : "What is this statement about?"
// -------------------------------------------------- //
// If there is a file within the statement, it's probably
// the subject
if (speech.match(fileEx) !== null) {
subject = speech.match(fileEx)[0].trim();
}
// If there is a website within the statement, it's probably
// the subject
else if (websites.length > 0) {
subject = websites[0].trim()
}
// If ownership and there are prepositions, scan for words beween
// prepositions, determinates, and posessive words and
// prepositions, nouns, and puncuation
else if (owner && preps.length > 0) {
debug && console.log("fire");
// To account for more than one preposition, we need to be able to filter between
// either the inside or outside preposition
if (preps.length === 1) {
subject = getBetween(words, ["IN", "DT", "PRP$"], ["IN", "NN", "."], "outside");
} else {
subject = getBetween(words, ["IN", "DT", "PRP$"], ["IN", "NN", "."], "inside");
}
// Autocorrect for trailing punctuation
if (getType(subject.slice(-1)[0]) === ".") {
subject = subject.slice(0, -1);
}
// Autocorrect for trailing ownership
if (subject.slice(-1)[0] === owner) {
subject = subject.slice(0, -1);
}
// Autocorrect for trailing prepositions
if (getType(subject.slice(-1)[0]) === "IN") {
subject = subject.slice(0, -1);
}
subject = subject.join(" ").trim();
}
// Okay, last chance. If there *is* ownership, and there are no prepositions
// then the subject is inside the owner/determinate/verb and the last noun
// (*phew...*)
else if (owner && preps.length === 0) {
subject = getBetween(words, ["DT", "VBP", "PRP$"], "NN", "inside").join(" ");
}
// Now that everything is properly classified,
// let's filter the ownership
switch(owner) {
// Reverse user possession
case "me": case "my": case "i": case "I":
owner = "user";
break;
// Reverse nodebot possession
case "your": case "you":
owner = "nodebot";
break;
// Tweak other non-specific possession cases to the last
// recorded context
case "": case "it": case "its":
case "they": case "their": case "he": case "she":
case "his": case "hers":
owner = Nodebot.memory.context;
break;
}
// Return what we find
// -------------------------------------------------- //
var ret = {
action : (action) ? action.toLowerCase() : undefined,
owner : owner,
subject : subject,
tokens : words
};
return ret;
}