Skip to content

Commit

Permalink
Fix: stop guessing single digit after dot as minute
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Feb 7, 2021
1 parent d898037 commit 1d949a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/common/parsers/AbstractTimeExpressionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ export abstract class AbstractTimeExpressionParser implements Parser {
const remainingText = context.text.substring(match.index + match[0].length);
const followingPattern = followingTimeExpression(this.followingPhase(), this.followingSuffix());
match = followingPattern.exec(remainingText);
if (!match) {
return result;
}

// Pattern "YY.YY -XXXX" is more like timezone offset
if (match[0].match(/^\s*([+-])\s*\d{3,4}$/)) {
return result;
if (
!match ||
// Pattern "YY.YY -XXXX" is more like timezone offset
match[0].match(/^\s*([+-])\s*\d{3,4}$/)
) {
return AbstractTimeExpressionParser.checkAndReturnStartTime(result);
}

result.end = this.extractFollowingTimeComponents(context, match, result);
Expand All @@ -105,6 +104,11 @@ export abstract class AbstractTimeExpressionParser implements Parser {

// ----- Minutes
if (match[MINUTE_GROUP] != null) {
if (match[MINUTE_GROUP].length == 1 && !match[AM_PM_HOUR_GROUP]) {
// Skip single digit minute e.g. "at 1.1 xx"
return null;
}

minute = parseInt(match[MINUTE_GROUP]);
} else if (hour > 100) {
minute = hour % 100;
Expand Down Expand Up @@ -280,4 +284,13 @@ export abstract class AbstractTimeExpressionParser implements Parser {

return components;
}

private static checkAndReturnStartTime(result) {
// Single digit (e.g "1") should not be counted as time expression
if (result.text.match(/^\d$/)) {
return null;
}

return result;
}
}
2 changes: 2 additions & 0 deletions test/en/en.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ test("Test - Random non-date patterns", function () {
testUnexpectedResult(chrono, "Version: 1.1.30");

testUnexpectedResult(chrono, "Version: 1.10.30");

testUnexpectedResult(chrono, "at 6.5 kilograms");
});

test("Test - Wikipedia Texts", function () {
Expand Down

0 comments on commit 1d949a2

Please sign in to comment.