Skip to content

Commit 74ecdfe

Browse files
author
Léo Guillaume
committed
feat: add nlg
1 parent 515d919 commit 74ecdfe

File tree

5 files changed

+223
-4
lines changed

5 files changed

+223
-4
lines changed

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { Language } from './language'
2525
import { NlpUtil, NlpManager, NlpExcelReader } from './nlp'
2626
import { XTableUtils, XTable, XDoc } from './xtables'
2727
import { removeEmojis, Evaluator, SpellCheck, Handlebars } from './util'
28-
28+
import { ActionManager, NlgManager } from './nlg'
2929
export {
3030
Language,
3131
NlpUtil,
@@ -38,8 +38,8 @@ export {
3838
Evaluator,
3939
SpellCheck,
4040
Handlebars,
41-
// ActionManager,
42-
// NlgManager,
41+
ActionManager,
42+
NlgManager,
4343
// NeuralNetwork,
4444
// SentimentAnalyzer,
4545
// SentimentManager,

src/nlg/index.ts

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

src/nlg/nlg-manager.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 NlgManagerBase from '@nlpjs/nlg';
25+
import { Evaluator } from '@nlpjs/evaluator';
26+
27+
class NlgManager extends NlgManagerBase {
28+
constructor(settings: any = {}, container?: any) {
29+
super(settings, container);
30+
this.container.register('Evaluator', Evaluator, true);
31+
}
32+
33+
addAnswer(locale: string, intent: string, answer: any, opts?: any): void {
34+
return this.add(locale, intent, answer, opts);
35+
}
36+
37+
async findAnswer(locale: string, intent: string, context: any, settings?: any): Promise<{ response: any } | undefined> {
38+
const answer = await this.find(locale, intent, context, settings);
39+
if (!answer.answer) {
40+
return undefined;
41+
}
42+
return {
43+
response: answer.answer,
44+
};
45+
}
46+
47+
removeAnswer(locale: string, intent: string, answer: any, opts?: any): void {
48+
return this.remove(locale, intent, answer, opts);
49+
}
50+
51+
isValid(condition: string | undefined, context: any): boolean {
52+
const evaluator = this.container.get('Evaluator');
53+
if (evaluator) {
54+
return (
55+
!condition ||
56+
condition === '' ||
57+
evaluator.evaluate(condition, context) === true
58+
);
59+
}
60+
return true;
61+
}
62+
63+
findAllAnswers(locale?: string, intent?: string, context?: any): {answer: string, opts: string}[] | any {
64+
if (typeof locale === 'string') {
65+
const found = super.findAllAnswers(locale, intent, context);
66+
const filtered = super.filterAnswers(found);
67+
return filtered.answers.map((x: {answer: string, opts: string}) => ({
68+
response: x.answer,
69+
opts: x.opts,
70+
}));
71+
}
72+
return super.findAllAnswers(locale);
73+
}
74+
}
75+
76+
export default NlgManager;
77+

src/types/@nlpjs/core.d.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,25 @@ declare module '@nlpjs/core' {
5757

5858
export function ArrToObj<T extends Obj>(arr: T[], keyField: keyof T): { [key: string]: T };
5959

60-
export function Clonable(Base: new () => any): new () => any;
60+
export class Clonable<T = any> {
61+
constructor(settings?: any, container?: any);
62+
readonly container: any;
63+
readonly settings: T;
64+
applySettings(target: any, source: any): void;
65+
getPipeline(name: string): any[];
66+
clone(): Clonable<T>;
67+
}
6168

6269
export class Container {
6370
constructor(options?: ContainerOptions);
6471

72+
register<T>(
73+
id: string,
74+
obj: T,
75+
singleton?: boolean,
76+
force?: boolean
77+
): void;
78+
6579
addStemmer(lang: Language, stemmer: StemmerFunction): void;
6680

6781
addTokenizer(lang: Language, tokenizer: TokenizerFunction): void;

src/types/@nlpjs/nlg.d.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
declare module '@nlpjs/nlg' {
2+
import { Clonable, Container } from '@nlpjs/core';
3+
4+
interface Answer {
5+
response: string;
6+
opts?: Record<string, any>;
7+
}
8+
9+
interface FindAnswerOptions {
10+
skipIfSameInput?: boolean;
11+
ignoreCase?: boolean;
12+
ignoreStopWords?: boolean;
13+
}
14+
15+
interface NlgManagerSettings {
16+
settingsPerLocale?: Record<string, NlgManagerSettings>;
17+
useNearestLocale?: boolean;
18+
}
19+
20+
interface NlgManagerAnswer {
21+
answer: string;
22+
score: number;
23+
opts?: Record<string, any>;
24+
context?: Record<string, any>;
25+
}
26+
27+
class NlgManager {
28+
constructor(settings?: NlgManagerSettings, container?: Container);
29+
30+
add(locale: string, intent: string, answer: string, opts?: Record<string, any>): void;
31+
addAnswer(locale: string, intent: string, answer: string, opts?: Record<string, any>): void;
32+
filterAnswers(srcInput: {answer: string, opts: string}[]): {answers: {answer: string, opts: string}[]};
33+
find(locale: string, intent: string, context?: Record<string, any>, options?: FindAnswerOptions): Promise<NlgManagerAnswer>;
34+
35+
findAnswer(locale: string, intent: string, context?: Record<string, any>, options?: FindAnswerOptions): Promise<Answer | undefined>;
36+
37+
findAllAnswers(locale?: string, intent?: string, context?: Record<string, any>): Array<{answer: string, opts: string}>;
38+
39+
remove(locale: string, intent: string, answer: string, opts?: Record<string, any>): void;
40+
removeAnswer(locale: string, intent: string, answer: string, opts?: Record<string, any>): void;
41+
42+
isValid(condition: string, context?: Record<string, any>): boolean;
43+
44+
container: Container;
45+
}
46+
47+
interface ActionManagerSettings {
48+
container?: Container;
49+
tag?: string;
50+
}
51+
52+
interface ActionBundle {
53+
action: string;
54+
parameters: any[];
55+
}
56+
57+
interface ProcessedAnswer {
58+
answer?: string | object;
59+
actions?: ActionBundle[];
60+
}
61+
62+
type ActionFunction = (input: ProcessedAnswer, ...parameters: any[]) => Promise<ProcessedAnswer>;
63+
64+
class ActionManager extends Clonable {
65+
constructor(settings?: ActionManagerSettings, container?: Container);
66+
67+
actions: Record<string, ActionBundle[]>;
68+
actionsMap: Record<string, ActionFunction>;
69+
70+
registerDefault(): void;
71+
72+
posAction(intent: string, action: string, parameters: any[]): number;
73+
74+
findActions(intent: string): { action: string; parameters: any[]; fn: ActionFunction }[];
75+
76+
processActions(intent: string, input: string | object): Promise<ProcessedAnswer>;
77+
78+
addAction(intent: string, action: string, parameters: any[], fn?: ActionFunction): void;
79+
80+
removeAction(intent: string, action: string, parameters: any[]): void;
81+
82+
removeActions(intent: string): void;
83+
84+
registerActionInMap(action: string, fn: ActionFunction): void;
85+
86+
removeActionFromMap(action: string): void;
87+
88+
run(srcInput: { intent: string; }, settings?: any): Promise<ProcessedAnswer>;
89+
90+
toJSON(): { settings: ActionManagerSettings; actions: Record<string, ActionBundle[]> };
91+
92+
fromJSON(json: { settings: ActionManagerSettings; actions: Record<string, ActionBundle[]> }): void;
93+
}
94+
95+
export { ActionManagerSettings, ActionManager };
96+
97+
export default NlgManager;
98+
}

0 commit comments

Comments
 (0)