Skip to content

Commit

Permalink
feat(range): add year era range
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudiuCeia committed Jul 21, 2022
1 parent c87aca1 commit ce36327
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 15 deletions.
77 changes: 65 additions & 12 deletions src/Range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { __, dot, EntityLanguage } from "./common.ts";
import { ent, Entity } from "./Entity.ts";
import { Quantity, QuantityEntity } from "./Quantity.ts";
import { Temperature, TemperatureEntity } from "./Temperature.ts";
import { Time, TimeEntity } from "./Time.ts";
import { time, Time, TimeEntity } from "./Time.ts";

export type RangeEntity<E extends Entity<unknown, unknown>> = Entity<
"range",
Expand All @@ -38,6 +38,7 @@ type RangeEntityLanguage = EntityLanguage<
{
TemperatureRange: Parser<RangeEntity<TemperatureEntity>>;
TimeRange: Parser<RangeEntity<TimeEntity>>;
YearRange: Parser<RangeEntity<TimeEntity>>;
},
RangeEntity<QuantityEntity | TemperatureEntity | TimeEntity>
>;
Expand Down Expand Up @@ -80,19 +81,71 @@ export const Range = createLanguage<RangeEntityLanguage>({
),
TimeRange: () =>
dot(
map(
seq(
__(str("from")),
optional(__(str("the"))),
Time.parser,
__(either(str("until"), str("to"))),
optional(__(str("the"))),
Time.parser
any(
map(
seq(
__(str("from")),
optional(__(str("the"))),
Time.parser,
__(either(str("until"), str("to"))),
optional(__(str("the"))),
Time.parser
),
([, , low, , , high], b, a) => {
return range(
{
min: low,
max: high,
},
b,
a
);
}
),
([, , low, , , high], b, a) => {
map(
seq(
__(str("between")),
optional(__(str("the"))),
Time.parser,
__(str("and")),
optional(__(str("the"))),
Time.parser
),
([, , low, , , high], b, a) => {
return range(
{
min: low,
max: high,
},
b,
a
);
}
)
)
),
YearRange: () =>
dot(
map(
seq(__(str("between")), Quantity.parser, __(str("and")), Time.YearEra),
([, low, , high], b, a) => {
return range(
{
min: low,
min: time(
{
when: `${low.value.amount} ${high.value.era}`,
grain: "era",
era: high.value.era,
},
{
text: b.text,
index: low.start,
},
{
text: b.text,
index: low.end,
}
),
max: high,
},
b,
Expand All @@ -101,5 +154,5 @@ export const Range = createLanguage<RangeEntityLanguage>({
}
)
),
parser: (s) => any(s.TemperatureRange, s.TimeRange),
parser: (s) => any(s.TemperatureRange, s.TimeRange, s.YearRange),
});
23 changes: 20 additions & 3 deletions src/Time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,28 @@ export type TimeEntity = Entity<
{
when: string | [string, string];
grain: TimeGranularity;
era: "BCE" | "CE";
}
>;

const time = (
value: TimeEntity["value"],
export type NoEraTimeEntityValue = Omit<TimeEntity["value"], "era"> & {
era?: TimeEntity["value"]["era"]
};

export const time = (
value: NoEraTimeEntityValue,
before: Context,
after: Context
): TimeEntity => {
return ent(value, "time", before, after);
return ent(
{
...value,
era: value.era || "CE"
},
"time",
before,
after
);
};

type TimeEntityLanguage = EntityLanguage<
Expand Down Expand Up @@ -352,6 +365,7 @@ export const Time = createLanguage<TimeEntityLanguage>({
maybeEra || ""
}`,
grain: grain as TimeGranularity,
era: (maybeEra === "BCE" || maybeEra === "BC") ? "BCE" : "CE",
},
b,
a
Expand Down Expand Up @@ -437,6 +451,7 @@ export const Time = createLanguage<TimeEntityLanguage>({
{
when: `${partial} ${era}`,
grain: "era",
era: era === "BCE" || era === "BC" ? "BCE" : "CE",
},
b,
a
Expand All @@ -450,6 +465,7 @@ export const Time = createLanguage<TimeEntityLanguage>({
{
when: `${full} ${era}`,
grain: "era",
era: era === "BCE" || era === "BC" ? "BCE" : "CE",
},
b,
a
Expand All @@ -463,6 +479,7 @@ export const Time = createLanguage<TimeEntityLanguage>({
{
when: `${year.value.amount} ${era}`,
grain: "era",
era: era === "BCE" || era === "BC" ? "BCE" : "CE",
},
b,
a
Expand Down
46 changes: 46 additions & 0 deletions tests/Range.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Deno.test("Time range", () => {
start: 64,
text: "5th of June 2022",
value: {
era: "CE",
grain: "day",
when: "2022-06-04T21:00:00.000Z",
},
Expand All @@ -33,6 +34,7 @@ Deno.test("Time range", () => {
start: 42,
text: "1st of June ",
value: {
era: "CE",
grain: "day",
when: "2022-05-31T21:00:00.000Z",
},
Expand Down Expand Up @@ -100,3 +102,47 @@ Deno.test("Temperature range", () => {
]);
}
});

Deno.test("Year range", () => {
const res = Duckling.extract({
text: `Developed in the period between 2700 and 2300 BCE in Sumer`,
index: 0,
});

assertEquals(res.success, true);

if (res.success) {
assertEquals(res.value, [
{
end: 50,
kind: "range",
start: 24,
text: "between 2700 and 2300 BCE ",
value: {
max: {
end: 50,
kind: "time",
start: 41,
text: "2300 BCE ",
value: {
era: "BCE",
grain: "era",
when: "2300 BCE",
},
},
min: {
end: 37,
kind: "time",
start: 32,
text: "2700 ",
value: {
era: "BCE",
grain: "era",
when: "2700 BCE",
},
},
},
},
]);
}
});
16 changes: 16 additions & 0 deletions tests/Time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Deno.test("UnspecifiedGrainAmount", () => {
start: 29,
text: "centuries",
value: {
era: "CE",
grain: "centuries",
when: "centuries",
},
Expand All @@ -41,6 +42,7 @@ Deno.test("DayOfWeek", () => {
start: 26,
text: "Monday",
value: {
era: "CE",
grain: "day",
when: "Monday",
},
Expand All @@ -51,6 +53,7 @@ Deno.test("DayOfWeek", () => {
start: 36,
text: "Friday",
value: {
era: "CE",
grain: "day",
when: "Friday",
},
Expand Down Expand Up @@ -88,6 +91,7 @@ Deno.test("GrainQuantity", () => {
start: 18,
text: "5 days",
value: {
era: "CE",
grain: "days",
when: "5 days",
},
Expand All @@ -98,6 +102,7 @@ Deno.test("GrainQuantity", () => {
start: 46,
text: "51615 seconds",
value: {
era: "CE",
grain: "seconds",
when: "51615 seconds",
},
Expand Down Expand Up @@ -126,6 +131,7 @@ Deno.test("Relative", () => {
start: 33,
text: "4 days ago",
value: {
era: "CE",
grain: "days",
when: "-4 days",
},
Expand All @@ -136,6 +142,7 @@ Deno.test("Relative", () => {
start: 45,
text: "Last week ",
value: {
era: "CE",
grain: "week",
when: "-1 week",
},
Expand All @@ -146,6 +153,7 @@ Deno.test("Relative", () => {
start: 114,
text: "past year ",
value: {
era: "CE",
grain: "year",
when: "-1 year",
},
Expand All @@ -156,6 +164,7 @@ Deno.test("Relative", () => {
start: 159,
text: "next\n 2 years\n ",
value: {
era: "CE",
grain: "years",
when: "2 years",
},
Expand All @@ -180,6 +189,7 @@ Deno.test("PartialDateMonthYear numeric", () => {
start: 17,
text: "12/2022",
value: {
era: "CE",
grain: "day",
when: "2022-01-11T22:00:00.000Z",
},
Expand All @@ -204,6 +214,7 @@ Deno.test("PartialDateMonthYear literal", () => {
start: 29,
text: "June 2022",
value: {
era: "CE",
grain: "day",
when: "2022-05-31T21:00:00.000Z",
},
Expand All @@ -228,6 +239,7 @@ Deno.test("PartialDateDayMonth literal", () => {
start: 17,
text: "12th of June",
value: {
era: "CE",
grain: "day",
when: "2022-06-11T21:00:00.000Z",
},
Expand All @@ -252,6 +264,7 @@ Deno.test("FullDate", () => {
start: 17,
text: "1st of June 2023",
value: {
era: "CE",
grain: "day",
when: "2023-05-31T21:00:00.000Z",
},
Expand All @@ -276,6 +289,7 @@ Deno.test("False positive time", () => {
start: 0,
text: "6/2022",
value: {
era: "CE",
grain: "day",
when: "2022-01-05T22:00:00.000Z",
},
Expand Down Expand Up @@ -309,6 +323,7 @@ Deno.test("Era", () => {
start: 27,
text: "100 BC",
value: {
era: "BCE",
grain: "era",
when: "100 BC",
},
Expand All @@ -333,6 +348,7 @@ Deno.test("QualifiedGrain", () => {
start: 7,
text: "5th century BC ",
value: {
era: "BCE",
grain: "century",
when: "5th century BC",
},
Expand Down

0 comments on commit ce36327

Please sign in to comment.