forked from facebookarchive/duckling_old
/
time.clj
384 lines (288 loc) · 9.63 KB
/
time.clj
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
(
"intersect"
[(dim :time #(not (:latent %))) (dim :time #(not (:latent %)))] ; sequence of two tokens with a time dimension
(intersect %1 %2)
; mostly for January 12, 2005
; this is a separate rule, because commas separate very specific tokens
; so we want this rule's classifier to learn this
"intersect by \",\""
[(dim :time #(not (:latent %))) #"," (dim :time #(not (:latent %)))] ; sequence of two tokens with a time fn
(intersect %1 %3)
;;;;;;;;;;;;;;;;;;;
;; Named things
"named-day"
#"星期一|周一|礼拜一|禮拜一|週一"
(day-of-week 1)
"named-day"
#"星期二|周二|礼拜二|禮拜二|週二"
(day-of-week 2)
"named-day"
#"星期三|周三|礼拜三|禮拜三|週三"
(day-of-week 3)
"named-day"
#"星期四|周四|礼拜四|禮拜四|週四"
(day-of-week 4)
"named-day"
#"星期五|周五|礼拜五|禮拜五|週五"
(day-of-week 5)
"named-day"
#"星期六|周六|礼拜六|禮拜六|週六"
(day-of-week 6)
"named-day"
#"星期日|星期天|礼拜天|周日|禮拜天|週日"
(day-of-week 7)
"named-month"
#"一月份?"
(month 1)
"named-month"
#"二月份?"
(month 2)
"named-month"
#"三月份?"
(month 3)
"named-month"
#"四月份?"
(month 4)
"named-month"
#"五月份?"
(month 5)
"named-month"
#"六月份?"
(month 6)
"named-month"
#"七月份?"
(month 7)
"named-month"
#"八月份?"
(month 8)
"named-month"
#"九月份?"
(month 9)
"named-month"
#"十月份?"
(month 10)
"named-month"
#"十一月份?"
(month 11)
"named-month"
#"十二月份?"
(month 12)
; Holiday TODO: check online holidays
; or define dynamic rule (last thursday of october..)
"new year's day"
#"元旦[节節]?"
(month-day 1 1)
"valentine's day"
#"情人[节節]"
(month-day 2 14)
"women's day"
#"[妇婦]女[节節]"
(month-day 3 8)
"labor day"
#"劳动节|勞動節"
(month-day 5 1)
"children's day"
#"[儿兒]童[节節]"
(month-day 6 1)
"army's day"
#"建(军节|軍節)"
(month-day 8 1)
"national day"
#"(国庆|國慶)[节節]?"
(month-day 10 1)
"christmas"
#"(圣诞|聖誕)[节節]?"
(month-day 12 25)
; Including cycles
"now"
#"现在|此时|此刻|当前|現在|此時|當前"
(cycle-nth :second 0)
"the day before yesterday"
#"前天"
(cycle-nth :day -2)
"yesterday"
#"昨天"
(cycle-nth :day -1)
"today"
#"今天"
(cycle-nth :day 0)
"tomorrow"
#"明天"
(cycle-nth :day 1)
"the day after tomorrow"
#"后天|後天"
(cycle-nth :day 2)
;;
;; This, Next, Last
;; assumed to be strictly in the future:
;; "this Monday" => next week if today in Monday
"this|next <day-of-week>"
[#"(?i)今|明|下[个|個]?" {:form :day-of-week}]
(pred-nth-not-immediate %2 0)
;; for other preds, it can be immediate:
;; "this month" => now is part of it
; See also: cycles in en.cycles.clj
"this <time>"
[#"(?i)今|这|這" (dim :time)]
(pred-nth %2 0)
"next <time>"
[#"(?i)明|下[个|個]?" (dim :time)] ;下[个|個]?
(pred-nth %2 1)
"last <time>"
[#"(?i)去" (dim :time)]
(pred-nth %2 -1)
;;Ordinals
"nth <time> of <time>"
[(dim :time) (dim :ordinal) (dim :time)]
(pred-nth (intersect %1 %3) (dec (:value %2)))
"nth <time> of <time>"
[(dim :time) #"的" (dim :ordinal) (dim :time)]
(pred-nth (intersect %1 %4) (dec (:value %3)))
;specific for year in chinese
"last year"
#"去年"
(cycle-nth :year -1)
"this year"
#"今年"
(cycle-nth :year 0)
"next year"
#"明年"
(cycle-nth :year 1)
"this <day-of-week>" ; assumed to be in the future
[#"这|這" {:form :named-day}]
(pred-nth %2 0)
"last tuesday, last july"
[#"上" (dim :time)]
(pred-nth %2 -1)
; "next tuesday, next july"
; [#"下" (dim :time)]
; (pred-nth %2 1)
;; part of day (morning, evening...)
"morning" ;; TODO "3am this morning" won't work since morning starts at 4...
[#"早上|早晨"]
(assoc (interval (hour 4 false) (hour 12 false) false) :form :part-of-day :latent true)
"afternoon"
[#"下午|中午"]
(assoc (interval (hour 12 false) (hour 19 false) false) :form :part-of-day :latent true)
"evening|night"
[#"晚上|晚间"]
(assoc (interval (hour 18 false) (hour 0 false) false) :form :part-of-day :latent true)
"<dim time> <part-of-day>" ; since "morning" "evening" etc. are latent, general time+time is blocked
[(dim :time) {:form :part-of-day}]
(intersect %1 %2)
"day of month (numeric with day symbol)"
[(dim :number) #"号|號|日"]
(assoc (day-of-month (:value %1)) :latent true :form :day-of-month)
"month (numeric with month symbol)" ;TOBECHECKED
[(integer 1 12) #"月"]
(assoc (month (:value %1)) :latent true :form :named-month)
"year (numeric with year symbol)"
[(integer 1000 9999) #"年"]
(year (:value %1))
"absorption of , after named day"
[{:form :named-day} #","]
%1
"<named-month> <day-of-month>" ; 三月十二号
[{:form :named-month} {:form :day-of-month}]
(intersect %1 %2)
; ;; Hours and minutes (absolute time)
"<integer> (latent time-of-day)"
(integer 0 23)
(assoc (hour (:value %1) true)
:form :time-of-day
:latent true)
"last night"
[#"昨晚|昨天晚上"] ;; special form in Chinese
(assoc (intersect (cycle-nth :day -1)
(interval (hour 18 false) (hour 0 false) false))
:form :part-of-day) ; no :latent
"tonight"
[#"今晚|今天晚上"]
(assoc (intersect (cycle-nth :day 0)
(interval (hour 18 false) (hour 0 false) false))
:form :part-of-day) ; no :latent
"tomorrow night"
[#"明晚|明天晚上"] ;; special form in Chinese
(assoc (intersect (cycle-nth :day 1)
(interval (hour 18 false) (hour 0 false) false))
:form :part-of-day) ; no :latent
"in|during the <part-of-day>" ;; removes latent
[{:form :part-of-day} #"点|點"]
(dissoc %1 :latent)
"<time-of-day> o'clock"
[{:form :time-of-day} #"點|点"]
(dissoc %1 :latent)
"hh:mm (time-of-day)"
#"(?i)((?:[01]?\d)|(?:2[0-3])):([0-5]\d)"
(hour-minute (Integer/parseInt (first (:groups %1)))
(Integer/parseInt (second (:groups %1)))
true)
"hhmm (military time-of-day)"
#"(?i)((?:[01]?\d)|(?:2[0-3]))([0-5]\d)"
(-> (hour-minute (Integer/parseInt (first (:groups %1)))
(Integer/parseInt (second (:groups %1)))
false) ; not a 12-hour clock)
(assoc :latent true))
"<time-of-day> am|pm"
[{:form :time-of-day} #"(?i)([ap])(\s|\.)?m?\.?"]
(let [[p meridiem] (if (= "a" (-> %2 :groups first .toLowerCase))
[[(hour 0) (hour 12) false] :am]
[[(hour 12) (hour 0) false] :pm])]
(-> (intersect %1 (apply interval p))
(assoc :form :time-of-day)))
"<part-of-day> <dim time>" ; in Chinese, we say `早上8点`, instead of `8点早上`
[{:form :part-of-day} (dim :time)]
(intersect %1 %2)
"noon"
#"中午"
(hour 12 false)
"midnight"
#"午夜|凌晨"
(hour 0 false)
"quarter (relative minutes)"
#"一刻"
{:relative-minutes 15}
"half (relative minutes)"
#"半"
{:relative-minutes 30}
"number (as relative minutes)"
(integer 1 59)
{:relative-minutes (:value %1)}
"relative minutes after|past <integer> (hour-of-day)"
[(dim :time :full-hour) #"点|點" #(:relative-minutes %)]
(hour-relativemin (:full-hour %1) (:relative-minutes %3) true)
"relative minutes to|till|before <integer> (hour-of-day)"
[(dim :time :full-hour) #"[点點]差" #(:relative-minutes %)]
(hour-relativemin (:full-hour %1) (- (:relative-minutes %3)) true)
; special forms for midnight and noon
"relative minutes to|till|before noon|midnight"
[#(:for-relative-minutes %) #"差" #(:relative-minutes %)]
(hour-relativemin (:value %1) true (- (:relative-minutes %3)))
"relative minutes after|past noon|midnight"
[#(:for-relative-minutes %) #"过" #(:relative-minutes %)]
(hour-relativemin (:value %1) true (:relative-minutes %3))
; ;; formatted
"mm/dd/yyyy"
#"(0?[1-9]|1[0-2])/(3[01]|[12]\d|0?[1-9])/(\d{2,4})"
(parse-dmy (second (:groups %1)) (first (:groups %1)) (nth (:groups %1) 2) true)
"yyyy-mm-dd"
#"(\d{2,4})-(0?[1-9]|1[0-2])-(3[01]|[12]\d|0?[1-9])"
(parse-dmy (nth (:groups %1) 2) (second (:groups %1)) (first (:groups %1)) true)
"mm/dd"
#"(0?[1-9]|1[0-2])/(3[01]|[12]\d|0?[1-9])"
(parse-dmy (second (:groups %1)) (first (:groups %1)) nil true)
; Other intervals: week-end, seasons
"week-end"
#"周末|週末"
(interval (intersect (day-of-week 5) (hour 18 false))
(intersect (day-of-week 1) (hour 0 false))
false)
;; Time zones
"timezone"
#"(?i)(YEKT|YEKST|YAPT|YAKT|YAKST|WT|WST|WITA|WIT|WIB|WGT|WGST|WFT|WEZ|WET|WESZ|WEST|WAT|WAST|VUT|VLAT|VLAST|VET|UZT|UYT|UYST|UTC|ULAT|TVT|TMT|TLT|TKT|TJT|TFT|TAHT|SST|SRT|SGT|SCT|SBT|SAST|SAMT|RET|PYT|PYST|PWT|PT|PST|PONT|PMST|PMDT|PKT|PHT|PHOT|PGT|PETT|PETST|PET|PDT|OMST|OMSST|NZST|NZDT|NUT|NST|NPT|NOVT|NOVST|NFT|NDT|NCT|MYT|MVT|MUT|MST|MSK|MSD|MMT|MHT|MEZ|MESZ|MDT|MAWT|MART|MAGT|MAGST|LINT|LHST|LHDT|KUYT|KST|KRAT|KRAST|KGT|JST|IST|IRST|IRKT|IRKST|IRDT|IOT|IDT|ICT|HOVT|HNY|HNT|HNR|HNP|HNE|HNC|HNA|HLV|HKT|HAY|HAT|HAST|HAR|HAP|HAE|HADT|HAC|HAA|GYT|GST|GMT|GILT|GFT|GET|GAMT|GALT|FNT|FKT|FKST|FJT|FJST|ET|EST|EGT|EGST|EET|EEST|EDT|ECT|EAT|EAST|EASST|DAVT|ChST|CXT|CVT|CST|COT|CLT|CLST|CKT|CHAST|CHADT|CET|CEST|CDT|CCT|CAT|CAST|BTT|BST|BRT|BRST|BOT|BNT|AZT|AZST|AZOT|AZOST|AWST|AWDT|AST|ART|AQTT|ANAT|ANAST|AMT|AMST|ALMT|AKST|AKDT|AFT|AEST|AEDT|ADT|ACST|ACDT)"
{:dim :timezone
:value (-> %1 :groups first .toUpperCase)}
"<time> timezone"
[(dim :time) (dim :timezone)]
(assoc %1 :timezone (:value %2))
)