fix(kb.gbapp): #276 use of NLP.js upgrade to v4.

This commit is contained in:
rodrigorodriguez 2023-02-27 19:17:52 -03:00
parent 5c48d39620
commit ec1c38f378
4 changed files with 45 additions and 17 deletions

View file

@ -223,7 +223,12 @@ export class GBVMService extends GBService {
let httpPs = this.httpPs; let httpPs = this.httpPs;
let page = null; let page = null;
for(i in this.variables) {
global[i] = this.variables[i];
}
debugger;
// Local functions. // Local functions.
const ubound = (array) => {return array.length}; const ubound = (array) => {return array.length};
@ -345,13 +350,14 @@ export class GBVMService extends GBService {
// Auto-NLP generates BASIC variables related to entities. // Auto-NLP generates BASIC variables related to entities.
if (text && min['nerEngine']) { let variables = [];
const result = await min['nerEngine'].process(text); if (step ? step.context.activity.originalText : null && min['nerEngine']) {
const result = await min['nerEngine'].process(step.context.activity.originalText);
for (let i = 0; i < result.entities.length; i++) { for (let i = 0; i < result.entities.length; i++) {
const v = result.entities[i]; const v = result.entities[i];
const variableName = `${v.entity}`; const variableName = `${v.entity}`;
sandbox[variableName] = v.option; variables[variableName] = v.option ? v.option : v.sourceText;
} }
} }
@ -368,6 +374,7 @@ export class GBVMService extends GBService {
instanceId: min.instance.instanceId instanceId: min.instance.instanceId
}; };
sandbox['variables'] = variables;
sandbox['id'] = dk.sys().getRandomId(); sandbox['id'] = dk.sys().getRandomId();
sandbox['username'] = await dk.userName({ pid }); sandbox['username'] = await dk.userName({ pid });
sandbox['mobile'] = await dk.userMobile({ pid }); sandbox['mobile'] = await dk.userMobile({ pid });

View file

@ -720,6 +720,8 @@ export class GBDeployer implements IGBDeployer {
GBLogEx.info(instance.instanceId, `Acquiring rebuildIndex mutex...`); GBLogEx.info(instance.instanceId, `Acquiring rebuildIndex mutex...`);
release = await GBServer.globals.indexSemaphore.acquire(); release = await GBServer.globals.indexSemaphore.acquire();
GBLogEx.info(instance.instanceId, `Acquire rebuildIndex done.`); GBLogEx.info(instance.instanceId, `Acquire rebuildIndex done.`);
// Prepares search.
const search = new AzureSearch( const search = new AzureSearch(
instance.searchKey, instance.searchKey,
instance.searchHost, instance.searchHost,
@ -728,12 +730,15 @@ export class GBDeployer implements IGBDeployer {
); );
const connectionString = GBDeployer.getConnectionStringFromInstance(instance); const connectionString = GBDeployer.getConnectionStringFromInstance(instance);
const dsName = 'gb'; const dsName = 'gb';
// Removes any previous index.
try { try {
await search.createDataSource(dsName, dsName, 'GuaribasQuestion', 'azuresql', connectionString); await search.deleteDataSource(dsName);
} catch (err) { } catch (err) {
// If it is a 404 there is nothing to delete as it is the first creation. // If it is a 404 there is nothing to delete as it is the first creation.
if (err.code !== 400 && err.message.indexOf('already exists') !==-1) { if (err.code !== 404) {
throw err; throw err;
} }
} }
@ -741,16 +746,25 @@ export class GBDeployer implements IGBDeployer {
// Removes the index. // Removes the index.
try { try {
await search.createIndex(searchSchema, dsName); await search.deleteIndex();
} catch (err) { } catch (err) {
// If it is a 404 there is nothing to delete as it is the first creation. // If it is a 404 there is nothing to delete as it is the first creation.
if (err.code !== 'ResourceNameAlreadyInUse') { if (err.code !== 404 && err.code !== 'OperationNotAllowed') {
throw err; throw err;
} }
} }
await search.rebuildIndex(instance.searchIndexer); // Creates the data source and index on the cloud.
try {
await search.createDataSource(dsName, dsName, 'GuaribasQuestion', 'azuresql', connectionString);
} catch (err) {
GBLog.error(err);
throw err;
}
await search.createIndex(searchSchema, dsName);
release(); release();
GBLogEx.info(instance.instanceId, `Released rebuildIndex mutex.`); GBLogEx.info(instance.instanceId, `Released rebuildIndex mutex.`);
} catch { } catch {

View file

@ -667,7 +667,7 @@ export class GBMinService {
// NLP Manager. // NLP Manager.
const manager = new NlpManager({ languages: ['en'], forceNER: true }); const manager = new NlpManager({ languages: ['pt'], forceNER: true });
min['nerEngine'] = manager; min['nerEngine'] = manager;
if (GBServer.globals.minBoot === undefined) { if (GBServer.globals.minBoot === undefined) {

View file

@ -866,7 +866,7 @@ export class KBService implements IGBKBService {
let menu; let menu;
// Detect menu level by skipping blank cells on left. // Detect menu level by skipping blank cells on left.
let level; let level;
for (level = 0; level < MAX_LEVEL; level++) { for (level = 0; level < MAX_LEVEL; level++) {
const cell = row._cells[level]; const cell = row._cells[level];
@ -876,9 +876,9 @@ export class KBService implements IGBKBService {
} }
} }
// Tree hierarchy calculation. // Tree hierarchy calculation.
if (level > lastLevel) { if (level > lastLevel) {
childrenNode = activeObj.children; childrenNode = activeObj.children;
} else if (level < lastLevel) { } else if (level < lastLevel) {
childrenNode = activeChildrenGivenLevel[level]; childrenNode = activeChildrenGivenLevel[level];
@ -891,7 +891,7 @@ export class KBService implements IGBKBService {
activeChildrenGivenLevel[level] = childrenNode; activeChildrenGivenLevel[level] = childrenNode;
// Insert the object into JSON. // Insert the object into JSON.
const description = row._cells[level + 1]?row._cells[level + 1].text: null; const description = row._cells[level + 1] ? row._cells[level + 1].text : null;
activeObj = { activeObj = {
title: menu, title: menu,
description: description, description: description,
@ -959,10 +959,17 @@ export class KBService implements IGBKBService {
const categoryReg = /.*\((.*)\).*/gi.exec(text); const categoryReg = /.*\((.*)\).*/gi.exec(text);
const nameReg = /(\w+)\(.*\).*/gi.exec(text); const nameReg = /(\w+)\(.*\).*/gi.exec(text);
if (categoryReg && nameReg) { if (categoryReg) {
let category = categoryReg[1]; let category = categoryReg[1];
let name = nameReg[1];
min['nerEngine'].addNamedEntityText(category, name, [contentLocale], [name]); if (category === 'number') {
min['nerEngine'].addRegexEntity('number','pt', '/d+/gi');
}
if (nameReg) {
let name = nameReg[1];
min['nerEngine'].addNamedEntityText(category, name, [contentLocale], [name]);
}
} }
}); });
} }