-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
127 lines (114 loc) · 4.06 KB
/
index.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
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
import * as NLP from "node-nlp"
import { Identifier, QuestionArray, ResponseArray } from "./types/NlpContextMember"
import { NlpManager } from "./types/manager"
export class NlpContext<T> {
nlp: NlpManager
questionArray: Array<QuestionArray>
responseArray: Array<ResponseArray<T>>
identifierArray: Array<Identifier>
constructor(dataConstruct: T) {
this.nlp = new NLP.NlpManager({ languages: ['id', 'en'] })
this.questionArray = []
this.responseArray = []
this.identifierArray = []
}
/**
* Add a question to the NLP context
*
* @param language The language of the question
* @param question The question to be added
* @param className The class name of the question
* @returns void
*/
addInput(language: string, question: string, className: string): void {
this.questionArray.push({ language, question, className })
}
/**
* Add a response to the NLP context
*
* @param language The language of the response
* @param className The class name of the response
* @param callback The callback to be executed
* @returns void
* @memberof NlpContext
* @template T
* @param data The data to be passed to the callback
* @returns void
*/
addResponse(language: string, className: string, callback: (param: T) => void): void {
this.responseArray.push({ language, className, callback })
}
/**
* Add multiple responses to the NLP context
*
* @param responses The responses to be added
* @returns void
* @memberof NlpContext
* @template T
* @param data The data to be passed to the callback
* @returns void
*/
addResponses(responses: Array<ResponseArray<T>>) {
responses.forEach(response => {
this.addResponse(response.language, response.className, response.callback)
})
}
/**
* Initialize document and train the NLP
*
* @returns void
*/
async init() {
this.questionArray.forEach(question => {
this.nlp.addDocument(question.language, question.question, question.className)
})
await this.nlp.train()
}
/**
* Process an input
*
* @param language The language of the input
* @param identifier The identifier of the input
* @param question The question to be processed
* @param data The data to be passed to the callback
* @returns void
*/
async process(language: string, identifier: string, question: string, data: T) {
// let nlp choose intent (or we call className)
const process = await this.nlp.process(language, question)
// get the intent
const className = process.intent
const response = this.responseArray.find(response => response.className === className)
if (!response) return
// first, we need find the identifier
const identifierIndex = this.identifierArray.find(identifierObject => identifierObject.id === identifier)
// if the identifier is not found, we create a new one
if (!identifierIndex) {
this.identifierArray.push({
id: identifier,
state: className,
needAnswer: false
})
} else {
// if the identifier is found and don't need answer, we can change the state
if (!identifierIndex.needAnswer) {
identifierIndex.state = className
}
}
return response.callback(data)
}
/**
* Change the state of an identifier
*
* @param identifier The identifier to be changed
* @param state The new state
* @param needAnswer If the identifier need answer
* @returns void
*/
changeState(identifier: string, state: string, needAnswer: boolean = false) {
const identifierIndex = this.identifierArray.find(identifierObject => identifierObject.id === identifier)
if (!identifierIndex) return
identifierIndex.state = state
identifierIndex.needAnswer = needAnswer
}
}