diff --git a/lib/date-cases.js b/lib/date-cases.js index f76e683..476a33d 100644 --- a/lib/date-cases.js +++ b/lib/date-cases.js @@ -140,15 +140,15 @@ class ParsedTime { isOffset; /**@type {Boolean} */ isFixed; - /** - * @param {DateTypes} dateType - * @param {TimeTypes} timeType - * @param {Number} number - * @param {Array.} indexes - * @param {Number} context - * @param {Number} prevalence + /** + * @param {DateTypes} dateType + * @param {TimeTypes} timeType + * @param {Number} number + * @param {Array.} indexes + * @param {Number} context + * @param {Number} prevalence * @param {{}} options - */ + */ constructor(dateType, timeType, number, indexes, context, prevalence, options) { this.dateType = dateType; this.timeType = timeType; @@ -172,10 +172,10 @@ class ParseCase { prevalence; /**@type {Function} */ parseFunction; - /** - * @param {Number} prevalence - * @param {Function} parseFunction - */ + /** + * @param {Number} prevalence + * @param {Function} parseFunction + */ constructor(prevalence, parseFunction) { this.prevalence = prevalence; this.parseFunction = parseFunction; @@ -187,10 +187,10 @@ class Context { start; /**@type {Number} */ end; - /** - * @param {Number} start - * @param {Number} end - */ + /** + * @param {Number} start + * @param {Number} end + */ constructor(start, end) { this.start = start; this.end = end; @@ -300,7 +300,7 @@ function processContexts(contextsData, indexes) { const numberAndWordPrevalence = 50; const numberAndWordParseCases = [ - new ParseCase(numberAndWordPrevalence, function findSeconds(parsedTimes, contextsData, prevalence) { + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { let matches = [...this.regchars.matchAll(/ns/g)]; for (const match of matches) { let num = +this.expressions[match.index].text; @@ -315,7 +315,7 @@ const numberAndWordParseCases = [ } return parsedTimes; }), - new ParseCase(numberAndWordPrevalence, function findMinutes(parsedTimes, contextsData, prevalence) { + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { let matches = [...this.regchars.matchAll(/nm/g)]; for (const match of matches) { let num = +this.expressions[match.index].text; @@ -330,7 +330,7 @@ const numberAndWordParseCases = [ } return parsedTimes; }), - new ParseCase(numberAndWordPrevalence, function findHours(parsedTimes, contextsData, prevalence) { + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { let matches = [...this.regchars.matchAll(/nh/g)]; for (const match of matches) { let num = +this.expressions[match.index].text; @@ -345,7 +345,7 @@ const numberAndWordParseCases = [ } return parsedTimes; }), - new ParseCase(numberAndWordPrevalence, function findDates(parsedTimes, contextsData, prevalence) { + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { let matches = [...this.regchars.matchAll(/n[dS]/g)]; for (const match of matches) { let num = +this.expressions[match.index].text; @@ -360,7 +360,7 @@ const numberAndWordParseCases = [ } return parsedTimes; }), - new ParseCase(numberAndWordPrevalence, function findWeeks(parsedTimes, contextsData, prevalence) { + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { let matches = [...this.regchars.matchAll(/nw/g)]; for (const match of matches) { let num = +this.expressions[match.index].text * 7; @@ -375,7 +375,7 @@ const numberAndWordParseCases = [ } return parsedTimes; }), - new ParseCase(numberAndWordPrevalence, function findMonths(parsedTimes, contextsData, prevalence) { + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { let matches = [...this.regchars.matchAll(/nQ{0,1}M/g)]; for (const match of matches) { let num = +this.expressions[match.index].text; @@ -391,7 +391,7 @@ const numberAndWordParseCases = [ } return parsedTimes; }), - new ParseCase(numberAndWordPrevalence, function findPureMonths(parsedTimes, contextsData, prevalence) { + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { let matches = [...this.regchars.matchAll(/nK/g)]; for (const match of matches) { let num = +this.expressions[match.index].text; @@ -403,7 +403,7 @@ const numberAndWordParseCases = [ } return parsedTimes; }), - new ParseCase(numberAndWordPrevalence, function findYears(parsedTimes, contextsData, prevalence) { + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { let matches = [...this.regchars.matchAll(/[nN]y/g)]; for (const match of matches) { let num = +this.expressions[match.index].text; @@ -480,6 +480,102 @@ const numberAndWordParseCases = [ parsedTimes.push(new ParsedTime(DateTypes.target, TimeTypes.dates, 1, indexes, context, prevalence, { validMode: ValidModes.notValid })); } return parsedTimes; + }), + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { + let matches = [...this.regchars.matchAll(/0/g)]; + for (const match of matches) { + let text = this.expressions[match.index].text + let num = +text.substring(0, text.length - 1); + let max = this.expressions[match.index].maximum; + let validMode = ValidModes.notCertified; + if (num >= max && max != 0) { + validMode = ValidModes.notValid; + } + let indexes = markIndexes.call(this, match.index, match[0].length, true, false); + let context = processContexts(contextsData, indexes); + parsedTimes.push(new ParsedTime(DateTypes.target, TimeTypes.seconds, num, indexes, context, prevalence, { validMode })); + } + return parsedTimes; + }), + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { + let matches = [...this.regchars.matchAll(/1/g)]; + for (const match of matches) { + let text = this.expressions[match.index].text + let num = +text.substring(0, text.length - 1); + let max = this.expressions[match.index].maximum; + let validMode = ValidModes.notCertified; + if (num >= max && max != 0) { + validMode = ValidModes.notValid; + } + let indexes = markIndexes.call(this, match.index, match[0].length, true, false); + let context = processContexts(contextsData, indexes); + parsedTimes.push(new ParsedTime(DateTypes.target, TimeTypes.minutes, num, indexes, context, prevalence, { validMode })); + } + return parsedTimes; + }), + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { + let matches = [...this.regchars.matchAll(/2/g)]; + for (const match of matches) { + let text = this.expressions[match.index].text + let num = +text.substring(0, text.length - 1); + let max = this.expressions[match.index].maximum; + let validMode = ValidModes.notCertified; + if (num >= max && max != 0) { + validMode = ValidModes.notValid; + } + let indexes = markIndexes.call(this, match.index, match[0].length, true, false); + let context = processContexts(contextsData, indexes); + parsedTimes.push(new ParsedTime(DateTypes.target, TimeTypes.hours, num, indexes, context, prevalence, { validMode })); + } + return parsedTimes; + }), + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { + let matches = [...this.regchars.matchAll(/3/g)]; + for (const match of matches) { + let text = this.expressions[match.index].text + let num = +text.substring(0, text.length - 1); + let max = this.expressions[match.index].maximum; + let validMode = ValidModes.notCertified; + if (num >= max && max != 0) { + validMode = ValidModes.notValid; + } + let indexes = markIndexes.call(this, match.index, match[0].length, true, false); + let context = processContexts(contextsData, indexes); + parsedTimes.push(new ParsedTime(DateTypes.target, TimeTypes.dates, num, indexes, context, prevalence, { validMode })); + } + return parsedTimes; + }), + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { + let matches = [...this.regchars.matchAll(/4/g)]; + for (const match of matches) { + let text = this.expressions[match.index].text + let num = +text.substring(0, text.length - 1); + let max = this.expressions[match.index].maximum; + let validMode = ValidModes.notCertified; + if (num >= max && max != 0) { + validMode = ValidModes.notValid; + } + let indexes = markIndexes.call(this, match.index, match[0].length, true, false); + let context = processContexts(contextsData, indexes); + parsedTimes.push(new ParsedTime(DateTypes.target, TimeTypes.months, num, indexes, context, prevalence, { validMode })); + } + return parsedTimes; + }), + new ParseCase(numberAndWordPrevalence, function (parsedTimes, contextsData, prevalence) { + let matches = [...this.regchars.matchAll(/5/g)]; + for (const match of matches) { + let text = this.expressions[match.index].text + let num = +text.substring(0, text.length - 1); + let max = this.expressions[match.index].maximum; + let validMode = ValidModes.notCertified; + if (num >= max && max != 0) { + validMode = ValidModes.notValid; + } + let indexes = markIndexes.call(this, match.index, match[0].length, true, false); + let context = processContexts(contextsData, indexes); + parsedTimes.push(new ParsedTime(DateTypes.target, TimeTypes.years, num, indexes, context, prevalence, { validMode })); + } + return parsedTimes; }) ]; @@ -834,7 +930,7 @@ function findParsedTimesByIndex(parsedTimes, index) { * @returns {Array.} */ function findAllMatchingParsedTimes(allowedTypes, step, start, expressions, parsedTimes, ignoreValidMode) { - if(typeof(ignoreValidMode) == 'undefined') { + if (typeof (ignoreValidMode) == 'undefined') { ignoreValidMode = ValidModes.certified; } let i = start + step; diff --git a/lib/expressions/EN.csv b/lib/expressions/EN.csv index f08eb1c..6415b91 100644 --- a/lib/expressions/EN.csv +++ b/lib/expressions/EN.csv @@ -47,4 +47,10 @@ from;V;0;0;0;0 every;E;0;0;0;0 past;L;0;0;0;0 of;Q;0;0;0;0 -until;Z;1;0;0;0 \ No newline at end of file +until;Z;1;0;0;0 +^[0-9]{1,2}[sS]$;0;0;0;60;1 +^[0-9]{1,2}m$;1;0;0;60;1 +^[0-9]{1,2}[hH]$;2;0;0;24;1 +^[0-9]{1,2}[dD]$;3;0;0;0;1 +^[0-9]{1,2}M$;4;0;0;12;1 +^[0-9]{1,2}[yY]$;5;0;0;0;1 \ No newline at end of file diff --git a/lib/expressions/RU.csv b/lib/expressions/RU.csv index c2a05b9..ef7e868 100644 --- a/lib/expressions/RU.csv +++ b/lib/expressions/RU.csv @@ -67,4 +67,10 @@ text;regex char;errors limit;value;maximum;is_regex до;Z;0;0;0;0 каждый;E;2;0;0;0 каждому;E;2;0;0;0 -после;L;0;0;0;0 \ No newline at end of file +после;L;0;0;0;0 +^[0-9]{1,2}[сС]$;0;0;0;60;1 +^[0-9]{1,2}м$;1;0;0;60;1 +^[0-9]{1,2}[чЧ]$;2;0;0;24;1 +^[0-9]{1,2}[дД]$;3;0;0;0;1 +^[0-9]{1,2}М$;4;0;0;12;1 +^[0-9]{1,2}[гГ]$;5;0;0;0;1 \ No newline at end of file diff --git a/lib/regex-extractor.js b/lib/regex-extractor.js index 318e07d..6bc9615 100644 --- a/lib/regex-extractor.js +++ b/lib/regex-extractor.js @@ -32,7 +32,7 @@ function extractRegexChars(string, errorLimit) { expression = Object.assign(new Expression(), expressions[j]); if (expression.is_regex) { let regexp = new RegExp(expression.text, 'g'); - let match = word.match(regexp); + let match = words[k].match(regexp); if (match != null && match.length == 1) { found = true; expression.text = words[k];