-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #398 from jebbench/enhance-dutch-support
Added more support for Dutch.
- Loading branch information
Showing
11 changed files
with
1,012 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { TIME_UNIT_DICTIONARY } from "../constants"; | ||
import { ParsingContext } from "../../../chrono"; | ||
import { ParsingComponents } from "../../../results"; | ||
import dayjs from "dayjs"; | ||
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary"; | ||
import { matchAnyPattern } from "../../../utils/pattern"; | ||
|
||
const PATTERN = new RegExp( | ||
`(dit|deze|komende|volgend|volgende|afgelopen|vorige)\\s*(${matchAnyPattern(TIME_UNIT_DICTIONARY)})(?=\\s*)` + | ||
"(?=\\W|$)", | ||
"i" | ||
); | ||
|
||
const MODIFIER_WORD_GROUP = 1; | ||
const RELATIVE_WORD_GROUP = 2; | ||
|
||
export default class NLRelativeDateFormatParser extends AbstractParserWithWordBoundaryChecking { | ||
innerPattern(): RegExp { | ||
return PATTERN; | ||
} | ||
|
||
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents { | ||
const modifier = match[MODIFIER_WORD_GROUP].toLowerCase(); | ||
const unitWord = match[RELATIVE_WORD_GROUP].toLowerCase(); | ||
const timeunit = TIME_UNIT_DICTIONARY[unitWord]; | ||
|
||
if (modifier == "volgend" || modifier == "volgende" || modifier == "komende") { | ||
const timeUnits = {}; | ||
timeUnits[timeunit] = 1; | ||
return ParsingComponents.createRelativeFromRefDate(context.refDate, timeUnits); | ||
} | ||
|
||
if (modifier == "afgelopen" || modifier == "vorige") { | ||
const timeUnits = {}; | ||
timeUnits[timeunit] = -1; | ||
return ParsingComponents.createRelativeFromRefDate(context.refDate, timeUnits); | ||
} | ||
|
||
const components = context.createParsingComponents(); | ||
let date = dayjs(context.refDate); | ||
|
||
// This week | ||
if (unitWord.match(/week/i)) { | ||
date = date.add(-date.get("d"), "d"); | ||
components.imply("day", date.date()); | ||
components.imply("month", date.month() + 1); | ||
components.imply("year", date.year()); | ||
} | ||
|
||
// This month | ||
else if (unitWord.match(/maand/i)) { | ||
date = date.add(-date.date() + 1, "d"); | ||
components.imply("day", date.date()); | ||
components.assign("year", date.year()); | ||
components.assign("month", date.month() + 1); | ||
} | ||
|
||
// This year | ||
else if (unitWord.match(/jaar/i)) { | ||
date = date.add(-date.date() + 1, "d"); | ||
date = date.add(-date.month(), "month"); | ||
|
||
components.imply("day", date.date()); | ||
components.imply("month", date.month() + 1); | ||
components.assign("year", date.year()); | ||
} | ||
|
||
return components; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ParsingContext } from "../../../chrono"; | ||
import { parseTimeUnits, TIME_UNITS_PATTERN } from "../constants"; | ||
import { ParsingComponents } from "../../../results"; | ||
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary"; | ||
import { reverseTimeUnits } from "../../../utils/timeunits"; | ||
|
||
const PATTERN = new RegExp("" + "(" + TIME_UNITS_PATTERN + ")" + "(?:geleden|voor|eerder)(?=(?:\\W|$))", "i"); | ||
|
||
const STRICT_PATTERN = new RegExp("" + "(" + TIME_UNITS_PATTERN + ")" + "geleden(?=(?:\\W|$))", "i"); | ||
|
||
export default class NLTimeUnitAgoFormatParser extends AbstractParserWithWordBoundaryChecking { | ||
constructor(private strictMode: boolean) { | ||
super(); | ||
} | ||
|
||
innerPattern(): RegExp { | ||
return this.strictMode ? STRICT_PATTERN : PATTERN; | ||
} | ||
|
||
innerExtract(context: ParsingContext, match: RegExpMatchArray) { | ||
const timeUnits = parseTimeUnits(match[1]); | ||
const outputTimeUnits = reverseTimeUnits(timeUnits); | ||
|
||
return ParsingComponents.createRelativeFromRefDate(context.refDate, outputTimeUnits); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/locales/nl/parsers/NLTimeUnitCasualRelativeFormatParser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { TIME_UNITS_PATTERN, parseTimeUnits } from "../constants"; | ||
import { ParsingContext } from "../../../chrono"; | ||
import { ParsingComponents } from "../../../results"; | ||
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary"; | ||
import { reverseTimeUnits } from "../../../utils/timeunits"; | ||
|
||
const PATTERN = new RegExp(`(deze|vorige|afgelopen|komende|over|\\+|-)\\s*(${TIME_UNITS_PATTERN})(?=\\W|$)`, "i"); | ||
|
||
export default class NLTimeUnitCasualRelativeFormatParser extends AbstractParserWithWordBoundaryChecking { | ||
innerPattern(): RegExp { | ||
return PATTERN; | ||
} | ||
|
||
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents { | ||
const prefix = match[1].toLowerCase(); | ||
let timeUnits = parseTimeUnits(match[2]); | ||
switch (prefix) { | ||
case "vorige": | ||
case "afgelopen": | ||
case "-": | ||
timeUnits = reverseTimeUnits(timeUnits); | ||
break; | ||
} | ||
|
||
return ParsingComponents.createRelativeFromRefDate(context.refDate, timeUnits); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ParsingContext } from "../../../chrono"; | ||
import { parseTimeUnits, TIME_UNITS_PATTERN } from "../constants"; | ||
import { ParsingComponents } from "../../../results"; | ||
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary"; | ||
|
||
const PATTERN = new RegExp( | ||
"" + "(" + TIME_UNITS_PATTERN + ")" + "(later|na|vanaf nu|voortaan|vooruit|uit)" + "(?=(?:\\W|$))", | ||
"i" | ||
); | ||
|
||
const STRICT_PATTERN = new RegExp("" + "(" + TIME_UNITS_PATTERN + ")" + "(later|vanaf nu)" + "(?=(?:\\W|$))", "i"); | ||
const GROUP_NUM_TIMEUNITS = 1; | ||
|
||
export default class NLTimeUnitLaterFormatParser extends AbstractParserWithWordBoundaryChecking { | ||
constructor(private strictMode: boolean) { | ||
super(); | ||
} | ||
|
||
innerPattern(): RegExp { | ||
return this.strictMode ? STRICT_PATTERN : PATTERN; | ||
} | ||
|
||
innerExtract(context: ParsingContext, match: RegExpMatchArray) { | ||
const fragments = parseTimeUnits(match[GROUP_NUM_TIMEUNITS]); | ||
return ParsingComponents.createRelativeFromRefDate(context.refDate, fragments); | ||
} | ||
} |
Oops, something went wrong.