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

[JA] Add support for Hiragana #511

Merged
merged 5 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 25 additions & 2 deletions src/locales/ja/parsers/JPCasualDateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,38 @@ import dayjs from "dayjs";
import { Meridiem } from "../../../index";
import * as references from "../../../common/casualReferences";

const PATTERN = /今日|当日|昨日|明日|今夜|今夕|今晩|今朝/i;
const PATTERN = /今日|きょう|当日|とうじつ|昨日|きのう|明日|あした|今夜|こんや|今夕|こんゆう|今晩|こんばん|今朝|けさ/i;

function normalizeTextToKanji(str: string) {
Copy link
Owner

Choose a reason for hiding this comment

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

Nit: text not str, because your function name is normalizeTextToKanji, not normalizeStrToKanji.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree!

switch (str) {
case "きょう":
return "今日";
case "とうじつ":
return "当日";
case "きのう":
return "昨日";
case "あした":
return "明日";
case "こんや":
return "今夜";
case "こんゆう":
return "今夕";
case "こんばん":
return "今晩";
case "けさ":
return "今朝";
default:
return str;
}
}

export default class JPCasualDateParser implements Parser {
pattern() {
return PATTERN;
}

extract(context: ParsingContext, match: RegExpMatchArray) {
const text = match[0];
const text = normalizeTextToKanji(match[0]);

const date = dayjs(context.refDate);
const components = context.createParsingComponents();
Expand Down
99 changes: 81 additions & 18 deletions test/ja/ja_casual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,90 @@ import * as chrono from "../../src/";
import { testSingleCase } from "../test_util";

test("Test - Single Expression", function () {
testSingleCase(chrono.ja, "今日感じたことを忘れずに", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("今日");
function testToday(pattern: string) {
Copy link
Owner

@wanasit wanasit Apr 9, 2023

Choose a reason for hiding this comment

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

Could you not use a function like this and repeat testSingleCase() for each case?

I know it's a lot of duplicated code, but we want to ensure the tests are really straightforward. (e.g. if the expect fails it would be more difficult to see where we break the test).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we want to ensure the tests are really straightforward

I absolutely agree with you.
I'll make the tests like so and maybe with less assertions like you mentioned here

testSingleCase(chrono.ja, `${pattern}感じたことを忘れずに`, new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe(pattern);
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));
Copy link
Owner

Choose a reason for hiding this comment

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

I think, previously, we test too many things. e.g. only expect(result.start).toBeDate() and maybe expect(result.text) is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do so accordingly 👍

});
}

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);
testToday("今日");
testToday("きょう");

expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 12));
});
function testYesterday(pattern: string) {
testSingleCase(chrono.ja, `${pattern}の全国観測値ランキング`, new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe(pattern);

testSingleCase(chrono.ja, "昨日の全国観測値ランキング", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("昨日");
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(9);

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(9);
expect(result.start).toBeDate(new Date(2012, 8 - 1, 9, 12));
});
}
testYesterday("昨日");
testYesterday("きのう");

expect(result.start).toBeDate(new Date(2012, 8 - 1, 9, 12));
});
function testTomorrow(pattern: string) {
testSingleCase(chrono.ja, `${pattern}の天気は晴れです`, new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe(pattern);

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(11);

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

testTomorrow("明日");
testTomorrow("あした");

function testTonight(pattern: string) {
testSingleCase(chrono.ja, `${pattern}には雨が降るでしょう`, new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe(pattern);

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.get("meridiem")).toBe(chrono.Meridiem.PM);

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

testTonight("今夜");
testTonight("こんや");
testTonight("今夕");
testTonight("こんゆう");
testTonight("今晩");
testTonight("こんばん");

function testMorning(pattern: string) {
testSingleCase(chrono.ja, `${pattern}食べたパンは美味しかった`, new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe(pattern);

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.get("meridiem")).toBe(chrono.Meridiem.AM);

expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 6));
});
}
testMorning("今朝");
testMorning("けさ");
});