forked from tajddin/voiceplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intents.js
228 lines (207 loc) · 4.86 KB
/
intents.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
///////////////////////////////////////////////////////////////////////////////////////////////////////
//// intents.js allows us to train the app with examples and classify the inputted statements to match
//// one of a set of possible intents (e.g. play, pause, skip, movie-info, etc.).
///////////////////////////////////////////////////////////////////////////////////////////////////////
var openNLP = require("opennlp");
var natural = require('natural');
var nlp = require('nlp_compromise');
var that = this;
var intents = [
{
name: 'play',
strict: false,
statements: [
'play a',
'put on',
'can you play',
'show me',
'show us',
'show',
'play some',
'play something like',
'play a video by',
'play something by',
'show me a video by',
'play a show'
]
},
{
name: 'open-video',
strict: false,
statements: [
'open this',
'launch this on my desktop',
'pull this up on my computer',
'put this on my computer',
'put this on my desktop',
'show this to me on my mac',
'put this on my desktop',
'paul this up on my'
]
},
{
name: 'movieinfo',
strict: true,
declaritive: true,
statements: [
'what is this about',
'what is this',
'more information',
'what is this about',
'what\'s this video about',
'what are we watching',
'what is this movie about',
'what\'s playing',
'what\'s currently playing',
'what are we watching',
'more info'
]
},
{
name: 'skip',
strict: false,
statements: [
'next',
'play something else',
'another',
'play something different',
'to play something different',
'skip this'
]
},
{
name: 'pause',
strict: false,
statements: [
'pause',
'stop playback',
'stop',
'stop playing'
]
},
{
name: 'resume',
strict: false,
statements: [
'continue',
'resume playback',
'keep playing'
]
},
{
name: 'back',
strict: false,
statements: [
'go back',
'to go back',
'go back to that last',
'previous',
'play the last',
]
},
{
name: 'replay',
strict: false,
statements: [
'replay this',
'to replay',
'start this over',
'start from the beginning',
'to start over',
'to restart this',
'restart this video',
'play this again',
'repeat',
'play again'
]
}
];
exports.getIntentByName = function (name) {
var returnIntent = null;
intents.forEach(function (element) {
if (element.name == name) {
returnIntent = element;
return;
}
}, this);
return returnIntent;
}
// Needs improvements
exports.affirmativeIntent = function (q) {
return q.query && q.query.indexOf('yes') > -1
}
exports.determineIntent = function (statement) {
if (statement) {
var classifier = new natural.BayesClassifier();
var i = null;
intents.forEach(function (intent) {
intent.statements.forEach(function (matchStatement) {
if (intent.strict) {
if (matchStatement.indexOf(statement) > -1 || statement.indexOf(matchStatement) > -1) {
i = intent;
}
}
classifier.addDocument(matchStatement, intent.name);
}, this);
}, this);
if (i) {
return i;
} else {
classifier.train();
return that.getIntentByName(classifier.classify(statement));
}
} else {
return that.getIntentByName('play');;
}
};
exports.generateSearchQuery = function (statement) {
if (statement) {
var r = '';
var s = nlp.pos(statement).sentences[0];
var a = [];
a = a.concat(s.entities());
a = a.concat(s.people());
a = a.concat(s.nouns());
a = a.concat(s.adjectives());
a = a.concat(s.adverbs());
a = a.concat(s.values());
a = a.concat(nlp.value(statement).date());
a.forEach(function (element) {
if (element && element.normalised && r.indexOf(element.normalised) == -1) {
r = element.normalised + ' ' + r;
}
}, this);
return that.generateEasterEgg(r);
} else {
return statement;
}
};
exports.generateEasterEgg = function (statement) {
var s = statement;
if (s && s.trim() == 'something') {
//do random at some point:
s = 'funny videos compilation';
}
return s;
};
/*
that.determineIntent('play a video by michael jackson');
that.determineIntent('restart this video');
that.determineIntent('to play something else');
that.determineIntent('play something different');
that.determineIntent('what\'s this video about');
that.determineIntent('Billy Jean');
that.determineIntent('Play the last video');
that.determineIntent('Give me more information about this video');
that.determineIntent('what\'s playing');
that.determineIntent('what is');
that.determineIntent('play something else');
that.determineIntent('play this again');
that.determineIntent('repeat');
that.determineIntent('more info');
that.determineIntent('play some keeping up with the kardashians');
that.determineIntent('awsome fail footage');
that.determineIntent('go back');
that.determineIntent('for Apple Macintosh software tutorials');
that.determineIntent('play a three second video');
*/