Skip to content

Commit

Permalink
Fix: (en) incorrect timeunit words extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Sep 16, 2024
1 parent 31bcae3 commit 08e4dc4
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/locales/en/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export const TIME_UNITS_NO_ABBR_PATTERN = repeatedTimeunitPattern(
TIME_UNIT_CONNECTOR_PATTERN
);

export function parseTimeUnits(timeunitText): TimeUnits {
export function parseTimeUnits(timeunitText): null | TimeUnits {
const fragments = {};
let remainingText = timeunitText;
let match = SINGLE_TIME_UNIT_REGEX.exec(remainingText);
Expand All @@ -286,10 +286,16 @@ export function parseTimeUnits(timeunitText): TimeUnits {
remainingText = remainingText.substring(match[0].length).trim();
match = SINGLE_TIME_UNIT_REGEX.exec(remainingText);
}
if (Object.keys(fragments).length == 0) {
return null;
}
return fragments;
}

function collectDateTimeFragment(fragments, match) {
if (match[0].match(/^[a-zA-Z]+$/)) {
return;
}
const num = parseNumberPattern(match[1]);
const unit = TIME_UNIT_DICTIONARY[match[2].toLowerCase()];
fragments[unit] = num;
Expand Down
4 changes: 3 additions & 1 deletion src/locales/en/parsers/ENTimeUnitAgoFormatParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export default class ENTimeUnitAgoFormatParser extends AbstractParserWithWordBou

innerExtract(context: ParsingContext, match: RegExpMatchArray) {
const timeUnits = parseTimeUnits(match[1]);
if (!timeUnits) {
return null;
}
const outputTimeUnits = reverseTimeUnits(timeUnits);

return ParsingComponents.createRelativeFromReference(context.reference, outputTimeUnits);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ export default class ENTimeUnitCasualRelativeFormatParser extends AbstractParser
return this.allowAbbreviations ? PATTERN : PATTERN_NO_ABBR;
}

innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
const prefix = match[1].toLowerCase();
let timeUnits = parseTimeUnits(match[2]);
if (!timeUnits) {
return null;
}
switch (prefix) {
case "last":
case "past":
case "-":
timeUnits = reverseTimeUnits(timeUnits);
break;
}

return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
}
}
7 changes: 5 additions & 2 deletions src/locales/en/parsers/ENTimeUnitLaterFormatParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export default class ENTimeUnitLaterFormatParser extends AbstractParserWithWordB
}

innerExtract(context: ParsingContext, match: RegExpMatchArray) {
const fragments = parseTimeUnits(match[GROUP_NUM_TIMEUNITS]);
return ParsingComponents.createRelativeFromReference(context.reference, fragments);
const timeUnits = parseTimeUnits(match[GROUP_NUM_TIMEUNITS]);
if (!timeUnits) {
return null;
}
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
}
}
6 changes: 4 additions & 2 deletions src/locales/en/parsers/ENTimeUnitWithinFormatParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ export default class ENTimeUnitWithinFormatParser extends AbstractParserWithWord
return context.option.forwardDate ? PATTERN_WITH_OPTIONAL_PREFIX : PATTERN_WITH_PREFIX;
}

innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
innerExtract(context: ParsingContext, match: RegExpMatchArray) {
// Exclude "for the unit" phases, e.g. "for the year"
if (match[0].match(/^for\s*the\s*\w+/)) {
return null;
}

const timeUnits = parseTimeUnits(match[1]);
if (!timeUnits) {
return null;
}
return ParsingComponents.createRelativeFromReference(context.reference, timeUnits);
}
}
3 changes: 3 additions & 0 deletions test/en/en_time_units_ago.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,7 @@ test("Test - Negative cases", function () {
testUnexpectedResult(chrono, "15 hours 29 min");
testUnexpectedResult(chrono, "a few hour");
testUnexpectedResult(chrono, "5 days");

testUnexpectedResult(chrono, "am ago");
testUnexpectedResult(chrono, "them ago");
});
3 changes: 3 additions & 0 deletions test/en/en_time_units_casual_relative.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,7 @@ test("Test - Negative cases", () => {
testUnexpectedResult(chrono.casual, "1 m", new Date(2015, 7 - 1, 10, 12, 14));
testUnexpectedResult(chrono.casual, "the day", new Date(2015, 7 - 1, 10, 12, 14));
testUnexpectedResult(chrono.casual, "a day", new Date(2015, 7 - 1, 10, 12, 14));

testUnexpectedResult(chrono, "+am");
testUnexpectedResult(chrono, "+them");
});
4 changes: 4 additions & 0 deletions test/en/en_time_units_later.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,7 @@ test("Test - Plus after reference", () => {
expect(result.start.get("minute")).toBe(10);
});
});

test("Test - Negative cases", () => {
testUnexpectedResult(chrono, "tell them later");
});
5 changes: 5 additions & 0 deletions test/en/en_time_units_within.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,8 @@ test("Test - Forward date option", () => {
expect(result.start.get("day")).toBe(1);
});
});

test("Test - Negative cases", () => {
testUnexpectedResult(chrono, "in am");
testUnexpectedResult(chrono, "in them");
});

0 comments on commit 08e4dc4

Please sign in to comment.