-
Notifications
You must be signed in to change notification settings - Fork 2
/
tempexp_normalizer.py
1778 lines (1567 loc) · 57.8 KB
/
tempexp_normalizer.py
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
tempexp_normalizer.py
Copyright (c) 2012, gnTEAM, School of Computer Science, University of Manchester.
All rights reserved. This program and the accompanying materials
are made available under the terms of the GNU General Public License.
authors: Michele Filannino
email: [email protected]
TempExp normaliser is a piece of software that provide the TimeML type and
value attributes for each temporal expression given in input.
This work is an extension of TRIOS normaliser. See the next comment.
For details, see www.cs.man.ac.uk/~filannim/
'''
#!/usr/bin/python
'''This program takes a temporal expression and returns the normalized type and
value according to TimeML scheme.
This is a regular expression based naive program, but this normalizer had the
second best performance in TempEval 2010 (temporal evaluation competition). The
reason for sharing this program is to help others start from this basic program
and extend it to get better performance. It handles basic temporal expressions
decently, so most of the people don't need to modify it. Any modification will
be highly appreciated. Give me your updated copy, I will add your modification
with credit.
Usage:
call the function get_timex_value with your "temporal expression" and
"document creation time" as parameters. See get_date() function to see the
document creation time format and see examples in the bottom of the page to
see usage.
To see the sample output, run:
>> python tempexp-normalizer.py
Output format:
the fuction outputs a tuple, e.g.
('last friday', 'DATE', '2010-01-22', 'DOW')
the first entry is the temporal expression, next entry is TYPE (DATE, TIME,
SET, DURATION) according to TimeML and the next entry is normalized VALUE
according to TimeML and the last entry is for debugging.
TimeML specification can be found in:
http://www.timeml.org/site/publications/specs.html.
This program used specification:
http://www.timeml.org/site/publications/timeMLdocs/annguide_1.2.pdf
temporal expressions referred as Timex3 in TimeML
As mentioned already, this program takes temporal expressions as input. If you
need the program to extract the temporal expressions from text then this is not
the program you want. After using a program to extract temporal expression this
program gives the normalized value of temporal expression with respect to
document creation time. I will release my program for extracting temporal
expression from text in future. If you need it before I release, feel free to
let me know.
Developed by:
Naushad UzZaman (naushad AT cs.rochester.edu)
Michele Filannino (filannim AT cs.manchester.ac.uk)
Feel free to contact for any help. :)
'''
import re
import os
import sys
import commands
import math
from datetime import date
##################################################################
############ PROCESS TIMEX VALUE ###########
#
### DATE RELATED FUNCTIONS ####
#
# usage
# generate_week_range(1998) to get week range for a particular year,
# specifically the CREATION DATE
# day_of_week(day, month, year) to get the day (Sunday, Monday, ..) for a
# specific date
# get_dows_date_from_date('Sunday', 31, 12, 2009, 'next')
# get_dows_date_from_date('Sunday', 26, 1, 2010, 'prev')
dow = {}
dow[0] = "sunday"
dow[1] = "monday"
dow[2] = "tuesday"
dow[3] = "wednesday"
dow[4] = "thursday"
dow[5] = "friday"
dow[6] = "saturday"
dow_reverse = {}
dow_reverse["sunday"] = 0
dow_reverse["monday"] = 1
dow_reverse["tuesday"] = 2
dow_reverse["wednesday"] = 3
dow_reverse["thursday"] = 4
dow_reverse["friday"] = 5
dow_reverse["saturday"] = 6
days_in_month = {}
def get_today():
today = date.today()
return today.year, today.month, today.day
def day_of_week(day, month, year):
a = math.floor((14-month)/12)
y = year - a
m = month + 12 * a - 2
d = (day + y + math.floor(y / 4) - math.floor(y / 100) +
math.floor(y / 400) + math.floor((31 * m) / 12)) % 7
return int(d)
def isleapyear(year):
year = int(year)
if 0 == year % 4 and (0 != year % 100 or 0 == year % 400):
return 'true'
return 'false'
def init_days_in_month(year):
days_in_month = {}
days_in_month[1] = 31 # jan
if isleapyear(year) == 'true':
days_in_month[2] = 29
else:
days_in_month[2] = 28
days_in_month[3] = 31 # mar
days_in_month[4] = 30 # apr
days_in_month[5] = 31 # may
days_in_month[6] = 30 # jun
days_in_month[7] = 31 # jul
days_in_month[8] = 31 # aug
days_in_month[9] = 30 # sep
days_in_month[10] = 31 # oct
days_in_month[11] = 30 # nov
days_in_month[12] = 31 # dec
return days_in_month
def get_string_range_numbers(start, stop, with_zeros=False):
values = '('
if start > stop:
temp = start
start = stop
stop = temp
if with_zeros:
if start < 10 and stop < 10:
values += '|'.join(['0'+str(n) for n in range(start,stop+1)]) + '|'
elif start < 10 and stop >= 10:
values += '|'.join(['0'+str(n) for n in range(start,10)]) + '|'
values += '|'.join([str(n) for n in range(start,stop+1)])
values += ')'
return values
def init_month_start_end(year):
days_in_month = init_days_in_month(year)
month_start_end = {}
month_start_end[0] = 0, 0
for i in range(1, 13):
a = month_start_end[i-1][1] + 1
b = month_start_end[i-1][1] + days_in_month[i]
month_start_end[i] = a, b
# print month_start_end[i]
return month_start_end
def get_week_range(index, month_start_end, days_in_month, year):
start_month = 0
end_month = 0
# print 'index', str(index)
found_s = 'f'
found_e = 'f'
for i in range(1, 13):
#print 'm_s', month_start_end[i][0]
#print 'm_e', month_start_end[i][1]
if index >= month_start_end[i][0]:
if found_s == 'f':
start_month = i
#print 's', str(start_month)
else:
found_s = 't'
#break
if index + 7 >= month_start_end[i-1][1]:
if found_e == 'f':
end_month = i
# print 'e', str(end_month)
else:
found_e = 't'
#break
#print month_start_end[start_month][0]
start_date = (index - month_start_end[start_month][0] + 1)
#print (start_date + 7)
#print days_in_month[start_month]
end_date = (start_date + 7 - 1) % days_in_month[start_month]
a = str(year) + '-' + get_date_str(start_month)+'-'+get_date_str(start_date)
b = str(year) + '-' + get_date_str(end_month)+'-'+get_date_str(end_date)
# print a, b
return a, b
def get_date_str(foo):
if foo < 10:
return '0'+str(foo)
return str(foo)
def generate_week_range(year):
month_start_end = init_month_start_end(year)
days_in_month = init_days_in_month(year)
week_range = {}
for i in range(1, 54):
#print i
index = i * 7 - 6
week_range[i] = get_week_range(index, month_start_end, days_in_month, year)
# print 'w:'+str(i), week_range[i]
week_range[53] = week_range[53][0], str(year)+'-12-31'
# print 'w:53', week_range[53]
return week_range
#generate_week_range(1998)
def get_year(date):
foo = date.split('-')[0]
def get_one_week_range(week_date):
year = week_date.split('-')[0]
#print year
week_range = generate_week_range(year)
#week = re.sub('W', '', week_date.split('-')[1])
for week in week_range:
start = week_range[week][0]
end = week_range[week][1]
if week_date >= start and week_date <= end:
#print 'INDEX: ', week
return week
return 0
def add_date(day, month, year, diff):
#print diff
days_in_month = init_days_in_month(year)
new_day = day + diff
#print new_day
new_month = month
if new_day < 1:
if month == 1:
days_in_month_prev = init_days_in_month(year-1)
new_day = days_in_month[12] + new_day
new_month = 12
else:
new_day = days_in_month[month-1] + new_day
new_month = month - 1
elif new_day > days_in_month[month]:
new_day = new_day - days_in_month[month]
new_month = month + 1
new_year = year
if new_month < 1:
new_month = 12
new_year = year - 1
elif new_month > 12:
new_month = 1
new_year = year + 1
new_year = str(new_year)
new_month = str(new_month)
if len(new_month) == 1:
new_month = '0'+new_month
new_day = str(new_day)
if len(new_day) == 1:
new_day = '0'+new_day
return new_year+'-'+new_month+'-'+new_day
## given a date (jan 26), it will calculate the next/prev DOW (Sunday)'s date
def get_dows_date_from_date(dow, day, month, year, next_or_prev):
dow_of_date = day_of_week(day, month, year)
#print 'dow_of_date:', dow_of_date
dow_reverse_foo = dow_reverse[dow]
#print 'dow_reverse_foo:', dow_reverse_foo
if next_or_prev == 'prev':
if dow_of_date >= dow_reverse_foo:
diff = dow_of_date - dow_reverse_foo
else:
diff = dow_of_date - dow_reverse_foo + 7
val = add_date(day, month, year, diff*-1)
elif next_or_prev == 'next':
if dow_reverse_foo > dow_of_date:
diff = dow_reverse_foo - dow_of_date
else:
diff = dow_reverse_foo - dow_of_date + 7
val = add_date(day, month, year, diff)
#print '#$$#', val
return val
#
#print get_dows_date_from_date('Sunday', 1, 3, 1998, 'prev')
def get_number(word):
#print '$', word, '$'
if word == 'one' or word == 'a':
return 1
elif word == 'two' or word == 'couple':
return 2
elif word == 'three':
return 3
elif word == 'four':
return 4
elif word == 'five':
return 5
elif word == 'six':
return 6
elif word == 'seven':
return 7
elif word == 'eight':
return 8
elif word == 'nine':
return 9
elif word == 'ten':
return 10
elif word == 'eleven':
return 11
elif word == 'tweleve':
return 12
elif word == 'thirteen':
return 13
elif word == 'fourteen':
return 14
elif word == 'fifteen':
return 15
elif word == 'sixteen':
return 16
elif word == 'seventeen':
return 17
elif word == 'eighteen':
return 18
elif word == 'nineteen':
return 19
elif word == 'twenty':
return 20
elif word == 'thirty':
return 30
elif word == 'forty':
return 40
elif word == 'fifty':
return 50
elif word == 'sixty':
return 60
elif word == 'seventy':
return 70
elif word == 'eighty':
return 80
elif word == 'ninety':
return 90
elif re.search('hundred', word):
return 100
elif re.search('thousand', word):
return 1000
else:
return 0
def get_month(month):
month = month.upper()
if month == 'JANUARY' or month == 'JAN' :
return '01'
elif month == 'FEBRUARY' or month == 'FEB':
return '02'
elif month == 'MARCH' or month == 'MAR':
return '03'
elif month == 'APRIL' or month == 'APR':
return '04'
elif month == 'MAY' or month == 'MAY':
return '05'
elif month == 'JUNE' or month == 'JUN':
return '06'
elif month == 'JULY' or month == 'JUL':
return '07'
elif month == 'AUGUST' or month == 'AUG':
return '08'
elif month == 'SEPTEMBER' or month == 'SEP':
return '09'
elif month == 'OCTOBER' or month == 'OCT':
return '10'
elif month == 'NOVEMBER' or month == 'NOV':
return '11'
elif month == 'DECEMBER' or month == 'DEC':
return '12'
else:
return '00'
def find_cons_in_string(cons, string):
for word in cons.split(' '):
#print 'compare:', word, string
if re.search(word.strip(), string.strip()):
return word
return 'NONE'
def remove_punctuation(word):
word = re.sub('\.', '', word)
word = re.sub('\,', '', word)
return word
def pad_zero(word):
word = str(word)
if len(word) == 1:
word = '0'+word
return word
def get_date_value(year, month, day):
year = str(year)
month = str(month)
day = str(day)
month = pad_zero(month)
day = pad_zero(day)
if int(month) > 12:
value = str(year) + '-' + str(day) + '-' + str(month)
else:
value = str(year) + '-' + str(month) + '-' + str(day)
return value
def get_datetime_value(year, month, day, hour, minutes, seconds=''):
value = get_date_value(year, month, day)
value += 'T' + str(hour) + ':' + str(minutes)
if seconds:
value += ':' + str(seconds)
return value
def pad_space(word):
return ' ' + word + ' '
def get_timex_value(cons, date):
timex_str = cons
#remove a, the, -, in
cons = ' ' + cons.lower() + ' '
cons = re.sub(' - ', ' ', cons)
cons = re.sub(' a ', ' ', cons)
cons = re.sub(' the ', ' ', cons)
cons = cons.strip()
year = int(date[0])
month = int(date[1])
day = int(date[2])
value = 'NONE'
type = 'NONE'
## handle DCT ##
year_re = '[12][0-9][0-9][0-9]'
month_re = '[01][0-9]'
day_re = '[0123][0-9]'
hour_re = '[012][0-9]'
minute_re = '[0123456][0-9]'
# Handle "yyyymmdd"
p = re.compile(year_re+month_re+day_re)
if p.search(cons):
val = cons.strip()
yr = val[0:4]
mn = val[4:6]
dt = val[6:8]
value = get_date_value(yr, mn, dt)
type = 'DATE'
return timex_str, type, value, 'DCT1mic'
# Handle "mm/dd/yyyy hh:mm:ss"
p = re.compile(month_re+'/'+day_re+'/'+year_re +' '+hour_re+':'+minute_re+':'+minute_re)
if p.search(cons):
val = p.findall(cons)[0]
mn = val[0:2]
dt = val[3:5]
yr = val[6:10]
hr = val[11:13]
min = val[14:16]
sec = val[17:19]
value = get_date_value(yr, mn, dt)+'T'+str(hr)+':'+str(min)+':'+str(sec)
type = 'TIME'
return timex_str, type, value, 'DCT2'
# Handle "mm/dd/yyyy"
p = re.compile(month_re+'/'+day_re+'/'+year_re)
if p.search(cons):
val = p.findall(cons)[0]
mn = val[0:2]
dt = val[3:5]
yr = val[6:10]
value = get_date_value(yr, mn, dt)
type = 'DATE'
return timex_str, type, value, 'DCT3'
# Handle "mm/dd/yy"
p = re.compile(month_re+'/'+day_re+'/[0-9][0-9]')
if p.search(cons):
val = p.findall(cons)[0]
mn = val[0:2]
dt = val[3:5]
yr = val[6:8]
if int(yr) > 50:
yr = '19'+str(yr)
value = get_date_value(yr, mn, dt)
type = 'DATE'
return timex_str, type, value, 'DCT4'
# Handle "mm-dd-yy hhmmX...X OR mm/dd/yy hhmmX...X"
p = re.compile(month_re+'[-|/]'+day_re+'[-|/][0-9]{2} [0-9]{4}[a-z]*')
if p.search(cons):
val = p.findall(cons)[0]
mn = val[0:2]
dt = val[3:5]
yr = str(year)[:-2] + val[6:8]
hh = val[9:11]
mi = val[11:13]
value = get_datetime_value(yr, dt, mn, hh, mi)
type = 'DATE'
return timex_str, type, value, 'mic2'
# Handle "mm-dd-yy hhmmssX...X OR mm/dd/yy hhmmssX...X"
p = re.compile(month_re+'[-|/]'+day_re+'[-|/][0-9]{2} [0-9]{6}[a-z]*')
if p.search(cons):
val = p.findall(cons)[0]
mn = val[0:2]
dt = val[3:5]
yr = str(year)[:-2] + val[6:8]
hh = val[9:11]
mi = val[11:13]
se = val[13:15]
value = get_datetime_value(yr, dt, mn, hh, mi, se)
type = 'DATE'
return timex_str, type, value, 'mic3'
# Handle "mm-dd-yy"
p = re.compile(month_re+'-'+day_re+'-[0-9][0-9]')
if p.search(cons):
val = p.findall(cons)[0]
mn = val[0:2]
dt = val[3:5]
yr = val[6:8]
if int(yr) > 50:
yr = '19'+str(yr)
value = get_date_value(yr, mn, dt)
type = 'DATE'
return timex_str, type, value, 'DCT5'
# Handle "yyyy-mm-dd OR yyyy/mm/dd"
p = re.compile(year_re+'[-|/]'+month_re+'[-|/]'+day_re)
if p.search(cons):
val = p.findall(cons)[0]
yr = val[0:4]
mn = val[5:7]
dt = val[8:10]
value = get_date_value(yr, mn, dt)
type = 'DATE'
return timex_str, type, value, 'mic1'
# Handle "yyyy-mm-ddThh:mm OR yyyy/mm/ddThh:mm"
p = re.compile(year_re+'[-|/]'+month_re+'[-|/]'+day_re+'T[0-9]{2}:[0-9]{2}')
if p.search(cons):
val = p.findall(cons)[0]
yr = val[0:4]
mn = val[5:7]
dt = val[8:10]
hh = val[11:13]
mi = val[14:16]
value = get_datetime_value(yr, dt, mn, hh, mi)
type = 'DATE'
return timex_str, type, value, 'mic4'
# Handle "yyyy-mm-ddThh:mm:ss OR yyyy/mm/ddThh:mm:ss"
p = re.compile(year_re+'[-|/]'+month_re+'[-|/]'+day_re+'T[0-9]{2}:[0-9]{2}:[0-9]{2}')
if p.search(cons):
val = p.findall(cons)[0]
yr = val[0:4]
mn = val[5:7]
dt = val[8:10]
hh = val[11:13]
mi = val[14:16]
se = val[17:19]
value = get_datetime_value(yr, dt, mn, hh, mi, se)
type = 'DATE'
return timex_str, type, value, 'mic5'
#### handle DATE type ####
if cons == 'today':
value = date[0]+'-'+date[1]+'-'+date[2]
type = 'DATE'
return timex_str, type, value, 'today'
if re.search(' now ', ' '+cons+' ') or cons == 'currently' or cons == 'at present' or re.search('moment', cons):
value = 'PRESENT_REF'
type = 'DATE'
return timex_str, type, value, 'now'
if cons.strip() == 'one day' or re.search('future', cons.strip()) or re.search('coming', cons.strip()) :
value = 'FUTURE_REF'
type = 'DATE'
return timex_str, type, value, 'future_ref'
if cons.strip() == 'times' or cons.strip() == 'several years ago' or cons.strip() == 'last time' or cons.strip() == 'few years ago' or re.search('past', cons) or re.search('previous', cons.strip()) or re.search('recently', cons):
value = 'PAST_REF'
type = 'DATE'
return timex_str, type, value, 'past_ref'
if cons.strip() == 'tomorrow':
value = add_date(day, month, year, 1)
type = 'DATE'
return timex_str, type, value, 'tomorrow'
if cons.strip() == 'yesterday':
value = add_date(day, month, year, -1)
type = 'DATE'
return timex_str, type, value, 'yesterday'
# TIME
p = re.compile('[012]?[0-9]:[0123456][0-9]')
if p.search(cons):
q = re.compile('[012]?[0-9]')
hr = q.findall(cons)[0]
r = re.compile(':[0123456][0-9]')
min = r.findall(cons)[0]
min = re.sub(':', '', min)
if re.search('p\.m\.', cons):
if int(hr) < 12:
hr = int(hr) + 12
value = get_datetime_value(year, month, day, hr, min)
type = 'TIME'
return timex_str, type, value, 'time'
# decade
prev_mod = 'previous|last'
next_mod = 'next|later'
current = 'this'
modifier = '('+prev_mod+'|'+next_mod+'|'+current+')'
p = re.compile(modifier+'[ ]*decade')
if p.search(cons):
# print cons
q = re.compile(modifier)
dec = str(year)[:3]
type = 'DATE'
if q.search(cons):
mod = q.findall(cons)[0]
if re.search(mod, prev_mod):
mod_val = 'prev'
dec = int(dec) - 1
elif re.search(mod, next_mod):
mod_val = 'next'
dec = int(dec) + 1
elif re.search(mod, current):
mod_val = 'current'
dec = dec
else:
type = 'DURATION'
dec = dec
value = str(dec)
return timex_str, type, value, 'decade'
if re.search('decade', cons):
type = 'DURATION'
value = 'P1E'
return timex_str, type, value, 'decade-rest'
# Sunday, Monday
prev_mod = '(previous|last)'
next_mod = '(next|later)'
modifier = '('+prev_mod+'|'+next_mod+')'
dow_string = '(sunday|monday|tuesday|wednesday|thursday|friday|saturday)'
p = re.compile(modifier+'?[ ]*'+dow_string)
if p.search(cons):
r = re.compile(dow_string)
tmp_dow = r.findall(cons)[0]
type = 'DATE'
q = re.compile(next_mod)
if q.search(cons):
value = get_dows_date_from_date(tmp_dow, day, month, year, 'next')
else:
value = get_dows_date_from_date(tmp_dow, day, month, year, 'prev')
if re.search('night', cons):
value = value + 'TNI'
type = 'TIME'
if re.search('nights', cons):
type = 'SET'
if re.search('morning', cons):
value = value + 'TMO'
type = 'TIME'
if re.search('mornings', cons):
type = 'SET'
if re.search('afternoon', cons):
value = value + 'TAF'
type = 'TIME'
if re.search('afternoons', cons):
type = 'SET'
if re.search('evening', cons):
value = value + 'TEV'
type = 'TIME'
if re.search('evenings', cons):
type = 'SET'
if re.search('[0-9]{2}( )?p.?m.?',cons):
hours = int(re.findall('[0-9]{2}',cons)[0])+12
value += 'T' + str(hours) + ':' + '00'
type = 'TIME'
if re.search('[0-9]{2}:[0-9]{2}( )?p.?m.?',cons):
hours = int(re.findall('[0-9]{2}',cons)[0])+12
minutes = int(re.findall('[0-9]{2}',cons)[1])+12
value += 'T' + str(hours) + ':' + minutes
type = 'TIME'
return timex_str, type, value, 'DOWmic'
if re.search('night', cons):
value = get_date_value(year, month, day) +'TNI'
type = 'DATE'
return timex_str, type, value, 'night'
if re.search('morning', cons):
value = get_date_value(year, month, day) +'TMO'
type = 'DATE'
return timex_str, type, value, 'morning'
if re.search('evening', cons):
value = get_date_value(year, month, day) +'TEV'
type = 'DATE'
return timex_str, type, value, 'evening'
if re.search('afternoon', cons):
value = get_date_value(year, month, day) +'TAF'
type = 'DATE'
return timex_str, type, value, 'afternoon'
# Handle 'this century'
if p.find('this century') > 0:
return timex_str, 'DATE', 'P100Y', 'thisCentury'
# nearly four years ago, three months ago, 10 days ago
number_one = 'one|two|couple|three|four|five|six|seven|eight|nine'
number_two = 'ten|eleven|tweleve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen'
number_three = 'twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|thousand'
num = '[0-9]+'
number = '('+number_one+'|'+number_two+'|'+number_three + '|' + num+')'
time_type = '(day|days|week|weeks|month|months|year|years|quarter)'
p = re.compile(number + ' ' + time_type + '.* ago')
if p.search(cons):
type = 'DATE'
q = re.compile(number)
foo_num = q.findall(cons)[0]
q2 = re.compile(num)
if q2.search(cons):
foo_num = int(foo_num)
else:
foo_num = get_number(foo_num)
r = re.compile(time_type)
foo_time = r.findall(cons)[0]
if re.search('week', foo_time):
week_date = get_date_value(year, month, day)
week = get_one_week_range(week_date)
week = week - 1 - int(foo_num)
value = str(year)+'-W'+pad_zero(week)
elif re.search('year', foo_time):
year = year - int(foo_num)
value = str(year)
elif re.search('day', foo_time):
value = add_date(day, month, year, -1*int(foo_num))
elif re.search('month', foo_time):
#print foo_num
year_1 = foo_num / 12
month_1 = foo_num % 12
if month > month_1:
month = month - month_1
year = year + year_1
else:
month = 12 + month - month_1
year = year + year_1 - 1
value = str(year) + '-' + pad_zero(month)
return timex_str, type, value, 'NUM TIME AGO'
p = re.compile(' a ' + time_type + '.* ago')
if p.search(' ' +timex_str.lower() +' '):
type = 'DATE'
foo_num = 1
r = re.compile(time_type)
foo_time = r.findall(cons)[0]
if re.search('week', cons):
week_date = get_date_value(year, month, day)
week = get_one_week_range(week_date)
week = week - int(foo_num)
value = str(year)+'-W'+pad_zero(week)
elif re.search('year', cons):
year = year - int(foo_num)
value = str(year)
elif re.search('day', cons):
value = add_date(day, month, year, -1*int(foo_num))
elif re.search('month', cons):
# print foo_num
year_1 = foo_num / 12
month_1 = foo_num % 12
if month > month_1:
month = month - month_1
year = year + year_1
else:
month = 12 + month - month_1
year = year + year_1 - 1
value = str(year) + '-' + pad_zero(month)
return timex_str, type, value, 'A TIME AGO'
# January this year, June last year
prev_mod = 'previous|last'
next_mod = 'next|later'
current_mod = 'this|current'
modifier = '('+prev_mod+'|'+next_mod+'|'+current_mod +')'
month_string = '(january|jan|february|feb|march|mar|april|apr|may|june|jun|july|jul|august|aug|september|sep|sept|october|oct|november|nov|december|dec)'
p = re.compile(month_string+' '+modifier+' year')
if p.search(cons):
q = re.compile(month_string)
foo_month = q.findall(cons)[0]
month = get_month(foo_month)
r = re.compile(modifier)
if r.search(cons):
mod = r.findall(cons)[0]
if re.search(mod, prev_mod):
year = year - 1
elif re.search(mod, next_mod):
year = year + 1
type = 'DATE'
value = str(year) + '-' + pad_zero(month)
return timex_str, type, value, 'January this year'
# last February
p = re.compile(modifier + ' ' + month_string)
if p.search(cons):
q = re.compile(month_string)
foo_month = q.findall(cons)[0]
month = get_month(foo_month)
r = re.compile(modifier)
if r.search(cons):
mod = r.findall(cons)[0]
if re.search(mod, prev_mod) and int(date[1]) <= int(month):
year = year - 1
elif re.search(mod, next_mod) and int(date[1]) >= int(month):
year = year + 1
type = 'DATE'
value = str(year) + '-' + pad_zero(month)
return timex_str, type, value, 'last February'
# set
# every quarters|months|years|weeks
season = 'summer|winter|spring|fall'
duration = 'day|days|hour|hours|week|weeks|month|months|year|years|quarter|quarters|period|periods'
all = '('+season+'|'+duration+')'
p = re.compile('every '+all)
if p.search(cons):
type = 'SET'
foo = p.findall(cons)[0][:1].upper()
value = 'P1'+str(foo)
return timex_str, type, value, 'every'
# several months, quarters
season = 'summer|winter'
duration = 'day|days|hour|hours|week|weeks|month|months|year|years|quarters|period|periods'
all = '('+season+'|'+duration+')'
p = re.compile('(several|recent) '+all)
if p.search(cons):
type = 'DURATION'
r = re.compile(all)
foo = r.findall(cons)[0][:1].upper()
value = 'PX'+str(foo)
return timex_str, type, value, 'several-recent'
# quarter
if cons.strip() == 'quarter' or cons.strip() == 'period':
if month >= 1 and month <= 3:
qt = 1#'Q1'
elif month >= 4 and month <= 6:
qt = 2#'Q2'
elif month >= 7 and month <= 9:
qt = 3#'Q3'
elif month >= 10 and month <= 12:
qt = 4
else:
qt = 'X'
type = 'DATE'
value = str(year) + '-Q'+str(qt)
return timex_str, type, value, 'quarter-only'
# year-ago (first)? quarter, 1988 second quarter
time = '(first|second|third|fourth)'
year_re = '[12][0-9][0-9][0-9]'
p = re.compile('(year-(ago|earlier)|'+year_re+') ('+ time +' )?(quarter|period)')
if p.search(cons):
type = 'DATE'
if re.search('first', cons):
qt = 1#'Q1'
elif re.search('second', cons):
qt = 2#'Q2'
elif re.search('third', cons):
qt = 3#'Q3'
elif re.search('fourth', cons):
qt = 4#'Q4'
elif month >= 1 and month <= 3:
qt = 1#'Q1'
elif month >= 4 and month <= 6:
qt = 2#'Q2'
elif month >= 7 and month <= 9:
qt = 3#'Q3'
elif month >= 10 and month <= 12:
qt = 4
else:
qt = 'X'
if re.search('year-ago', cons) or re.search('year-earlier', cons):
year = year - 1
r = re.compile(year_re)
if r.search(cons):
year = r.findall(cons)[0]
value = str(year) + '-Q'+str(qt)
return timex_str, type, value, 'year quarter'
# next three quarters (P9M)
number_one = ' one|two|couple|three|four|five|six|seven|eight|nine'
number_two = 'ten|eleven|tweleve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen'
number_three = 'twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|thousand'
num = '[0-9]+'
number = '('+number_one+'|'+number_two+'|'+number_three + '|' + num +')'
trace = ''
p = re.compile(number+ ' (quarter|period)')
if p.search(cons):
q = re.compile(number)
if q.search(timex_str):
# if cons == number:
r1 = re.compile(number_three)
r2 = re.compile(number_one)
r3 = re.compile(number_two)
r4 = re.compile(num)
if r1.search(cons):
trace += 'r1'
word1 = r1.findall(cons)[0]
# print word1, cons
if not re.search(pad_space(word1), pad_space(cons)):
word1 = '0'
else:
word1 = '0'
if r2.search(cons):
trace += 'r2'
word3 = r2.findall(cons)[0]
if not re.search(pad_space(word3), pad_space(cons)):
word3 = '0'
else:
word3 = '0'
if r3.search(cons):
trace += 'r3'
word2 = r3.findall(cons)[0]
if not re.search(pad_space(word2), pad_space(cons)):
word2 = '0'
else:
word2 = '0'
if r4.search(cons):
trace += 'r4'
word4 = r4.findall(cons)[0]
else:
word4 = '0'
num = get_number(word1) + get_number(word2) + get_number(word3) + int(word4)
qt = num# * 3
value = 'P'+str(qt)+'Q'
type = 'DURATION'
return timex_str, type, value, 'quarter-duration'
# this year's third quarter, next year's first quarter
prev_mod = 'previous|last'
next_mod = 'next|later'
current_mod = 'this|current|latest'
time = 'first|second|third|fourth'
modifier = '('+prev_mod+'|'+next_mod+'|'+current_mod +')'
p = re.compile(modifier+ ' year\'s ' + time + '[- ]?(quarter|period)')
if p.search(cons):
type = 'DATE'
r = re.compile(modifier)
if r.search(cons):
mod = r.findall(cons)[0]
if re.search(mod, prev_mod):
mod_val = 'prev'
year = year - 1
elif re.search(mod, next_mod):
mod_val = 'next'
year = year + 1