Skip to content

Commit

Permalink
fix(kb.gbapp): #276 use of NLP.js upgrade to v4.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Feb 27, 2023
1 parent 5c48d39 commit ec1c38f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
15 changes: 11 additions & 4 deletions packages/basic.gblib/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,12 @@ export class GBVMService extends GBService {
let httpPs = this.httpPs;
let page = null;
for(i in this.variables) {
global[i] = this.variables[i];
}
debugger;
// Local functions.
const ubound = (array) => {return array.length};
Expand Down Expand Up @@ -345,13 +350,14 @@ export class GBVMService extends GBService {

// Auto-NLP generates BASIC variables related to entities.

if (text && min['nerEngine']) {
const result = await min['nerEngine'].process(text);
let variables = [];
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++) {
const v = result.entities[i];
const variableName = `${v.entity}`;
sandbox[variableName] = v.option;
variables[variableName] = v.option ? v.option : v.sourceText;
}
}

Expand All @@ -368,6 +374,7 @@ export class GBVMService extends GBService {
instanceId: min.instance.instanceId
};

sandbox['variables'] = variables;
sandbox['id'] = dk.sys().getRandomId();
sandbox['username'] = await dk.userName({ pid });
sandbox['mobile'] = await dk.userMobile({ pid });
Expand Down
24 changes: 19 additions & 5 deletions packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,8 @@ export class GBDeployer implements IGBDeployer {
GBLogEx.info(instance.instanceId, `Acquiring rebuildIndex mutex...`);
release = await GBServer.globals.indexSemaphore.acquire();
GBLogEx.info(instance.instanceId, `Acquire rebuildIndex done.`);
// Prepares search.

const search = new AzureSearch(
instance.searchKey,
instance.searchHost,
Expand All @@ -728,29 +730,41 @@ export class GBDeployer implements IGBDeployer {
);
const connectionString = GBDeployer.getConnectionStringFromInstance(instance);
const dsName = 'gb';

// Removes any previous index.

try {
await search.createDataSource(dsName, dsName, 'GuaribasQuestion', 'azuresql', connectionString);
await search.deleteDataSource(dsName);
} catch (err) {
// 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;
}
}

// Removes the index.

try {
await search.createIndex(searchSchema, dsName);
await search.deleteIndex();
} catch (err) {
// 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;
}
}

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();
GBLogEx.info(instance.instanceId, `Released rebuildIndex mutex.`);
} catch {
Expand Down
2 changes: 1 addition & 1 deletion packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ export class GBMinService {

// NLP Manager.

const manager = new NlpManager({ languages: ['en'], forceNER: true });
const manager = new NlpManager({ languages: ['pt'], forceNER: true });
min['nerEngine'] = manager;

if (GBServer.globals.minBoot === undefined) {
Expand Down
21 changes: 14 additions & 7 deletions packages/kb.gbapp/services/KBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ export class KBService implements IGBKBService {
let menu;

// Detect menu level by skipping blank cells on left.

let level;
for (level = 0; level < MAX_LEVEL; level++) {
const cell = row._cells[level];
Expand All @@ -876,9 +876,9 @@ export class KBService implements IGBKBService {
}
}

// Tree hierarchy calculation.
// Tree hierarchy calculation.

if (level > lastLevel) {
if (level > lastLevel) {
childrenNode = activeObj.children;
} else if (level < lastLevel) {
childrenNode = activeChildrenGivenLevel[level];
Expand All @@ -891,7 +891,7 @@ export class KBService implements IGBKBService {
activeChildrenGivenLevel[level] = childrenNode;

// 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 = {
title: menu,
description: description,
Expand Down Expand Up @@ -959,10 +959,17 @@ export class KBService implements IGBKBService {
const categoryReg = /.*\((.*)\).*/gi.exec(text);
const nameReg = /(\w+)\(.*\).*/gi.exec(text);

if (categoryReg && nameReg) {
if (categoryReg) {
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]);
}
}
});
}
Expand Down

0 comments on commit ec1c38f

Please sign in to comment.