-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirstUnit.js
150 lines (113 loc) · 3.04 KB
/
firstUnit.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
// require NaifJS dialog manager module
const {
say, next, exec, setvar, getvar,
match, slots, take
} = require('../../')
// require random helper module
const { rand, optional, } = require('./lib/random')
//
// dialogs tag shortcuts
//
const verifyName = 'firstUnit.verifyName'
const presentations = 'firstUnit.presentations'
const askRestart = 'secondUnit.askRestart'
/**
* sendImageDog
*
* write an ASCII art image of a dog,
* simulating in text mode, an image output
* @private
*/
function sendImageDog() {
say(`
o-''|\\_____/)
\\_/|_) )
\\ __ /
(_/ (_/ `)
}
/**
* firstUnit
* dialog states and properties
*/
const firstUnit = {
/**
* dialog function: dogIntroduction
* type: prompt
*/
dogIntroduction() {
// visualize an "image"
sendImageDog()
say('Hello! I\'m Pluto. I\'m a dog.')
say('What is your name?')
next(presentations)
},
/**
* presentations
* input state function
*
* @param {requestDataObject} request
*/
presentations(request) {
// rule to get a first name
// Anna Lisa
// Pier Luigi
// Giorgio La Malfa
const firstNameReg = '(?<firstName>[A-Z][a-z]{1,}(\\s[A-Z][a-z]{1,})*)'
const introNameReg = '^(my name is |you can name me |I\'m )?'
const FIRSTNAME_REGEX = new RegExp ( introNameReg + firstNameReg )
switch (take(request)) {
// pattern 1
case match( /hi|hello|good morning|good afternoon|greetings|hey|hallo|hallo|ciao/i ):
say('Hello yourself! Nice to meet you.')
say('What\'s your name?')
break
// pattern 2
case match( /I am a chicken/ ):
say('Nice to meet you! You are a nice chicken.')
say('What\'s your name?')
break
// pattern 3
case match( FIRSTNAME_REGEX ): {
// store name in a dialog unit variable
const firstName = setvar('firstName', slots().firstName)
say(rand(`Is ${firstName} your name?|Really is ${firstName} your name?`))
next(verifyName)
break
}
// balblabla
default:
say( rand('I do not understand.|I don\'t think I understand.|mmhhh.') )
say( rand('What is your name?|What\'s your name?|What\'s with the name, anyway?') )
break
}
},
/**
* subdialog function:
* verifyName
*
* type:
* sentence processing
*/
verifyName(sentence) {
switch(take(sentence)) {
// Yes
case match( /^(yes|really|true|right|correct|sure)/i ): {
const firstName = getvar('firstName')
say(rand(`Hi ${firstName}|Nice to meet you.${optional(' Your name is very nice!')}`))
exec(askRestart)
break
}
// No
case match( /^no/i ):
say('What\'s with your name, anyway?')
next(presentations)
break
// fallback
default:
say('I do not understand. What\'s your name?')
next(presentations)
break
}
}
}
module.exports = firstUnit