Skip to content

Commit

Permalink
Added support for short time case ("3ч 20м")
Browse files Browse the repository at this point in the history
  • Loading branch information
alordash committed Sep 21, 2020
1 parent f8d0c77 commit c51d0ff
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 28 deletions.
146 changes: 121 additions & 25 deletions lib/date-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ class ParsedTime {
isOffset;
/**@type {Boolean} */
isFixed;
/**
* @param {DateTypes} dateType
* @param {TimeTypes} timeType
* @param {Number} number
* @param {Array.<Number>} indexes
* @param {Number} context
* @param {Number} prevalence
/**
* @param {DateTypes} dateType
* @param {TimeTypes} timeType
* @param {Number} number
* @param {Array.<Number>} indexes
* @param {Number} context
* @param {Number} prevalence
* @param {{}} options
*/
*/
constructor(dateType, timeType, number, indexes, context, prevalence, options) {
this.dateType = dateType;
this.timeType = timeType;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
})
];

Expand Down Expand Up @@ -834,7 +930,7 @@ function findParsedTimesByIndex(parsedTimes, index) {
* @returns {Array.<Number>}
*/
function findAllMatchingParsedTimes(allowedTypes, step, start, expressions, parsedTimes, ignoreValidMode) {
if(typeof(ignoreValidMode) == 'undefined') {
if (typeof (ignoreValidMode) == 'undefined') {
ignoreValidMode = ValidModes.certified;
}
let i = start + step;
Expand Down
8 changes: 7 additions & 1 deletion lib/expressions/EN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
8 changes: 7 additions & 1 deletion lib/expressions/RU.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
после;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
2 changes: 1 addition & 1 deletion lib/regex-extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit c51d0ff

Please sign in to comment.