UNPKG

umbot

Version:

Universal bot(vk, telegram, viber) or skills for Yandex.Alisa, Маруся and sber

121 lines (120 loc) 3.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Text = void 0; const util_1 = require("./util"); class Text { static regexCache = new Map(); static resize(text, size = 950, isEllipsis = true) { if (!text) { return ''; } if (text.length <= size) { return text; } if (!isEllipsis) { return text.substring(0, size); } const ellipsisSize = Math.max(0, size - 3); return text.substring(0, ellipsisSize) + '...'; } static isUrl(link) { const URL_PATTERN = /((http|s:\/\/)[^( |\n)]+)/gimu; return URL_PATTERN.test(link); } static isSayTrue(text) { if (!text) { return false; } const CONFIRM_PATTERNS = [ '(?:^|\\s)да(?:^|\\s|$)', '(?:^|\\s)конечно(?:^|\\s|$)', '(?:^|\\s)соглас[^s]+(?:^|\\s|$)', '(?:^|\\s)подтвер[^s]+(?:^|\\s|$)', ]; return Text.isSayPattern(CONFIRM_PATTERNS, text); } static isSayFalse(text) { if (!text) { return false; } const REJECT_PATTERNS = [ '(?:^|\\s)нет(?:^|\\s|$)', '(?:^|\\s)неа(?:^|\\s|$)', '(?:^|\\s)не(?:^|\\s|$)', ]; return Text.isSayPattern(REJECT_PATTERNS, text); } static isSayPattern(patterns, text) { if (!text) { return false; } const pattern = Array.isArray(patterns) ? `(${patterns.join(')|(')})` : patterns; const cachedRegex = Text.getCachedRegex(pattern); return !!text.match(cachedRegex); } static isSayText(find, text, isPattern = false) { if (!text) { return false; } if (!isPattern) { return Array.isArray(find) ? find.some((value) => text.includes(value)) : text === find || text.includes(find); } return Text.isSayPattern(find, text); } static getCachedRegex(pattern) { let regex = Text.regexCache.get(pattern); if (!regex) { regex = new RegExp(pattern, 'umig'); Text.regexCache.set(pattern, regex); } return regex; } static getText(str) { return Array.isArray(str) ? str[(0, util_1.rand)(0, str.length - 1)] : str; } static getEnding(num, titles, index = null) { if (index !== null && titles[index] !== undefined) { return titles[index]; } const absNum = Math.abs(num); const cases = [2, 0, 1, 1, 1, 2]; const titleIndex = absNum % 100 > 4 && absNum % 100 < 20 ? 2 : cases[Math.min(absNum % 10, 5)]; return titles[titleIndex] || null; } static textSimilarity(origText, compareText, threshold = 80) { const texts = Array.isArray(compareText) ? compareText : [compareText]; const normalizedOrigText = origText.toLowerCase(); let maxSimilarity = { percent: 0, index: null, status: false, text: null, }; const exactMatch = texts.findIndex((t) => t.toLowerCase() === normalizedOrigText); if (exactMatch !== -1) { return { index: exactMatch, status: true, percent: 100, text: texts[exactMatch], }; } texts.forEach((currentText, index) => { const similarity = (0, util_1.similarText)(normalizedOrigText, currentText.toLowerCase()); if (similarity > maxSimilarity.percent) { maxSimilarity = { percent: similarity, index: index, status: similarity >= threshold, text: currentText, }; } }); return maxSimilarity; } } exports.Text = Text;