umbot
Version:
Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber
135 lines (134 loc) • 3.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Nlu = void 0;
const Text_1 = require("../../utils/standard/Text");
class Nlu {
_nlu;
_cachedData = new Map();
static EMAIL_REGEX = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi;
static PHONE_REGEX = /([\d\-() ]{4,}\d)|((?:\+|\d)[\d\-() ]{9,}\d)/gimu;
static LINK_REGEX = /((http|s:\/\/)[^( |\n)]+)/gimu;
static T_FIO = 'YANDEX.FIO';
static T_GEO = 'YANDEX.GEO';
static T_DATETIME = 'YANDEX.DATETIME';
static T_NUMBER = 'YANDEX.NUMBER';
static T_INTENT_CONFIRM = 'YANDEX.CONFIRM';
static T_INTENT_REJECT = 'YANDEX.REJECT';
static T_INTENT_HELP = 'YANDEX.HELP';
static T_INTENT_REPEAT = 'YANDEX.REPEAT';
constructor() {
this._nlu = {};
}
_serializeNlu(nlu) {
return nlu;
}
setNlu(nlu) {
this._nlu = this._serializeNlu(nlu);
this._cachedData.clear();
}
_getData(type) {
if (this._cachedData.has(type)) {
return this._cachedData.get(type) || null;
}
let data = null;
if (this._nlu.entities) {
this._nlu.entities.forEach((entity) => {
if (typeof entity.type !== 'undefined' && entity.type === type) {
if (data === null) {
data = [];
}
data.push(entity.value);
}
});
}
this._cachedData.set(type, data);
return data;
}
getUserName() {
return this._nlu.thisUser || null;
}
getFio() {
const fio = this._getData(Nlu.T_FIO);
const status = !!fio;
return {
status,
result: fio,
};
}
getGeo() {
const geo = this._getData(Nlu.T_GEO);
const status = !!geo;
return {
status,
result: geo,
};
}
getDateTime() {
const dateTime = this._getData(Nlu.T_DATETIME);
const status = !!dateTime;
return {
status,
result: dateTime,
};
}
getNumber() {
const number = this._getData(Nlu.T_NUMBER);
const status = !!number;
return {
status,
result: number,
};
}
isIntentConfirm(userCommand = '') {
const result = this.getIntent(Nlu.T_INTENT_CONFIRM) !== null;
if (!result && userCommand) {
return Text_1.Text.isSayTrue(userCommand);
}
return result;
}
isIntentReject(userCommand = '') {
const result = this.getIntent(Nlu.T_INTENT_REJECT) !== null;
if (!result && userCommand) {
return Text_1.Text.isSayFalse(userCommand);
}
return result;
}
isIntentHelp() {
return this.getIntent(Nlu.T_INTENT_HELP) !== null;
}
isIntentRepeat() {
return this.getIntent(Nlu.T_INTENT_REPEAT) !== null;
}
getIntents() {
return this._nlu.intents || null;
}
getIntent(intentName) {
const intents = this.getIntents();
if (intents) {
return intents[intentName] || null;
}
return null;
}
static getLink(query) {
const links = query.match(Nlu.LINK_REGEX);
return {
status: !!links,
result: links,
};
}
static getPhone(query) {
const phones = query.match(Nlu.PHONE_REGEX);
return {
status: !!phones,
result: phones,
};
}
static getEMail(query) {
const emails = query.match(Nlu.EMAIL_REGEX);
return {
status: !!emails,
result: emails,
};
}
}
exports.Nlu = Nlu;