|
1 | | -// /* |
2 | | -// * Copyright (c) AXA Group Operations Spain S.A. |
3 | | -// * |
4 | | -// * Permission is hereby granted, free of charge, to any person obtaining |
5 | | -// * a copy of this software and associated documentation files (the |
6 | | -// * "Software"), to deal in the Software without restriction, including |
7 | | -// * without limitation the rights to use, copy, modify, merge, publish, |
8 | | -// * distribute, sublicense, and/or sell copies of the Software, and to |
9 | | -// * permit persons to whom the Software is furnished to do so, subject to |
10 | | -// * the following conditions: |
11 | | -// * |
12 | | -// * The above copyright notice and this permission notice shall be |
13 | | -// * included in all copies or substantial portions of the Software. |
14 | | -// * |
15 | | -// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16 | | -// * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17 | | -// * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18 | | -// * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
19 | | -// * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
20 | | -// * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
21 | | -// * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22 | | -// */ |
23 | | -// |
24 | | -// |
25 | | -// import { NlpManager } from '../nlp'; |
26 | | -// import MemoryConversationContext from './memory-conversation-context'; |
27 | | -// import { Session } from '../types/session'; |
28 | | -// |
29 | | -// /** |
30 | | -// * Microsoft Bot Framework compatible recognizer for nlp.js. |
31 | | -// */ |
32 | | -// class Recognizer { |
33 | | -// private readonly nlpManager: NlpManager; |
34 | | -// private readonly threshold: number; |
35 | | -// private readonly conversationContext: MemoryConversationContext; |
36 | | -// |
37 | | -// /** |
38 | | -// * Constructor of the class. |
39 | | -// * @param {Object} settings Settings for the instance. |
40 | | -// */ |
41 | | -// constructor(private readonly settings: { |
42 | | -// nlpManager?: NlpManager; |
43 | | -// container?: any; |
44 | | -// nerThreshold?: number; |
45 | | -// threshold?: number; |
46 | | -// conversationContext?: MemoryConversationContext; |
47 | | -// }) { |
48 | | -// this.nlpManager = |
49 | | -// this.settings.nlpManager || |
50 | | -// new NlpManager({ |
51 | | -// container: this.settings.container, |
52 | | -// ner: { threshold: this.settings.nerThreshold || 1 }, |
53 | | -// }); |
54 | | -// this.threshold = this.settings.threshold || 0.7; |
55 | | -// this.conversationContext = |
56 | | -// this.settings.conversationContext || new MemoryConversationContext({}); |
57 | | -// } |
58 | | -// |
59 | | -// /** |
60 | | -// * Train the NLP manager. |
61 | | -// */ |
62 | | -// public async train(): Promise<void> { |
63 | | -// await this.nlpManager.train(); |
64 | | -// } |
65 | | -// |
66 | | -// /** |
67 | | -// * Loads the model from a file. |
68 | | -// * @param {String} filename Name of the file. |
69 | | -// */ |
70 | | -// public load(filename: string): void { |
71 | | -// this.nlpManager.load(filename); |
72 | | -// } |
73 | | -// |
74 | | -// /** |
75 | | -// * Saves the model into a file. |
76 | | -// * @param {String} filename Name of the file. |
77 | | -// */ |
78 | | -// public save(filename: string): void { |
79 | | -// this.nlpManager.save(filename); |
80 | | -// } |
81 | | -// |
82 | | -// /** |
83 | | -// * Loads the NLP manager from an excel. |
84 | | -// * @param {String} filename Name of the file. |
85 | | -// */ |
86 | | -// public async loadExcel(filename: string): Promise<void> { |
87 | | -// this.nlpManager.loadExcel(filename); |
88 | | -// await this.train(); |
89 | | -// this.save(filename); |
90 | | -// } |
91 | | -// |
92 | | -// /** |
93 | | -// * Process an utterance using the NLP manager. This is done using a given context |
94 | | -// * as the context object. |
95 | | -// * @param {Object} srcContext Source context |
96 | | -// * @param {String} locale Locale of the utterance. |
97 | | -// * @param {String} utterance Locale of the utterance. |
98 | | -// */ |
99 | | -// public async process( |
100 | | -// srcContext: string, |
101 | | -// locale?: string, |
102 | | -// utterance?: string |
103 | | -// ): Promise<string> { |
104 | | -// const context = srcContext || {}; |
105 | | -// const response = await (locale |
106 | | -// ? this.nlpManager.process(locale, utterance, context) |
107 | | -// : this.nlpManager.process(utterance, undefined, context)); |
108 | | -// if (response.score < this.threshold || response.intent === 'None') { |
109 | | -// response.answer = undefined; |
110 | | -// return response; |
111 | | -// } |
112 | | -// for (let i = 0; i < response.entities.length; i += 1) { |
113 | | -// const entity = response.entities[i]; |
114 | | -// context[entity.entity] = entity.option; |
115 | | -// } |
116 | | -// if (response.slotFill) { |
117 | | -// context.slotFill = response.slotFill; |
118 | | -// } else { |
119 | | -// delete context.slotFill; |
120 | | -// } |
121 | | -// return response; |
122 | | -// } |
123 | | -// |
124 | | -// /** |
125 | | -// * Given an utterance and the locale, returns the recognition of the utterance. |
126 | | -// * @param {String} utterance Utterance to be recognized. |
127 | | -// * @param {String} model Model of the utterance. |
128 | | -// * @param {Function} cb Callback Function. |
129 | | -// */ |
130 | | -// public async recognizeUtterance(utterance: string, model: {locale: string}, cb: Function): Promise<any> { |
131 | | -// const response = await this.process( |
132 | | -// model, |
133 | | -// model ? model.locale : undefined, |
134 | | -// utterance |
135 | | -// ); |
136 | | -// return cb(null, response); |
137 | | -// } |
138 | | -// |
139 | | -// |
140 | | -// } |
| 1 | +/* |
| 2 | + * Copyright (c) AXA Group Operations Spain S.A. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | + * a copy of this software and associated documentation files (the |
| 6 | + * "Software"), to deal in the Software without restriction, including |
| 7 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | + * permit persons to whom the Software is furnished to do so, subject to |
| 10 | + * the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 19 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | + |
| 25 | +import { NlpManager } from '../nlp'; |
| 26 | +import MemoryConversationContext from './memory-conversation-context'; |
| 27 | + |
| 28 | +/** |
| 29 | + * Microsoft Bot Framework compatible recognizer for nlp.js. |
| 30 | + */ |
| 31 | +class Recognizer { |
| 32 | + private readonly nlpManager: NlpManager; |
| 33 | + private readonly threshold: number; |
| 34 | + private readonly conversationContext: MemoryConversationContext; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructor of the class. |
| 38 | + * @param {Object} settings Settings for the instance. |
| 39 | + */ |
| 40 | + constructor(private readonly settings: { |
| 41 | + nlpManager?: NlpManager; |
| 42 | + container?: any; |
| 43 | + nerThreshold?: number; |
| 44 | + threshold?: number; |
| 45 | + conversationContext?: MemoryConversationContext; |
| 46 | + }) { |
| 47 | + this.nlpManager = |
| 48 | + this.settings.nlpManager || |
| 49 | + new NlpManager({ |
| 50 | + container: this.settings.container, |
| 51 | + ner: { threshold: this.settings.nerThreshold || 1 }, |
| 52 | + }); |
| 53 | + this.threshold = this.settings.threshold || 0.7; |
| 54 | + this.conversationContext = |
| 55 | + this.settings.conversationContext || new MemoryConversationContext({}); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Train the NLP manager. |
| 60 | + */ |
| 61 | + public async train(): Promise<void> { |
| 62 | + await this.nlpManager.train(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Loads the model from a file. |
| 67 | + * @param {String} filename Name of the file. |
| 68 | + */ |
| 69 | + public load(filename: string): void { |
| 70 | + this.nlpManager.load(filename); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Saves the model into a file. |
| 75 | + * @param {String} filename Name of the file. |
| 76 | + */ |
| 77 | + public save(filename: string): void { |
| 78 | + this.nlpManager.save(filename); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Loads the NLP manager from an excel. |
| 83 | + * @param {String} filename Name of the file. |
| 84 | + */ |
| 85 | + public async loadExcel(filename: string): Promise<void> { |
| 86 | + this.nlpManager.loadExcel(filename); |
| 87 | + await this.train(); |
| 88 | + this.save(filename); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Process an utterance using the NLP manager. This is done using a given context |
| 93 | + * as the context object. |
| 94 | + * @param {Object} srcContext Source context |
| 95 | + * @param {String} locale Locale of the utterance. |
| 96 | + * @param {String} utterance Locale of the utterance. |
| 97 | + */ |
| 98 | + public async process( |
| 99 | + srcContext: Record<string, unknown>, |
| 100 | + locale?: string, |
| 101 | + utterance?: string |
| 102 | + ): Promise<string> { |
| 103 | + const context = srcContext || {}; |
| 104 | + const response = await (locale |
| 105 | + ? this.nlpManager.process(locale, utterance, context) |
| 106 | + : this.nlpManager.process(utterance, undefined, context)); |
| 107 | + if (response.score < this.threshold || response.intent === 'None') { |
| 108 | + response.answer = undefined; |
| 109 | + return response; |
| 110 | + } |
| 111 | + for (let i = 0; i < response.entities.length; i += 1) { |
| 112 | + const entity = response.entities[i]; |
| 113 | + context[entity.entity] = entity.option; |
| 114 | + } |
| 115 | + if (response.slotFill) { |
| 116 | + context.slotFill = response.slotFill; |
| 117 | + } else { |
| 118 | + delete context.slotFill; |
| 119 | + } |
| 120 | + return response; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Given an utterance and the locale, returns the recognition of the utterance. |
| 125 | + * @param {String} utterance Utterance to be recognized. |
| 126 | + * @param {String} model Model of the utterance. |
| 127 | + * @param {Function} cb Callback Function. |
| 128 | + */ |
| 129 | + public async recognizeUtterance(utterance: string, model: {locale: string}, cb: Function): Promise<any> { |
| 130 | + const response = await this.process( |
| 131 | + model, |
| 132 | + model ? model.locale : undefined, |
| 133 | + utterance |
| 134 | + ); |
| 135 | + return cb(null, response); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +export default Recognizer; |
0 commit comments