-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlp.ts
49 lines (45 loc) · 1.41 KB
/
nlp.ts
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
import { TurnContext } from 'botbuilder'
import { NlpEntity, NlpResult } from 'wolf-core'
// Creating a simple regex for hi to test if it is in the user message
const greetTest = new RegExp('^hi')
// Creating a function that either returns null, or the wolf-defined Entity Object if name is detected
const nameRecognizer = (input: string): NlpEntity | null => {
const nameReg = /my name is (\w*)/
const result = nameReg.exec(input)
if (!result) {
return null
}
return {
name: 'name',
value: result[1],
text: result[1]
}
}
export default (context: TurnContext): NlpResult[] => {
// First test to see if the utterance is a greeting
const isGreeting = greetTest.test(context.activity.text)
if (isGreeting) {
// If it is a greeting, detect to see if the greeting has a name in there
const nameFound = nameRecognizer(context.activity.text)
if (nameFound) {
// if there is a name, return the entire Nlp Result with the name entity
return [{
message: context.activity.text,
intent: 'greet',
entities: [nameFound]
}]
}
// if there is no name, return the Nlp Result with no entities
return [{
message: context.activity.text,
intent: 'greet',
entities: []
}]
}
// if the utterance is not a greeting, just return the default Nlp Result
return [{
message: context.activity.text,
intent: null,
entities: []
}]
}