Skip to content

Commit b0b7897

Browse files
author
Léo Guillaume
committed
feat: recognizer class
1 parent 5bac8db commit b0b7897

File tree

4 files changed

+157
-157
lines changed

4 files changed

+157
-157
lines changed

src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import { removeEmojis, Evaluator, SpellCheck, Handlebars } from './util'
2828
import { ActionManager, NlgManager } from './nlg'
2929
import { NeuralNetwork } from './classifiers'
3030
import { SentimentAnalyzer, SentimentManager } from './sentiment'
31-
// import {
32-
// Recognizer,
33-
// ConversationContext,
34-
// MemoryConversationContext,
35-
// } from './recognizer'
31+
import {
32+
Recognizer,
33+
ConversationContext,
34+
MemoryConversationContext,
35+
} from './recognizer'
3636

3737
export {
3838
Language,
@@ -51,8 +51,8 @@ export {
5151
NeuralNetwork,
5252
SentimentAnalyzer,
5353
SentimentManager,
54-
// Recognizer,
55-
// ConversationContext,
56-
// MemoryConversationContext,
54+
Recognizer,
55+
ConversationContext,
56+
MemoryConversationContext,
5757
// BrainNLU,
5858
};

src/nlp/nlp-manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface NlpManagerSettings {
2121
useDuckling?: boolean
2222
ducklingUrl?: string
2323
locale?: string
24+
threshold?: number
2425
}
2526
action?: {
2627
[key: string]: (params: any, context: any, result: any) => Promise<void> | void

src/recognizer/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
2323

24-
// const Recognizer = require('./recognizer');
25-
// const ConversationContext = require('./conversation-context');
26-
// const MemoryConversationContext = require('./memory-conversation-context');
27-
//
28-
// module.exports = {
29-
// Recognizer,
30-
// ConversationContext,
31-
// MemoryConversationContext,
32-
// };
24+
import Recognizer from './recognizer';
25+
import ConversationContext from './conversation-context'
26+
import MemoryConversationContext from './memory-conversation-context'
27+
28+
export {
29+
Recognizer,
30+
ConversationContext,
31+
MemoryConversationContext,
32+
};

src/recognizer/recognizer.ts

Lines changed: 139 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,139 @@
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

Comments
 (0)