forked from itsabot/itsabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.go
603 lines (577 loc) · 12 KB
/
language.go
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Package language makes it easier to build plugins and natural-sounding
// responses. This package does the following four things:
//
// 1. Provides easy-to-use helpers for returning commonly used, randomized
// text such as greetings.
// 2. Normalizes varied user responses like "yup" or "nah" into something
// to be more easily used by plugins.
// 3. Consolidates triggers by categories (e.g. automotive brands) if
// commonly used across plugins.
// 4. Summarizes text using the custom rule-based algorithm found in
// summarize.go.
package language
import (
"fmt"
"math/rand"
"strings"
"github.com/itsabot/abot/core/log"
)
var yes = map[string]struct{}{
"yes": struct{}{},
"yea": struct{}{},
"yah": struct{}{},
"yeah": struct{}{},
"yup": struct{}{},
"yesh": struct{}{},
"sure": struct{}{},
"aye": struct{}{},
"ok": struct{}{},
"o.k.": struct{}{},
"k": struct{}{},
"kk": struct{}{},
"that's right": struct{}{},
"thats right": struct{}{},
"affirmative": struct{}{},
"perfect": struct{}{},
}
var no = map[string]struct{}{
"no": struct{}{},
"nope": struct{}{},
"nah": struct{}{},
"not sure": struct{}{},
"dunno": struct{}{},
"don't know": struct{}{},
"do not know": struct{}{},
"negative": struct{}{},
}
// Join concatenates triggers together, like Recommend() and Broken(), ensuring
// no duplicates exist
func Join(ss ...[]string) []string {
used := map[string]bool{}
var s []string
for _, tmp := range ss {
for _, w := range tmp {
if !used[w] {
s = append(s, w)
}
used[w] = true
}
}
return s
}
// Greeting returns a randomized greeting.
func Greeting(r *rand.Rand, name string) string {
var n int
if len(name) == 0 {
n = r.Intn(3)
switch n {
case 0:
return fmt.Sprintf("Hi, %s.", name)
case 1:
return fmt.Sprintf("Hello, %s.", name)
case 2:
return fmt.Sprintf("Hi there, %s.", name)
}
} else {
n = r.Intn(3)
switch n {
case 0:
return "Hi. How can I help you?"
case 1:
return "Hello. What can I do for you?"
}
}
log.Debug("greeting failed to return a response")
return ""
}
// Positive returns a randomized positive response to a user message.
func Positive() string {
n := rand.Intn(3)
switch n {
case 0:
return "Great!"
case 1:
return "I'm glad to hear that!"
case 2:
return "Great to hear!"
}
log.Debug("positive failed to return a response")
return ""
}
// Welcome returns a randomized "you're welcome" response to a user message.
func Welcome() string {
n := rand.Intn(5)
switch n {
case 0:
return "You're welcome!"
case 1:
return "Sure thing!"
case 2:
return "I'm happy to help!"
case 3:
return "My pleasure."
case 4:
return "Sure."
}
log.Debug("welcome failed to return a response")
return ""
}
// SuggestedPlace returns a randomized place suggestion useful for recommending
// restaurants, businesses, etc.
func SuggestedPlace(s string) string {
n := rand.Intn(4)
switch n {
case 0:
return "How does this place look? " + s
case 1:
return "How about " + s + "?"
case 2:
return "Have you been here before? " + s
case 3:
return "You could try this: " + s
}
log.Debug("suggestedPlace failed to return a response")
return ""
}
// SuggestedProduct returns natural language, randomized text for a product
// suggestion.
func SuggestedProduct(s string, num uint) string {
var n int
var val, flair string
if num > 0 {
n = rand.Intn(3)
switch n {
case 0, 1:
flair = ", then"
case 2: // do nothing
}
}
n = rand.Intn(6)
switch n {
case 0:
val = "I found just the thing"
case 1:
val = "This is the one for you"
case 2:
val = "You'll love this"
case 3:
val = "This is a real treat"
case 4:
val = "This will amaze you"
case 5:
val = "I found just the thing for you"
}
return val + flair + ". " + s
}
// Foods returns a list of foods useful in a plugin's object triggers.
func Foods() []string {
return []string{
"almonds",
"apple",
"artichokes",
"asparagus",
"avocado",
"bacon",
"banana",
"beef",
"blueberries",
"burger",
"butter",
"can",
"candy",
"cashew",
"cheddar",
"cheese",
"chicken",
"chocolate",
"cream",
"donuts",
"eggs",
"fish",
"food",
"grapes",
"halibut",
"ham",
"hamburger",
"ice",
"lamb",
"mango",
"meatball",
"mozzarella",
"mushroom",
"noodles",
"oyster",
"pancakes",
"pasta",
"peanut",
"pineapple",
"pizza",
"pop",
"popcorn",
"pork",
"potato",
"prawns",
"ramen",
"rice",
"salmon",
"shrimp",
"soda",
"soup",
"steak",
"strawberries",
"sushi",
"sweetcorn",
"tomato",
"tuna",
"turkey",
"waffle",
"watermelon",
}
}
// Desserts returns a []string of dessert types useful in a
// plugin's object triggers
func Desserts() []string {
return []string{
"bar",
"brownie",
"cake",
"cheesecake",
"chocolate",
"cobbler",
"cookie",
"crepe",
"cupcake",
"frosting",
"macaroon",
"muffin",
"pie",
"shortbread",
"shortcake",
"square",
"tart",
}
}
// Restaurants returns a []string of restaurant types useful
// in a plugin's object triggers
func Restaurants() []string {
return []string{
"american",
"asian",
"bakery",
"bar",
"barbecue",
"bbq",
"bistro",
"bouchon",
"brasserie",
"breakfast",
"buffet",
"burger",
"cafe",
"cafeteria",
"cakery",
"carvery",
"chinese",
"coffeehouse",
"concession",
"cosplay",
"cuisine",
"diner",
"dining",
"dinner",
"drive-in",
"eat",
"fast",
"food",
"french",
"greasy",
"haute",
"health",
"italian",
"izakaya",
"japanese",
"korean",
"lunch",
"meadery",
"milk",
"ouzeri",
"pancake",
"parlor",
"pizza",
"pub",
"ramen",
"restaurant",
"roadhouse",
"sandwich",
"seafood",
"snack",
"soda",
"soup",
"steakhouse",
"stop",
"take-out",
"tavern",
"thai",
"theme",
"trattoria",
"truck",
}
}
// Transportation returns a list of vehicle types useful in a plugin's
// object triggers.
func Transportation() []string {
return []string{
"SUV",
"automobile",
"automotive",
"bike",
"bus",
"cab",
"car",
"motorcycle",
"scooter",
"taxi",
"truck",
"van",
"vehicle",
}
}
// Recommend returns a slice of words related to recommending a product, which
// is useful in a plugin's command trigger.
func Recommend() []string {
return []string{
"find",
"show",
"where",
"recommend",
"recommended",
"recommendation",
"recommendations",
}
}
// Repair returns a slice of words related to repairing something, which is
// useful in a plugin's command trigger.
func Repair() []string {
return []string{
"repair",
"repairing",
"fix",
"fixing",
}
}
// Broken returns a slice of words related to something breaking, which is
// useful in a plugin's command trigger.
func Broken() []string {
return []string{
"broke",
"broken",
"breaking",
"help",
"start",
"stop",
"stopped",
"stopping",
}
}
// Purchase returns a slice of words related to purchasing something, which is
// useful in a plugin's command trigger.
func Purchase() []string {
return []string{
"find",
"send",
"get",
"buy",
"order",
"purchase",
"recommend",
"recommendation",
"recommendations",
"want",
"finish",
"complete",
"cancel",
}
}
// QuestionLocation returns a randomized question asking a user where they are.
func QuestionLocation(loc string) string {
if len(loc) == 0 {
n := rand.Intn(10)
switch n {
case 0:
return "Where are you?"
case 1:
return "Where are you now?"
case 2:
return "Sure thing. Where are you?"
case 3:
return "Sure thing. Where are you now?"
case 4:
return "Happy to help. Where are you?"
case 5:
return "Happy to help. Where are you now?"
case 6:
return "I can help with that. Where are you?"
case 7:
return "I can help with that. Where are you now?"
case 8:
return "I can help solve this. Where are you?"
case 9:
return "I can help solve this. Where are you now?"
}
}
return fmt.Sprintf("Are you still near %s?", loc)
}
// Yes determines if a specific word is a positive "Yes" response. For example,
// "yeah" returns true.
func Yes(s string) bool {
s = strings.ToLower(s)
_, ok := yes[s]
return ok
}
// No determines if a specific word is a "No" response. For example, "nah"
// returns true.
func No(s string) bool {
s = strings.ToLower(s)
_, ok := no[s]
return ok
}
// SliceToString converts a slice of strings into a natural-language list with
// appropriately placed commas and a custom and/or separator.
func SliceToString(ss []string, andor string) string {
l := len(ss)
if l == 0 {
return ""
}
if l == 1 {
return ss[0]
}
if l == 2 {
if andor == "." {
tmp := strings.Title(ss[1][:1]) + ss[1][1:]
return fmt.Sprintf("%s%s %s", ss[0], andor, tmp)
}
return fmt.Sprintf("%s %s %s", ss[0], andor, ss[1])
}
var ret string
// TODO handle andor == "."
for i, s := range ss {
if i == l-2 {
ret += fmt.Sprintf("%s %s", s, andor)
} else if i == l-1 {
ret += " " + s
} else {
ret += s + ", "
}
}
return ret
}
// StopWords are articles that can be ignored by Abot.
var StopWords = []string{
"a",
"an",
"the",
}
// Prepositions contains the most commonly used prepositions.
var Prepositions = map[string]struct{}{
"aboard": struct{}{},
"about": struct{}{},
"above": struct{}{},
"across": struct{}{},
"after": struct{}{},
"against": struct{}{},
"along": struct{}{},
"amid": struct{}{},
"among": struct{}{},
"anti": struct{}{},
"around": struct{}{},
"as": struct{}{},
"at": struct{}{},
"before": struct{}{},
"behind": struct{}{},
"below": struct{}{},
"beneath": struct{}{},
"beside": struct{}{},
"besides": struct{}{},
"between": struct{}{},
"beyond": struct{}{},
"but": struct{}{},
"by": struct{}{},
"concerning": struct{}{},
"considering": struct{}{},
"despite": struct{}{},
"down": struct{}{},
"during": struct{}{},
"except": struct{}{},
"excepting": struct{}{},
"excluding": struct{}{},
"following": struct{}{},
"for": struct{}{},
"from": struct{}{},
"in": struct{}{},
"inside": struct{}{},
"into": struct{}{},
"like": struct{}{},
"minus": struct{}{},
"near": struct{}{},
"of": struct{}{},
"off": struct{}{},
"on": struct{}{},
"onto": struct{}{},
"opposite": struct{}{},
"outside": struct{}{},
"over": struct{}{},
"past": struct{}{},
"per": struct{}{},
"plus": struct{}{},
"regarding": struct{}{},
"round": struct{}{},
"save": struct{}{},
"since": struct{}{},
"than": struct{}{},
"through": struct{}{},
"to": struct{}{},
"toward": struct{}{},
"towards": struct{}{},
"under": struct{}{},
"underneath": struct{}{},
"unlike": struct{}{},
"until": struct{}{},
"up": struct{}{},
"upon": struct{}{},
"versus": struct{}{},
"via": struct{}{},
"with": struct{}{},
"within": struct{}{},
"without": struct{}{},
}
// RemoveStopWords finds and removes stopwords from a slice of strings.
func RemoveStopWords(ss []string) []string {
var removal []int
for i, s := range ss {
if Contains(StopWords, s) {
removal = append(removal, i)
}
}
for _, i := range removal {
ss = append(ss[:i], ss[i+1:]...)
}
return ss
}
// NiceMeetingYou is used to greet the user and request signup during an
// onboarding process.
func NiceMeetingYou() string {
n := rand.Intn(3)
switch n {
case 0:
return "It's nice to meet you. If we're going to work " +
"together, can you sign up for me here? "
case 1:
return "Nice meeting you. Before we take this further, can " +
"you sign up for me here? "
case 2:
return "Great to meet you! Can you sign up for me here to " +
"get started? "
}
log.Debug("nicemeetingyou failed to return a response")
return ""
}