Skip to content

Commit

Permalink
Merge pull request #511 from yuichkun/ja/add-support-for-hiragana
Browse files Browse the repository at this point in the history
[JA] Add support for Hiragana
  • Loading branch information
wanasit authored Apr 14, 2023
2 parents 4886988 + bbb1e2e commit bd838bd
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
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(text: string) {
switch (text) {
case "きょう":
return "今日";
case "とうじつ":
return "当日";
case "きのう":
return "昨日";
case "あした":
return "明日";
case "こんや":
return "今夜";
case "こんゆう":
return "今夕";
case "こんばん":
return "今晩";
case "けさ":
return "今朝";
default:
return text;
}
}

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
63 changes: 50 additions & 13 deletions test/ja/ja_casual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,63 @@ 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("今日");

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.ja, "きょう感じたことを忘れずに", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("きょう");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 12));
});

testSingleCase(chrono.ja, "昨日の全国観測値ランキング", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.index).toBe(0);
expect(result.text).toBe("昨日");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 9, 12));
});
testSingleCase(chrono.ja, "きのうの全国観測値ランキング", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("きのう");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 9, 12));
});

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);
testSingleCase(chrono.ja, "明日の天気は晴れです", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("明日");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 11, 12));
});
testSingleCase(chrono.ja, "あしたの天気は晴れです", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("あした");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 11, 12));
});

expect(result.start).toBeDate(new Date(2012, 8 - 1, 9, 12));
testSingleCase(chrono.ja, "今夜には雨が降るでしょう", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("今夜");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 22));
});
testSingleCase(chrono.ja, "こんやには雨が降るでしょう", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("こんや");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 22));
});
testSingleCase(chrono.ja, "今夕には雨が降るでしょう", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("今夕");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 22));
});
testSingleCase(chrono.ja, "こんゆうには雨が降るでしょう", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("こんゆう");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 22));
});
testSingleCase(chrono.ja, "今晩には雨が降るでしょう", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("今晩");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 22));
});
testSingleCase(chrono.ja, "こんばんには雨が降るでしょう", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("こんばん");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 22));
});

testSingleCase(chrono.ja, "今朝食べたパンは美味しかった", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("今朝");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 6));
});
testSingleCase(chrono.ja, "けさ食べたパンは美味しかった", new Date(2012, 8 - 1, 10, 12), (result) => {
expect(result.text).toBe("けさ");
expect(result.start).toBeDate(new Date(2012, 8 - 1, 10, 6));
});
});

0 comments on commit bd838bd

Please sign in to comment.