Skip to content

Commit 74e4c01

Browse files
author
Léo Guillaume
committed
feat: nlu
1 parent b124ba6 commit 74e4c01

File tree

9 files changed

+196
-7
lines changed

9 files changed

+196
-7
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ jobs:
5454
run: npm publish --access public
5555
env:
5656
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
57+
- name: Remove dist folder 🗑️
58+
run: rm -rf dist

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
ConversationContext,
3434
MemoryConversationContext,
3535
} from './recognizer'
36+
import { BrainNLU } from './nlu'
3637

3738
export {
3839
Language,
@@ -54,5 +55,5 @@ export {
5455
Recognizer,
5556
ConversationContext,
5657
MemoryConversationContext,
57-
// BrainNLU,
58+
BrainNLU,
5859
};

src/nlp/nlp-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BuiltinMicrosoft } from '@nlpjs/builtin-microsoft';
33
import { BuiltinDuckling } from '@nlpjs/builtin-duckling';
44
import { containerBootstrap } from '@nlpjs/core-loader';
55
import Language from '@nlpjs/language';
6-
import LangAll from '@nlpjs/lang-all';
6+
import { LangAll } from '@nlpjs/lang-all';
77
import { Nlp } from '@nlpjs/nlp';
88
import { Evaluator, Template } from '@nlpjs/evaluator';
99
import { fs as requestfs } from '@nlpjs/request';

src/nlp/nlp-util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
defaultContainer,
2828
containerBootstrap,
2929
} from '@nlpjs/core-loader';
30-
import LangAll from '@nlpjs/lang-all';
30+
import { LangAll } from '@nlpjs/lang-all';
3131

3232
interface Cultures {
3333
[key: string]: string;

src/nlu/brain-nlu.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 { containerBootstrap } from '@nlpjs/core-loader';
25+
import { LangAll } from '@nlpjs/lang-all';
26+
import { NluNeural } from '@nlpjs/nlu';
27+
28+
class BrainNLU {
29+
private container: any;
30+
private nlu: NluNeural | undefined;
31+
private readonly corpus: any[];
32+
private readonly settings: any;
33+
34+
constructor(settings: any = {}) {
35+
this.settings = settings;
36+
if (!this.settings.container) {
37+
this.settings.container = containerBootstrap();
38+
}
39+
this.container = this.settings.container;
40+
this.container.use(LangAll);
41+
if (!this.settings.l) {
42+
this.nlu = new NluNeural({
43+
locale: this.settings.locale || this.settings.language || 'en',
44+
});
45+
}
46+
this.corpus = [];
47+
}
48+
49+
add(utterance: string, intent: string) {
50+
this.corpus.push({ utterance, intent });
51+
}
52+
53+
train() {
54+
return this.nlu?.train(this.corpus, this.settings);
55+
}
56+
57+
async getClassifications(utterance: string) {
58+
const result = await this.nlu?.process(utterance);
59+
return result?.classifications.sort((a, b) => b.score - a.score);
60+
}
61+
62+
async getBestClassification(utterance: string) {
63+
const result = await this.getClassifications(utterance);
64+
return result?.[0];
65+
}
66+
}
67+
68+
export default BrainNLU;

src/nlu/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import BrainNLU from './brain-nlu'
2+
3+
export {
4+
BrainNLU,
5+
};

src/sentiment/sentiment-analyzer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323

2424
import { SentimentAnalyzer as SentimentAnalyzerBase } from '@nlpjs/sentiment';
25-
import LangAll from '@nlpjs/lang-all';
25+
import { LangAll } from '@nlpjs/lang-all';
2626
import { Nlu } from '@nlpjs/nlu';
2727
import { Container } from '@nlpjs/core'
2828

src/types/@nlpjs/lang-all.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ declare module "@nlpjs/lang-all" {
2828

2929

3030
const LangAll: LangAll;
31-
export = LangAll;
31+
export { LangAll };
3232
}

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

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,119 @@
11
declare module '@nlpjs/nlu' {
2+
import { EventEmitter } from 'events';
3+
import { Container } from '@nlpjs/core';
24

3-
export class Nlu {
4-
constructor()
5+
export interface INluOptions {
6+
container?: Container;
7+
containerName?: string;
8+
autoSave?: boolean;
9+
autoLoad?: boolean;
10+
persist?: boolean;
11+
persistFilename?: string;
12+
persistDir?: string;
13+
persistInterval?: number;
14+
persistStateFilename?: string;
15+
persistStateDir?: string;
16+
persistStateInterval?: number;
17+
log?: boolean;
18+
minSamplesPerIntent?: number;
19+
trainByDomain?: boolean;
20+
languages?: string[];
21+
locale?: string;
522
}
23+
24+
export interface INluClassifyPayload {
25+
utterance: string;
26+
locale?: string;
27+
language?: string;
28+
domain?: string;
29+
timezone?: string;
30+
userId?: string;
31+
sessionId?: string;
32+
additional?: any;
33+
}
34+
35+
export interface INluModel {
36+
lang?: string;
37+
lastUpdate?: string;
38+
minSamplesPerIntent?: number;
39+
trainByDomain?: boolean;
40+
intentThresholds?: { [name: string]: number };
41+
entitiesThresholds?: { [name: string]: number };
42+
utterances?: { [locale: string]: { [intent: string]: string[] } };
43+
domains?: { [name: string]: { [locale: string]: { [intent: string]: string[] } } };
44+
entities?: {
45+
[locale: string]: {
46+
[name: string]: {
47+
type: string;
48+
values: { [value: string]: any };
49+
};
50+
};
51+
};
52+
regex?: { [name: string]: { [locale: string]: string } };
53+
stems?: { [locale: string]: { [value: string]: string } };
54+
}
55+
56+
export class Nlu extends EventEmitter {
57+
constructor();
58+
59+
container: Container;
60+
model?: INluModel;
61+
containerName?: string;
62+
locale: string;
63+
languages: string[];
64+
settings: INluOptions;
65+
stopWords: Set<string>;
66+
stemmers: {
67+
[locale: string]: (str: string) => string;
68+
};
69+
classifiers: {
70+
[locale: string]: {
71+
[name: string]: any;
72+
};
73+
};
74+
75+
load(): Promise<void>;
76+
77+
process(payload: INluClassifyPayload): Promise<INluModel>;
78+
79+
train(): Promise<void>;
80+
81+
save(): Promise<void>;
82+
83+
export(): INluModel;
84+
}
85+
86+
export interface NluNeuralSettings {
87+
locale?: string;
88+
log?: boolean;
89+
useNoneFeature?: boolean;
90+
noneValue?: number;
91+
useNeural?: boolean;
92+
stemming?: boolean;
93+
useRegExpTokenize?: boolean;
94+
useLemma?: boolean;
95+
minScore?: number;
96+
ner?: any;
97+
skipStopWords?: boolean;
98+
pipeline?: any;
99+
}
100+
101+
export class NluNeural {
102+
constructor(settings?: NluNeuralSettings);
103+
settings: NluNeuralSettings;
104+
train(corpus: any[], settings?: NluNeuralSettings): Promise<void>;
105+
process(utterance: string, context?: any): Promise<{
106+
classifications: Array<{
107+
intent: string;
108+
score: number;
109+
}>;
110+
}>;
111+
}
112+
113+
export class NluNeuralManager {
114+
constructor(settings?: NluNeuralSettings);
115+
nlu: NluNeural;
116+
container: any;
117+
}
118+
export function register(container: any, options?: NluNeuralSettings): void;
6119
}

0 commit comments

Comments
 (0)