Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance de #442

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 184 additions & 8 deletions src/locales/de/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,159 @@ export const INTEGER_WORD_DICTIONARY: { [word: string]: number } = {
"zwoelf": 12,
};

export const ORDINAL_WORD_DICTIONARY: { [word: string]: number } = {
erste: 1,
ersten: 1,
erster: 1,
erstes: 1,
zweite: 2,
zweiten: 2,
zweiter: 2,
zweites: 2,
dritte: 3,
dritten: 3,
dritter: 3,
drittes: 3,
vierte: 4,
vierten: 4,
fünfte: 5,
fünften: 5,
fünfter: 5,
fünftes: 5,
fuenfte: 5,
fuenften: 5,
fuenfter: 5,
fuenftes: 5,
sechste: 6,
sechsten: 6,
sechster: 6,
sechstes: 6,
siebte: 7,
siebten: 7,
siebter: 7,
siebtes: 7,
siebente: 7,
siebenten: 7,
siebenter: 7,
siebentes: 7,
achte: 8,
achten: 8,
achter: 8,
achtes: 8,
neunte: 9,
neunten: 9,
neunter: 9,
neuntes: 9,
zehnte: 10,
zehnten: 10,
zehnter: 10,
zehntes: 10,
elfte: 11,
elften: 11,
elfter: 11,
elftes: 11,
zwölfte: 12,
zwölften: 12,
zwölfter: 12,
zwölftes: 12,
zwoelfte: 12,
zwoelften: 12,
zwoelfter: 12,
zwoelftes: 12,
dreizehnte: 13,
dreizehnten: 13,
dreizehnter: 13,
dreizehntes: 13,
vierzehnte: 14,
vierzehnten: 14,
vierzehnter: 14,
vierzehntes: 14,
fünfzehnte: 15,
fünfzehnten: 15,
fünfzehnter: 15,
fünfzehntes: 15,
fuenfzehnte: 15,
fuenfzehnten: 15,
fuenfzehnter: 15,
fuenfzehntes: 15,
sechzehnte: 16,
sechzehnten: 16,
sechzehnter: 16,
sechzehntes: 16,
siebzehnte: 17,
siebzehnten: 17,
siebzehnter: 17,
siebzehntes: 17,
achtzehnte: 18,
achtzehnten: 18,
achtzehnter: 18,
achtzehntes: 18,
neunzehnte: 19,
neunzehnten: 19,
neunzehnter: 19,
neunzehntes: 19,
zwanzigste: 20,
zwanzigsten: 20,
zwanzigster: 20,
zwanzigstes: 20,
einundzwanzigste: 21,
einundzwanzigsten: 21,
einundzwanzigster: 21,
einundzwanzigstes: 21,
zweiundzwanzigste: 22,
zweiundzwanzigsten: 22,
zweiundzwanzigster: 22,
zweiundzwanzigstes: 22,
dreiundzwanzigste: 23,
dreiundzwanzigsten: 23,
dreiundzwanzigster: 23,
dreiundzwanzigstes: 23,
vierundzwanzigste: 24,
vierundzwanzigsten: 24,
vierundzwanzigster: 24,
vierundzwanzigstes: 24,
fünfundzwanzigste: 25,
fünfundzwanzigsten: 25,
fünfundzwanzigster: 25,
fünfundzwanzigstes: 25,
fuenfundzwanzigste: 25,
fuenfundzwanzigsten: 25,
fuenfundzwanzigster: 25,
fuenfundzwanzigstes: 25,
sechsundzwanzigste: 26,
sechsundzwanzigsten: 26,
sechsundzwanzigster: 26,
sechsundzwanzigstes: 26,
siebenundzwanzigste: 27,
siebenundzwanzigsten: 27,
siebenundzwanzigster: 27,
siebenundzwanzigstes: 27,
achtundzanzigste: 28,
achtundzanzigsten: 28,
achtundzanzigster: 28,
achtundzanzigstes: 28,
neundundzwanzigste: 29,
neundundzwanzigsten: 29,
neundundzwanzigster: 29,
neundundzwanzigstes: 29,
dreißigste: 30,
dreißigsten: 30,
dreißigster: 30,
dreißigstes: 30,
dreissigste: 30,
dreissigsten: 30,
dreissigster: 30,
dreissigstes: 30,
einunddreißigste: 31,
einunddreißigsten: 31,
einunddreißigster: 31,
einunddreißigstes: 31,
einunddreissigste: 31,
einunddreissigsten: 31,
einunddreissigster: 31,
einunddreissigstes: 31,
};

export const TIME_UNIT_DICTIONARY: { [word: string]: OpUnitType | QUnitType } = {
sek: "second",
sekunde: "second",
Expand All @@ -97,6 +250,7 @@ export const TIME_UNIT_DICTIONARY: { [word: string]: OpUnitType | QUnitType } =
std: "hour",
stunde: "hour",
stunden: "hour",
d: "d",
tag: "d",
tage: "d",
tagen: "d",
Expand All @@ -123,25 +277,44 @@ export const TIME_UNIT_DICTIONARY: { [word: string]: OpUnitType | QUnitType } =

export const NUMBER_PATTERN = `(?:${matchAnyPattern(
INTEGER_WORD_DICTIONARY
)}|[0-9]+|[0-9]+\\.[0-9]+|half(?:\\s*an?)?|an?\\b(?:\\s*few)?|few|several|a?\\s*couple\\s*(?:of)?)`;
)}|[0-9]+|[0-9]+,[0-9]+|(?:ein(?:e(?:m|n|r|s)?)?\\s*)?(?:halb(?:e(?:n|r)?)?|(?:drei\\s?)?viertel|vtl\\.?)|ein\\s+paar|einige(?:r|n)?|mehrere(?:r|n)?|wenige(?:r|n)?|ein(?:e(?:m|n|r|s)?)?)`;

export function parseNumberPattern(match: string): number {
const num = match.toLowerCase();
if (INTEGER_WORD_DICTIONARY[num] !== undefined) {
return INTEGER_WORD_DICTIONARY[num];
} else if (num === "a" || num === "an") {
} else if (num === "ein" || num === "einem" || num === "einen" || num === "einer" || num === "eines") {
return 1;
} else if (num.match(/few/)) {
} else if (num.match(/wenige/)) {
return 3;
} else if (num.match(/half/)) {
} else if (num.match(/halb/)) {
return 0.5;
} else if (num.match(/couple/)) {
} else if (num.match(/drei\s?viertel/)) {
return 0.75;
} else if (num.match(/viertel|vtl/)) {
return 0.25;
} else if (num.match(/paar/)) {
return 2;
} else if (num.match(/several/)) {
} else if (num.match(/einige/)) {
return 5;
} else if (num.match(/mehrere/)) {
return 7;
}

return parseFloat(num);
return parseFloat(num.replace(",", "."));
}

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

export const ORDINAL_NUMBER_PATTERN = `(?:${matchAnyPattern(ORDINAL_WORD_DICTIONARY)}|[0-9]{1,2}\\.?(?:te(?:n|r|s)?)?)`;
export function parseOrdinalNumberPattern(match: string): number {
let num = match.toLowerCase();
if (ORDINAL_WORD_DICTIONARY[num] !== undefined) {
return ORDINAL_WORD_DICTIONARY[num];
}

num = num.replace(/(?:\\.?(?:te(?:n|r|s)?))$/i, "");
return parseInt(num);
}

//-----------------------------
Expand Down Expand Up @@ -172,7 +345,10 @@ export function parseYear(match: string): number {
const SINGLE_TIME_UNIT_PATTERN = `(${NUMBER_PATTERN})\\s{0,5}(${matchAnyPattern(TIME_UNIT_DICTIONARY)})\\s{0,5}`;
const SINGLE_TIME_UNIT_REGEX = new RegExp(SINGLE_TIME_UNIT_PATTERN, "i");

export const TIME_UNITS_PATTERN = repeatedTimeunitPattern("", SINGLE_TIME_UNIT_PATTERN);
export const TIME_UNITS_PATTERN = repeatedTimeunitPattern(
`(?:(?:etwa|ungefähr|ca\\.?)\\s{0,3})?`,
SINGLE_TIME_UNIT_PATTERN
);

export function parseTimeUnits(timeunitText): TimeUnits {
const fragments = {};
Expand Down
12 changes: 8 additions & 4 deletions src/locales/de/parsers/DEMonthNameLittleEndianParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { ParsingResult } from "../../../results";
import { findYearClosestToRef } from "../../../calculation/years";
import { MONTH_DICTIONARY } from "../constants";
import { YEAR_PATTERN, parseYear } from "../constants";
import { ORDINAL_NUMBER_PATTERN, parseOrdinalNumberPattern } from "../constants";
import { matchAnyPattern } from "../../../utils/pattern";
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";

const PATTERN = new RegExp(
"(?:am\\s*?)?" +
"(?:den\\s*?)?" +
`([0-9]{1,2})\\.` +
`(?:\\s*(?:bis(?:\\s*(?:am|zum))?|\\-|\\–|\\s)\\s*([0-9]{1,2})\\.?)?\\s*` +
`(${ORDINAL_NUMBER_PATTERN})` +
`(?:` +
`\\s*(?:bis(?:\\s*(?:am|zum))?|\\-|\\–|\\s)?\\s*` +
`(${ORDINAL_NUMBER_PATTERN})` +
")?\\s*" +
`(${matchAnyPattern(MONTH_DICTIONARY)})` +
`(?:(?:-|/|,?\\s*)(${YEAR_PATTERN}(?![^\\s]\\d)))?` +
`(?=\\W|$)`,
Expand All @@ -31,7 +35,7 @@ export default class DEMonthNameLittleEndianParser extends AbstractParserWithWor
const result = context.createParsingResult(match.index, match[0]);

const month = MONTH_DICTIONARY[match[MONTH_NAME_GROUP].toLowerCase()];
const day = parseInt(match[DATE_GROUP]);
const day = parseOrdinalNumberPattern(match[DATE_GROUP]);
if (day > 31) {
// e.g. "[96 Aug]" => "9[6 Aug]", we need to shift away from the next number
match.index = match.index + match[DATE_GROUP].length;
Expand All @@ -50,7 +54,7 @@ export default class DEMonthNameLittleEndianParser extends AbstractParserWithWor
}

if (match[DATE_TO_GROUP]) {
const endDate = parseInt(match[DATE_TO_GROUP]);
const endDate = parseOrdinalNumberPattern(match[DATE_TO_GROUP]);

result.end = result.start.clone();
result.end.assign("day", endDate);
Expand Down
32 changes: 22 additions & 10 deletions src/locales/de/parsers/DETimeUnitRelativeFormatParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,47 @@ import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/
import { reverseTimeUnits } from "../../../utils/timeunits";
import { matchAnyPattern } from "../../../utils/pattern";

export default class DETimeUnitAgoFormatParser extends AbstractParserWithWordBoundaryChecking {
export default class DETimeUnitRelativeFormatParser extends AbstractParserWithWordBoundaryChecking {
constructor() {
super();
}

innerPattern(): RegExp {
return new RegExp(
`(?:\\s*((?:nächste|kommende|folgende|letzte|vergangene|vorige|vor(?:her|an)gegangene)(?:s|n|m|r)?|vor|in)\\s*)?` +
`(?:((?:(?:über)(?:über)nächste|kommende|folgende|(?:vor)?(?:vor)?letzte|vergangene|vorige|vor(?:her|an)gegangene)(?:s|n|m|r)?|vor|in|nach|ab\\sjetzt(?:\\sin)?)\\s*)?` +
`(${NUMBER_PATTERN})?` +
`(?:\\s*(nächste|kommende|folgende|letzte|vergangene|vorige|vor(?:her|an)gegangene)(?:s|n|m|r)?)?` +
`\\s*(${matchAnyPattern(TIME_UNIT_DICTIONARY)})`,
`\\s*(${matchAnyPattern(TIME_UNIT_DICTIONARY)}\\b)` +
`(?:\\s*(früher|frueher|vorher|davor|zuvor|später|spaeter|danach|nachher|ab\\sjetzt))?`,
"i"
);
}

innerExtract(context: ParsingContext, match: RegExpMatchArray) {
const num = match[2] ? parseNumberPattern(match[2]) : 1;
const unit = TIME_UNIT_DICTIONARY[match[4].toLowerCase()];
let timeUnits = {};
timeUnits[unit] = num;

// Modifier
let modifier = match[1] || match[3] || "";
let modifier = match[1] || match[3] || match[5] || "";
modifier = modifier.toLowerCase();
if (!modifier) {
return;
}

if (/vor/.test(modifier) || /letzte/.test(modifier) || /vergangen/.test(modifier)) {
let num = match[2] ? parseNumberPattern(match[2]) : 1;
if (/vorvorletzte/.test(modifier) || /überübernächste/.test(modifier)) {
num += 2;
} else if (/vorletzte/.test(modifier) || /übernächste/.test(modifier)) {
num += 1;
}

const unit = TIME_UNIT_DICTIONARY[match[4].toLowerCase()];
let timeUnits = {};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: first, I'd rename num to value and I think let timeUnits = { unit: num }; is clearer to understand.

timeUnits[unit] = num;

if (
/vor/.test(modifier) ||
/letzte/.test(modifier) ||
/vergangen/.test(modifier) ||
/fr(?:ü|ue)her/.test(modifier)
) {
timeUnits = reverseTimeUnits(timeUnits);
}

Expand Down
43 changes: 43 additions & 0 deletions test/de/de_month_name_little_endian.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ test("Test - Single expression", function () {
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 12));
});

testSingleCase(chrono.de, "Die Deadline ist am zehnten August", new Date(2012, 7, 10), (result) => {
expect(result.index).toBe(17);
expect(result.text).toBe("am zehnten August");

expect(result.start).not.toBeNull();
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(8);
expect(result.start.get("day")).toBe(10);

expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 12));
});

testSingleCase(chrono.de, "Die Deadline ist am Dienstag, den 10. Januar", new Date(2012, 7, 10), (result) => {
expect(result.index).toBe(17);
expect(result.text).toBe("am Dienstag, den 10. Januar");
Expand Down Expand Up @@ -167,6 +179,25 @@ test("Test - Range expression", function () {
expect(result.end).toBeDate(new Date(2012, 10 - 1, 22, 12));
});

testSingleCase(chrono.de, "zehnter bis zweiundzwanzigster Oktober 2012", new Date(2012, 7, 10), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("zehnter bis zweiundzwanzigster Oktober 2012");

expect(result.start).not.toBeNull();
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(10);
expect(result.start.get("day")).toBe(10);

expect(result.start).toBeDate(new Date(2012, 10 - 1, 10, 12));

expect(result.end).not.toBeNull();
expect(result.end.get("year")).toBe(2012);
expect(result.end.get("month")).toBe(10);
expect(result.end.get("day")).toBe(22);

expect(result.end).toBeDate(new Date(2012, 10 - 1, 22, 12));
});

testSingleCase(chrono.de, "10. Oktober - 12. Dezember", new Date(2012, 7, 10), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("10. Oktober - 12. Dezember");
Expand Down Expand Up @@ -216,6 +247,18 @@ test("Test - Range expression", function () {

expect(result.start).toBeDate(new Date(2012, 1 - 1, 10, 12));
});

testSingleCase(chrono.de, "zehnter jänner 2012", new Date(2012, 7, 10), (result) => {
expect(result.start).not.toBeNull();
expect(result.start.get("year")).toBe(2012);
expect(result.start.get("month")).toBe(1);
expect(result.start.get("day")).toBe(10);

expect(result.index).toBe(0);
expect(result.text).toBe("zehnter jänner 2012");

expect(result.start).toBeDate(new Date(2012, 1 - 1, 10, 12));
});
});

test("Test - Combined expression", function () {
Expand Down
Loading