-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
86 lines (82 loc) · 2.14 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
any,
createLanguage,
eof,
manyTill,
map,
skip1,
seq,
Parser,
optional,
space,
anyChar,
} from "combine/mod.ts";
import { word, __, dot } from "./src/common.ts";
import { Entity } from "./src/Entity.ts";
import { Quantity, QuantityEntity } from "./src/Quantity.ts";
import { Range } from "./src/Range.ts";
import { Temperature, TemperatureEntity } from "./src/Temperature.ts";
import { Time, TimeEntity } from "./src/Time.ts";
import { Location, LocationEntity } from "./src/Location.ts";
import { URL, URLEntity } from "./src/URL.ts";
import { Email, EmailEntity } from "./src/Email.ts";
import { Institution, InstitutionEntity } from "./src/Institution.ts";
import { Language, LanguageEntity } from "./src/Language.ts";
export type AnyEntity =
| Entity<unknown, unknown>
| TemperatureEntity
| TimeEntity
| QuantityEntity
| LocationEntity
| URLEntity
| EmailEntity
| InstitutionEntity
| LanguageEntity;
type DucklingLanguage = {
Entity: Parser<AnyEntity>;
Unstructured: Parser<string>;
extract: Parser<(AnyEntity | null)[]>;
};
export const Duckling = (
parsers: Parser<AnyEntity>[] = [
Range.parser,
Time.parser,
Temperature.parser,
Quantity.parser,
Location.parser,
URL.parser,
Email.parser,
Institution.parser,
Language.parser,
]
) =>
createLanguage<DucklingLanguage>({
Entity: () => any(...parsers),
Unstructured: () => any(dot(word), __(word), space()),
extract: (s) =>
map(
seq(
optional(space()),
map(
manyTill(
any(s.Entity, skip1(s.Unstructured), skip1(anyChar())),
skip1(eof())
),
([...matches]) => {
return matches.filter((m) => !!m);
}
)
),
([, res]) => res
),
});
export * from "./src/Entity.ts";
export * from "./src/Quantity.ts";
export * from "./src/Range.ts";
export * from "./src/Temperature.ts";
export * from "./src/Time.ts";
export * from "./src/Location.ts";
export * from "./src/URL.ts";
export * from "./src/Email.ts";
export * from "./src/Institution.ts";
export * from "./src/Language.ts";