-
Notifications
You must be signed in to change notification settings - Fork 342
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 #373 from JeroenVdb/master
Fixed and extend NL locale + add test for all
- Loading branch information
Showing
21 changed files
with
1,898 additions
and
64 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
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,75 @@ | ||
import { ParsingContext } from "../../../chrono"; | ||
import { ParsingComponents, ParsingResult } from "../../../results"; | ||
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary"; | ||
import { Meridiem } from "../../../index"; | ||
import { assignSimilarDate, assignTheNextDay } from "../../../utils/dayjs"; | ||
import dayjs from "dayjs"; | ||
|
||
/* | ||
* Find combined words | ||
* - morgenochtend | ||
* - morgenmiddag | ||
* - morgennamiddag | ||
* - morgenavond | ||
* - morgennacht | ||
* - vanochtend | ||
* - vanmiddag | ||
* - vannamiddag | ||
* - vanavond | ||
* - vannacht | ||
* - gisterenochtend | ||
* - gisterenmiddag | ||
* - gisterennamiddag | ||
* - gisterenavond | ||
* - gisterennacht | ||
* */ | ||
|
||
const DATE_GROUP = 1; | ||
const TIME_OF_DAY_GROUP = 2; | ||
|
||
export default class NLCasualDateTimeParser extends AbstractParserWithWordBoundaryChecking { | ||
innerPattern(context: ParsingContext): RegExp { | ||
return /(gisteren|morgen|van)(ochtend|middag|namiddag|avond|nacht)(?=\W|$)/i; | ||
} | ||
|
||
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | ParsingResult { | ||
const dateText = match[DATE_GROUP].toLowerCase(); | ||
const timeText = match[TIME_OF_DAY_GROUP].toLowerCase(); | ||
const component = context.createParsingComponents(); | ||
const targetDate = dayjs(context.refDate); | ||
|
||
switch (dateText) { | ||
case "gisteren": | ||
assignSimilarDate(component, targetDate.add(-1, "day")); | ||
break; | ||
case "van": | ||
assignSimilarDate(component, targetDate); | ||
break; | ||
case "morgen": | ||
assignTheNextDay(component, targetDate); | ||
break; | ||
} | ||
|
||
switch (timeText) { | ||
case "ochtend": | ||
component.imply("meridiem", Meridiem.AM); | ||
component.imply("hour", 6); | ||
break; | ||
case "middag": | ||
component.imply("meridiem", Meridiem.AM); | ||
component.imply("hour", 12); | ||
break; | ||
case "namiddag": | ||
component.imply("meridiem", Meridiem.PM); | ||
component.imply("hour", 15); | ||
break; | ||
|
||
case "avond": | ||
component.imply("meridiem", Meridiem.PM); | ||
component.imply("hour", 20); | ||
break; | ||
} | ||
|
||
return component; | ||
} | ||
} |
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,49 @@ | ||
import { ParsingContext } from "../../../chrono"; | ||
import { MONTH_DICTIONARY } from "../constants"; | ||
import { matchAnyPattern } from "../../../utils/pattern"; | ||
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary"; | ||
|
||
/* | ||
Date format with slash "/" between numbers like ENSlashDateFormatParser, | ||
but this parser expect year before month and date. | ||
- YYYY/MM/DD | ||
- YYYY-MM-DD | ||
- YYYY.MM.DD | ||
*/ | ||
const PATTERN = new RegExp( | ||
`([0-9]{4})[\\.\\/\\s]` + | ||
`(?:(${matchAnyPattern(MONTH_DICTIONARY)})|([0-9]{1,2}))[\\.\\/\\s]` + | ||
`([0-9]{1,2})` + | ||
"(?=\\W|$)", | ||
"i" | ||
); | ||
|
||
const YEAR_NUMBER_GROUP = 1; | ||
const MONTH_NAME_GROUP = 2; | ||
const MONTH_NUMBER_GROUP = 3; | ||
const DATE_NUMBER_GROUP = 4; | ||
|
||
export default class NLCasualYearMonthDayParser extends AbstractParserWithWordBoundaryChecking { | ||
innerPattern(): RegExp { | ||
return PATTERN; | ||
} | ||
|
||
innerExtract(context: ParsingContext, match: RegExpMatchArray) { | ||
const month = match[MONTH_NUMBER_GROUP] | ||
? parseInt(match[MONTH_NUMBER_GROUP]) | ||
: MONTH_DICTIONARY[match[MONTH_NAME_GROUP].toLowerCase()]; | ||
|
||
if (month < 1 || month > 12) { | ||
return null; | ||
} | ||
|
||
const year = parseInt(match[YEAR_NUMBER_GROUP]); | ||
const day = parseInt(match[DATE_NUMBER_GROUP]); | ||
|
||
return { | ||
day: day, | ||
month: month, | ||
year: year, | ||
}; | ||
} | ||
} |
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
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,22 @@ | ||
import { AbstractTimeExpressionParser } from "../../../common/parsers/AbstractTimeExpressionParser"; | ||
import { ParsingComponents } from "../../../results"; | ||
import { ParsingContext } from "../../../chrono"; | ||
|
||
export default class NLTimeExpressionParser extends AbstractTimeExpressionParser { | ||
primaryPrefix(): string { | ||
return "(?:(?:om)\\s*)?"; | ||
} | ||
|
||
followingPhase(): string { | ||
return "\\s*(?:\\-|\\–|\\~|\\〜|om|\\?)\\s*"; | ||
} | ||
|
||
extractPrimaryTimeComponents(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | null { | ||
// This looks more like a year e.g. 2020 | ||
if (match[0].match(/^\s*\d{4}\s*$/)) { | ||
return null; | ||
} | ||
|
||
return super.extractPrimaryTimeComponents(context, match); | ||
} | ||
} |
Oops, something went wrong.