Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EN] Fix for multiple time units within parser (e.g. set a timer for 5 minutes and 30 seconds) #542

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/locales/en/parsers/ENTimeUnitWithinFormatParser.ts
Expand Up @@ -3,15 +3,19 @@ import { ParsingContext } from "../../../chrono";
import { ParsingComponents } from "../../../results";
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";

const TIME_UNIT_PATTERN = `(${TIME_UNITS_PATTERN})`;
const TIME_UNIT_CONNECTOR_PATTERN = `\\s+and\\s+`;
const MULTIPLE_TIME_UNITS_PATTERN = `${TIME_UNIT_PATTERN}(?:${TIME_UNIT_CONNECTOR_PATTERN}${TIME_UNIT_PATTERN})*`;

const PATTERN_WITH_OPTIONAL_PREFIX = new RegExp(
`(?:(?:within|in|for)\\s*)?` +
`(?:(?:about|around|roughly|approximately|just)\\s*(?:~\\s*)?)?(${TIME_UNITS_PATTERN})(?=\\W|$)`,
`(?:(?:about|around|roughly|approximately|just)\\s*(?:~\\s*)?)?(${MULTIPLE_TIME_UNITS_PATTERN})(?=\\W|$)`,
"i"
);

const PATTERN_WITH_PREFIX = new RegExp(
`(?:within|in|for)\\s*` +
`(?:(?:about|around|roughly|approximately|just)\\s*(?:~\\s*)?)?(${TIME_UNITS_PATTERN})(?=\\W|$)`,
`(?:(?:about|around|roughly|approximately|just)\\s*(?:~\\s*)?)?(${MULTIPLE_TIME_UNITS_PATTERN})(?=\\W|$)`,
"i"
);

Expand Down
14 changes: 14 additions & 0 deletions test/en/en_time_units_within.test.ts
Expand Up @@ -53,6 +53,20 @@ test("Test - The normal within expression", () => {
expect(result.start).toBeDate(new Date(2012, 7, 10, 12, 19));
});

testSingleCase(chrono, "set a timer for 5 minutes and 30 seconds", new Date(2012, 7, 10, 12, 14), (result) => {
expect(result.index).toBe(12);
expect(result.text).toBe("for 5 minutes and 30 seconds");

expect(result.start).toBeDate(new Date(2012, 7, 10, 12, 19, 30));
});

testSingleCase(chrono, "set a timer for 10 minutes and 10 seconds", new Date(2012, 7, 10, 12, 14), { forwardDate: true }, (result) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also test a combination of both comma + and e.g. "1 hour, 10 minutes, and 10 seconds".

expect(result.index).toBe(12);
expect(result.text).toBe("for 10 minutes and 10 seconds");

expect(result.start).toBeDate(new Date(2012, 7, 10, 12, 24, 10));
});

testSingleCase(chrono, "within 1 hour", new Date(2012, 7, 10, 12, 14), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("within 1 hour");
Expand Down