@nlpjs/nlu
Advanced tools
Comparing version 4.9.1 to 4.9.2
{ | ||
"name": "@nlpjs/nlu", | ||
"version": "4.9.1", | ||
"version": "4.9.2", | ||
"description": "Natural Language Understanding", | ||
@@ -28,8 +28,8 @@ "author": { | ||
"dependencies": { | ||
"@nlpjs/core": "^4.6.0", | ||
"@nlpjs/core": "^4.9.2", | ||
"@nlpjs/language-min": "^4.6.0", | ||
"@nlpjs/neural": "^4.9.1", | ||
"@nlpjs/neural": "^4.9.2", | ||
"@nlpjs/similarity": "^4.9.1" | ||
}, | ||
"gitHead": "8133ce0c2da2d1c0c11ca787db27a4116215b875" | ||
"gitHead": "956809b1d31b96962f588bff97aef9b73794af4d" | ||
} |
@@ -84,7 +84,2 @@ /* | ||
); | ||
this.container.registerPipeline( | ||
'domain-manager-??-process', | ||
['.innerClassify', 'output.classification'], | ||
false | ||
); | ||
} | ||
@@ -167,8 +162,13 @@ | ||
const input = srcInput; | ||
if (!this.cache) { | ||
this.cache = { | ||
stem: this.container.get('stem'), | ||
}; | ||
} | ||
for (let i = 0; i < this.sentences.length; i += 1) { | ||
const current = this.sentences[i]; | ||
const subInput = { ...current, ...input }; | ||
await this.runPipeline(subInput, ['stem.addForTraining']); | ||
await this.cache.stem.addForTraining(subInput); | ||
} | ||
await this.runPipeline(input, ['stem.train']); | ||
await this.cache.stem.train(input); | ||
return input; | ||
@@ -365,2 +365,7 @@ } | ||
async defaultPipelineProcess(input) { | ||
const output = await this.innerClassify(input); | ||
return output.classification; | ||
} | ||
async process(utterance, settings) { | ||
@@ -374,3 +379,6 @@ const input = | ||
: utterance; | ||
return this.runPipeline(input, this.pipelineProcess); | ||
if (this.pipelineProcess) { | ||
return this.runPipeline(input, this.pipelineProcess); | ||
} | ||
return this.defaultPipelineProcess(input); | ||
} | ||
@@ -377,0 +385,0 @@ |
@@ -70,13 +70,2 @@ /* | ||
); | ||
this.container.registerPipeline( | ||
'nlu-manager-process', | ||
[ | ||
'.fillLanguage', | ||
'.innerClassify', | ||
'.checkIfIsNone', | ||
'delete output.settings', | ||
'delete output.classification', | ||
], | ||
false | ||
); | ||
} | ||
@@ -305,2 +294,11 @@ | ||
async defaultPipelineProcess(input) { | ||
let output = await this.fillLanguage(input); | ||
output = await this.innerClassify(output); | ||
output = await this.checkIfIsNone(output); | ||
delete output.settings; | ||
delete output.classification; | ||
return output; | ||
} | ||
process(locale, utterance, domain, settings) { | ||
@@ -316,3 +314,6 @@ const input = | ||
}; | ||
return this.runPipeline(input, this.pipelineProcess); | ||
if (this.pipelineProcess) { | ||
return this.runPipeline(input, this.pipelineProcess); | ||
} | ||
return this.defaultPipelineProcess(input); | ||
} | ||
@@ -319,0 +320,0 @@ |
125
src/nlu.js
@@ -70,14 +70,2 @@ /* | ||
this.container.registerPipeline( | ||
'nlu-??-prepare', | ||
[ | ||
'normalize', | ||
'tokenize', | ||
'removeStopwords', | ||
'stem', | ||
'arrToObj', | ||
'output.tokens', | ||
], | ||
false | ||
); | ||
this.container.registerPipeline( | ||
'nlu-??-train', | ||
@@ -87,16 +75,58 @@ ['.prepareCorpus', '.addNoneFeature', '.innerTrain'], | ||
); | ||
this.container.registerPipeline( | ||
'nlu-??-process', | ||
[ | ||
'.prepare', | ||
'.doSpellCheck', | ||
'.textToFeatures', | ||
'.innerProcess', | ||
'.filterNonActivated', | ||
'.normalizeClassifications', | ||
], | ||
false | ||
); | ||
} | ||
async defaultPipelinePrepare(input) { | ||
let result; | ||
if (this.cache) { | ||
const now = new Date(); | ||
const diff = Math.abs(now.getTime() - this.cache.created) / 3600000; | ||
if (diff > 1) { | ||
this.cache.results = undefined; | ||
this.cache.created = new Date().getTime(); | ||
} | ||
} | ||
if (!this.cache) { | ||
this.cache = { | ||
created: new Date().getTime(), | ||
results: {}, | ||
normalize: this.container.get('normalize'), | ||
tokenize: this.container.get('tokenize'), | ||
removeStopwords: this.container.get('removeStopwords'), | ||
stem: this.container.get('stem'), | ||
arrToObj: this.container.get('arrToObj'), | ||
}; | ||
} else if (this.cache.results[input.settings.locale]) { | ||
result = this.cache.results[input.settings.locale][ | ||
input.text || input.utterance | ||
]; | ||
if (result) { | ||
return result; | ||
} | ||
} | ||
let output = input; | ||
output = this.cache.normalize.run(output); | ||
output = await this.cache.tokenize.run(output); | ||
output = this.cache.removeStopwords.run(output); | ||
output = await this.cache.stem.run(output); | ||
output = this.cache.arrToObj.run(output); | ||
result = output.tokens; | ||
if (!this.cache.results[input.settings.locale]) { | ||
this.cache.results[input.settings.locale] = {}; | ||
} | ||
this.cache.results[input.settings.locale][ | ||
input.text || input.utterance | ||
] = result; | ||
return result; | ||
} | ||
async defaultPipelineProcess(input) { | ||
let output = await this.prepare(input); | ||
output = await this.doSpellCheck(output); | ||
output = await this.textToFeatures(output); | ||
output = await this.innerProcess(output); | ||
output = await this.filterNonActivated(output); | ||
output = await this.normalizeClassifications(output); | ||
return output; | ||
} | ||
async prepare(text, srcSettings) { | ||
@@ -110,3 +140,6 @@ const settings = srcSettings || this.settings; | ||
}; | ||
return this.runPipeline(input, this.pipelinePrepare); | ||
if (this.pipelinePrepare) { | ||
return this.runPipeline(input, this.pipelinePrepare); | ||
} | ||
return this.defaultPipelinePrepare(input); | ||
} | ||
@@ -198,3 +231,2 @@ if (typeof text === 'object') { | ||
} | ||
this.intentFeatures[keys[i]] = Object.keys(this.intentFeatures[keys[i]]); | ||
} | ||
@@ -258,14 +290,14 @@ this.spellCheck.setFeatures(this.features); | ||
getAllowList(tokens) { | ||
const result = {}; | ||
const features = Object.keys(tokens); | ||
for (let i = 0; i < features.length; i += 1) { | ||
const intents = this.featuresToIntent[features[i]]; | ||
if (intents) { | ||
for (let j = 0; j < intents.length; j += 1) { | ||
result[intents[j]] = 1; | ||
} | ||
intentIsActivated(intent, tokens) { | ||
const features = this.intentFeatures[intent]; | ||
if (!features) { | ||
return false; | ||
} | ||
const keys = Object.keys(tokens); | ||
for (let i = 0; i < keys.length; i += 1) { | ||
if (features[keys[i]]) { | ||
return true; | ||
} | ||
} | ||
return result; | ||
return false; | ||
} | ||
@@ -275,9 +307,9 @@ | ||
if (this.intentFeatures && srcInput.classifications) { | ||
const allowList = this.getAllowList(srcInput.tokens); | ||
allowList.None = 1; | ||
const { classifications } = srcInput; | ||
for (let i = 0; i < classifications.length; i += 1) { | ||
const classification = classifications[i]; | ||
if (!allowList[classification.intent]) { | ||
classification.score = 0; | ||
const intents = srcInput.classifications.map((x) => x.intent); | ||
for (let i = 0; i < intents.length; i += 1) { | ||
const intent = intents[i]; | ||
if (intent !== 'None') { | ||
if (!this.intentIsActivated(intent, srcInput.tokens)) { | ||
srcInput.classifications[i].score = 0; | ||
} | ||
} | ||
@@ -391,3 +423,8 @@ } | ||
}; | ||
const output = await this.runPipeline(input, this.pipelineProcess); | ||
let output; | ||
if (this.pipelineProcess) { | ||
output = await this.runPipeline(input, this.pipelineProcess); | ||
} else { | ||
output = await this.defaultPipelineProcess(input); | ||
} | ||
if (Array.isArray(output.classifications)) { | ||
@@ -394,0 +431,0 @@ const explanation = input.settings.returnExplanation |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45750
1319
Updated@nlpjs/core@^4.9.2
Updated@nlpjs/neural@^4.9.2