Skip to content

Commit

Permalink
New: Able to assign tags parsing results/components
Browse files Browse the repository at this point in the history
  • Loading branch information
Wanasit Tanakitrungruang committed Sep 2, 2023
1 parent 96d4a37 commit 94684b6
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/calculation/mergingCalculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ export function mergeDateTimeComponent(
}
}

dateTimeComponent.addTags(dateComponent.tags());
dateTimeComponent.addTags(timeComponent.tags());
return dateTimeComponent;
}
2 changes: 2 additions & 0 deletions src/common/casualReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function now(reference: ReferenceWithTimezone): ParsingComponents {
if (reference.timezoneOffset !== null) {
component.assign("timezoneOffset", targetDate.utcOffset());
}
component.addTag("casualReference/now");
return component;
}

Expand All @@ -25,6 +26,7 @@ export function today(reference: ReferenceWithTimezone): ParsingComponents {
const component = new ParsingComponents(reference, {});
assignSimilarDate(component, targetDate);
implySimilarTime(component, targetDate);
component.addTag("casualReference/today");
return component;
}

Expand Down
23 changes: 17 additions & 6 deletions src/locales/en/parsers/ENCasualDateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,35 @@ export default class ENCasualDateParser extends AbstractParserWithWordBoundaryCh
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | ParsingResult {
let targetDate = dayjs(context.refDate);
const lowerText = match[0].toLowerCase();
const component = context.createParsingComponents();
let component = context.createParsingComponents();

switch (lowerText) {
case "now":
return references.now(context.reference);
component = references.now(context.reference);
component.addTag("ENCasualDateParser/extract/now");
break;

case "today":
return references.today(context.reference);
component = references.today(context.reference);
component.addTag("ENCasualDateParser/extract/today");
break;

case "yesterday":
return references.yesterday(context.reference);
component = references.yesterday(context.reference);
component.addTag("ENCasualDateParser/extract/yesterday");
break;

case "tomorrow":
case "tmr":
case "tmrw":
return references.tomorrow(context.reference);
component = references.tomorrow(context.reference);
component.addTag("ENCasualDateParser/extract/tomorrow");
break;

case "tonight":
return references.tonight(context.reference);
component = references.tonight(context.reference);
component.addTag("ENCasualDateParser/extract/tonight");
break;

default:
if (lowerText.match(/last\s*night/)) {
Expand All @@ -43,6 +53,7 @@ export default class ENCasualDateParser extends AbstractParserWithWordBoundaryCh

assignSimilarDate(component, targetDate);
component.imply("hour", 0);
component.addTag("ENCasualDateParser/extract/last_night");
}
break;
}
Expand Down
35 changes: 32 additions & 3 deletions src/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class ParsingComponents implements ParsedComponents {
private knownValues: { [c in Component]?: number };
private impliedValues: { [c in Component]?: number };
private reference: ReferenceWithTimezone;
private _tags = new Set<string>();

constructor(reference: ReferenceWithTimezone, knownComponents?: { [c in Component]?: number }) {
this.reference = reference;
Expand Down Expand Up @@ -155,9 +156,11 @@ export class ParsingComponents implements ParsedComponents {
}

toString() {
return `[ParsingComponents {knownValues: ${JSON.stringify(this.knownValues)}, impliedValues: ${JSON.stringify(
this.impliedValues
)}}, reference: ${JSON.stringify(this.reference)}]`;
return `[ParsingComponents {
tags: ${JSON.stringify(Array.from(this._tags).sort())},
knownValues: ${JSON.stringify(this.knownValues)},
impliedValues: ${JSON.stringify(this.impliedValues)}},
reference: ${JSON.stringify(this.reference)}]`;
}

dayjs() {
Expand All @@ -170,6 +173,22 @@ export class ParsingComponents implements ParsedComponents {
return new Date(date.getTime() + timezoneAdjustment * 60000);
}

addTag(tag: string): ParsingComponents {
this._tags.add(tag);
return this;
}

addTags(tags: string[] | Set<string>): ParsingComponents {
for (const tag of tags) {
this._tags.add(tag);
}
return this;
}

tags(): Set<string> {
return new Set(this._tags);
}

private dateWithoutTimezoneAdjustment() {
const date = new Date(
this.get("year"),
Expand Down Expand Up @@ -271,6 +290,16 @@ export class ParsingResult implements ParsedResult {
return this.start.date();
}

tags(): Set<string> {
const combinedTags: Set<string> = new Set(this.start.tags());
if (this.end) {
for (const tag of this.end.tags()) {
combinedTags.add(tag);
}
}
return combinedTags;
}

toString() {
return `[ParsingResult {index: ${this.index}, text: '${this.text}', ...}]`;
}
Expand Down
12 changes: 11 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ export interface ParsedResult {
readonly end?: ParsedComponents;

/**
* Create a javascript date object (from the result.start).
* @return a javascript date object created from the `result.start`.
*/
date(): Date;

/**
* @return debugging tags combined of the `result.start` and `result.end`.
*/
tags(): Set<string>;
}

/**
Expand All @@ -105,6 +110,11 @@ export interface ParsedComponents {
* @return a javascript date object.
*/
date(): Date;

/**
* @return debugging tags of the parsed component.
*/
tags(): Set<string>;
}

export type Component =
Expand Down
37 changes: 37 additions & 0 deletions test/system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import UnlikelyFormatFilter from "../src/common/refiners/UnlikelyFormatFilter";
import SlashDateFormatParser from "../src/common/parsers/SlashDateFormatParser";
import ENWeekdayParser from "../src/locales/en/parsers/ENWeekdayParser";
import ENTimeUnitCasualRelativeFormatParser from "../src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser";
import { ParsingComponents } from "../src/";

//-------------------------------------

Expand Down Expand Up @@ -124,6 +125,42 @@ test("Test - Add custom refiner example", () => {
});
});

test("Test - Add custom parser with tags example", () => {
const custom = chrono.casual.clone();
custom.parsers.push({
pattern: () => {
return /\bChristmas\b/i;
},
extract: (context) => {
return context
.createParsingComponents({
day: 25,
month: 12,
})
.addTag("CustomParser/chirstmas");
},
});

testSingleCase(custom, "Doing something tomorrow", (result) => {
expect(result.text).toBe("tomorrow");
expect(result.tags()).toContain("ENCasualDateParser/extract/tomorrow");
});

testSingleCase(custom, "I'll arrive at 2.30AM on Christmas", (result) => {
expect(result.text).toBe("at 2.30AM on Christmas");
expect(result.tags()).toContain("CustomParser/chirstmas");
// TODO: Check for time (expression) parsing tags
});

testSingleCase(custom, "I'll arrive at Christmas night", (result) => {
expect(result.text).toBe("Christmas night");
expect(result.tags()).toContain("CustomParser/chirstmas");
// TODO: Check for time (casual) parsing tags
});

// TODO: Check if the merge date range combine tags correctly
});

test("Test - Remove a parser example", () => {
const custom = chrono.en.strict.clone();
custom.parsers = custom.parsers.filter((r) => !(r instanceof SlashDateFormatParser));
Expand Down

0 comments on commit 94684b6

Please sign in to comment.