forked from lexoyo/marcel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththinker.js
More file actions
64 lines (58 loc) · 1.71 KB
/
thinker.js
File metadata and controls
64 lines (58 loc) · 1.71 KB
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
const config = require('./package.json').marcel;
const Speaker = require('./speaker').Speaker;
const speaker = new Speaker(config);
const StateMachine = require("javascript-state-machine");
const Modes = {
LISTEN: 'listen',
SPEAK: 'speak',
LISTEN_WITH_PREDEFINED_WORDS: 'listenWithPredefinedWords',
}
exports.Modes = Modes;
const Lang = {
EN: 'en',
FR: 'fr',
}
exports.Lang = Lang;
const Thinker = function(config) {
this.config = config;
this.state = {
activity: StateMachine.create(config.states.activity),
lang: StateMachine.create(config.states.lang),
}
};
Thinker.prototype.lang = Lang.EN;
Thinker.prototype.mode = Modes.LISTEN_WITH_PREDEFINED_WORDS;
Thinker.prototype.think = function(phrase, cbk) {
// console.log('thinking', phrase, this.state.activity.current, this.state.lang.current);
if(phrase.indexOf('MARCEL') != -1) {
speaker.say('Yes I\'m Marcel!', function() {
cbk();
});
}
else {
// find the events of wich all words are in the phrase
let hasChanged = false;
for (let stateName in this.state) {
const state = this.state[stateName];
state.transitions().forEach(eventName => {
const isSaid = eventName.split(' ').reduce((prev, word) => prev && phrase.indexOf(word.toUpperCase()) > -1, true);
if(isSaid) {
// console.log('change state', isSaid, eventName);
state[eventName]();
hasChanged = true;
}
});
}
if(hasChanged) {
console.log('new state:', this.state.activity.current, this.state.lang.current)
speaker.say('switching state!', function() {
cbk();
});
}
else {
console.log('Nothing to do', phrase);
cbk();
}
}
};
exports.Thinker = Thinker;