Skip to content

Commit a857d3f

Browse files
author
Wanasit Tanakitrungruang
committedJan 25, 2025
New: (en) Add unlikely format filter specifically for some English words
1 parent 80c126b commit a857d3f

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed
 

‎src/locales/en/configuration.ts

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import ENMergeRelativeAfterDateRefiner from "./refiners/ENMergeRelativeAfterDate
2424
import ENMergeRelativeFollowByDateRefiner from "./refiners/ENMergeRelativeFollowByDateRefiner";
2525
import OverlapRemovalRefiner from "../../common/refiners/OverlapRemovalRefiner";
2626
import ENExtractYearSuffixRefiner from "./refiners/ENExtractYearSuffixRefiner";
27+
import ENUnlikelyFormatFilter from "./refiners/ENUnlikelyFormatFilter";
2728

2829
export default class ENDefaultConfiguration {
2930
/**
@@ -37,6 +38,7 @@ export default class ENDefaultConfiguration {
3738
option.parsers.push(new ENMonthNameParser());
3839
option.parsers.push(new ENRelativeDateFormatParser());
3940
option.parsers.push(new ENTimeUnitCasualRelativeFormatParser());
41+
option.refiners.push(new ENUnlikelyFormatFilter());
4042
return option;
4143
}
4244

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Filter } from "../../../common/abstractRefiners";
2+
import { ParsingResult } from "../../../results";
3+
4+
export default class ENUnlikelyFormatFilter extends Filter {
5+
constructor() {
6+
super();
7+
}
8+
9+
isValid(context, result: ParsingResult): boolean {
10+
const text = result.text.trim();
11+
12+
// If the result is consists of the whole text (e.g. "2024", "May", etc),
13+
// then it is unlikely to be a date.
14+
if (text === context.text.trim()) {
15+
return true;
16+
}
17+
18+
// In English, the word "may" is a month name, but it is also a modal verb.
19+
// Check if the text before "may" follows some allowed patterns.
20+
if (text.toLowerCase() === "may") {
21+
const textBefore = context.text.substring(0, result.index).trim();
22+
if (!textBefore.match(/\b(in)$/i)) {
23+
context.debug(() => {
24+
console.log(`Removing unlikely result: ${result}`);
25+
});
26+
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
}

‎test/en/en_casual.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ test("Test - Random negative text", () => {
412412

413413
testUnexpectedResult(chrono, "do I have the money");
414414

415+
testUnexpectedResult(chrono, "I may by here. May the force be with you. Theresa may become PM soon.");
416+
415417
testUnexpectedResult(chrono.casual, "do I have the money");
416418

417419
testUnexpectedResult(chrono.en.GB, "do I have the money");

‎test/en/en_month.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as chrono from "../../src";
2-
import { testSingleCase } from "../test_util";
2+
import { testSingleCase, testUnexpectedResult } from "../test_util";
33

44
test("Test - Month-Year expression", function () {
55
testSingleCase(chrono, "September 2012", (result) => {
@@ -272,3 +272,16 @@ test("Test - Month should not have timezone", () => {
272272
}
273273
);
274274
});
275+
276+
test("Test - Month only in different context", () => {
277+
testSingleCase(chrono.casual, "May", (result) => {
278+
expect(result.text).toContain("May");
279+
});
280+
281+
testSingleCase(chrono.casual, "in May", (result) => {
282+
expect(result.text).toContain("May");
283+
});
284+
285+
testUnexpectedResult(chrono.casual, "The mountain may not move");
286+
testUnexpectedResult(chrono.casual, "May not be correct");
287+
});

0 commit comments

Comments
 (0)
Please sign in to comment.