Skip to content

Commit 34ec809

Browse files
author
Léo Guillaume
committed
Merge branch 'recognizer' into main
2 parents 463dde0 + b0b7897 commit 34ec809

File tree

11 files changed

+479
-9
lines changed

11 files changed

+479
-9
lines changed

package-lock.json

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@nlpjs/request": "^4.25.0",
6060
"@nlpjs/sentiment": "^4.26.1",
6161
"@nlpjs/similarity": "^4.26.1",
62-
"@nlpjs/xtables": "^4.25.0"
62+
"@nlpjs/xtables": "^4.25.0",
63+
"node-nlp": "^4.26.1"
6364
}
6465
}

src/classifiers/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
import { NeuralNetwork } from '@nlpjs/neural'
25+
26+
export {
27+
NeuralNetwork,
28+
};

src/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ import { NlpUtil, NlpManager, NlpExcelReader } from './nlp'
2626
import { XTableUtils, XTable, XDoc } from './xtables'
2727
import { removeEmojis, Evaluator, SpellCheck, Handlebars } from './util'
2828
import { ActionManager, NlgManager } from './nlg'
29+
import { NeuralNetwork } from './classifiers'
30+
import { SentimentAnalyzer, SentimentManager } from './sentiment'
31+
import {
32+
Recognizer,
33+
ConversationContext,
34+
MemoryConversationContext,
35+
} from './recognizer'
36+
2937
export {
3038
Language,
3139
NlpUtil,
@@ -40,11 +48,11 @@ export {
4048
Handlebars,
4149
ActionManager,
4250
NlgManager,
43-
// NeuralNetwork,
44-
// SentimentAnalyzer,
45-
// SentimentManager,
46-
// Recognizer,
47-
// ConversationContext,
48-
// MemoryConversationContext,
51+
NeuralNetwork,
52+
SentimentAnalyzer,
53+
SentimentManager,
54+
Recognizer,
55+
ConversationContext,
56+
MemoryConversationContext,
4957
// BrainNLU,
5058
};

src/nlp/nlp-manager.ts

Lines changed: 2 additions & 1 deletion
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
@@ -198,7 +199,7 @@ class NlpManager {
198199
return this.nlp.classify(locale, utterance, settings);
199200
}
200201

201-
async process(locale: string, utterance: string, context?: Record<string, unknown>, settings?: Record<string, unknown>): Promise<any> {
202+
async process(locale?: string, utterance?: string, context?: Record<string, unknown>, settings?: Record<string, unknown>): Promise<any> {
202203
const result = await this.nlp.process(locale, utterance, context, settings);
203204
if (this.settings.processTransformer) {
204205
return this.settings.processTransformer(result);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
26+
import { Session } from "../types/session";
27+
28+
/**
29+
* Abstract class for a conversation context of a chatbot.
30+
* The conversation context is the responsible for storing and retrieving
31+
* the context scope variables based on the current conversation.
32+
* The getConversationContext receive the session of the chatbot, and must return
33+
* a promise with the context in the resolve.
34+
*/
35+
36+
class ConversationContext {
37+
private settings: object;
38+
39+
/**
40+
* Constructor of the class.
41+
* @param {Object} settings Settings for the instance.
42+
*/
43+
constructor(settings: object) {
44+
this.settings = settings || {};
45+
}
46+
47+
/**
48+
* Given a session instance of a chatbot, return the conversation identifier.
49+
* @param {Object} session Session instance of a message of chatbot.
50+
* @returns {String} Identifier of the conversation.
51+
*/
52+
public getConversationId(session: Session): string | undefined {
53+
if (session?.message?.address?.conversation) {
54+
return session.message.address.conversation.id;
55+
}
56+
if (session?._activity?.conversation) {
57+
return session._activity.conversation.id;
58+
}
59+
return undefined;
60+
}
61+
}
62+
63+
export default ConversationContext;
64+

src/recognizer/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
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+
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
import ConversationContext from './conversation-context';
25+
import { Session } from "../types/session";
26+
27+
/**
28+
* In memory conversation context manager.
29+
*/
30+
class MemoryConversationContext extends ConversationContext {
31+
private readonly conversationContexts: { [conversationId: string]: any };
32+
33+
/**
34+
* Constructor of the class.
35+
* @param {Object} settings Settings for the instance.
36+
*/
37+
constructor(settings: object) {
38+
super(settings);
39+
this.conversationContexts = {};
40+
}
41+
42+
/**
43+
* Gets the conversation context from the session.
44+
* @param {Object} session Chatbot session of the conversation.
45+
* @returns {Promise<Object>} Promise to resolve the conversation context.
46+
*/
47+
public getConversationContext(session: Session): Promise<Object> {
48+
return new Promise((resolve, reject) => {
49+
const conversationId = this.getConversationId(session);
50+
if (!conversationId) {
51+
return reject(new Error('No conversation id found'));
52+
}
53+
if (!this.conversationContexts[conversationId]) {
54+
this.conversationContexts[conversationId] = {};
55+
}
56+
return resolve(this.conversationContexts[conversationId]);
57+
});
58+
}
59+
60+
public setConversationContext(session: Session, context: any): Promise<void> {
61+
return new Promise((resolve, reject) => {
62+
const conversationId = this.getConversationId(session);
63+
if (!conversationId) {
64+
return reject(new Error('No conversation id found'));
65+
}
66+
this.conversationContexts[conversationId] = context;
67+
return resolve();
68+
});
69+
}
70+
}
71+
72+
export default MemoryConversationContext;
73+

0 commit comments

Comments
 (0)