-
Notifications
You must be signed in to change notification settings - Fork 3
/
find_time.py
130 lines (114 loc) · 5.64 KB
/
find_time.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
import re
from unidecode import unidecode
from persian_num_change import persian_num_change
def find_date_time(tokens_lem, sentence):
"""
Calculate time of given string by persian sentences in it and convert
it to HH:MM :
input : string from entity_group 'I_TIM' from tokens_lem
output: string
----------------------------------------
Function Description:
'بعد از ظهر' : Convert to pm (+12h)
'نیم' : Add 30 minutes(+30m)
'ربع' : Add 15 minutes(+15m)
Assuming that we don't have sentences like this:
'شش و ربع کم' : Corresponds to '5:45'
"""
# Convert persian number word to digit
times = []
for token in tokens_lem:
string = token['word']
if token['entity_group'] == 'time':
string = string.replace("یک", "1")
string = string.replace("دو", "2")
string = string.replace("سه", "3")
string = string.replace("چهار", "4")
string = string.replace("پنج", "5")
string = string.replace("شش", "6")
string = string.replace("هفت", "7")
string = string.replace("هشت", "8")
string = string.replace("نه", "9")
string = string.replace("ده", "10")
string = string.replace("یازده", "11")
string = string.replace("دوازده", "12")
# Converting to HH:MM format
numbers = [x.group() for x in re.finditer(r'([\u0660-\u0669]|[\d])+', string)]
raw_hour = numbers[0]
raw_minutes = numbers[1] if len(numbers) == 2 else '0'
if "بعد از ظهر" in string or "عصر" in string or "شب" in string:
if int(raw_hour) < 12:
raw_hour = str(int(raw_hour) + 12)
if "بامداد" in string or "صبح" in string:
if int(raw_hour) > 12:
raw_hour = str(int(raw_hour) - 12)
if "و نیم" in string:
raw_minutes = '30'
elif "و ربع" in string:
raw_minutes = '15'
times.append(raw_hour.zfill(2) + ':' + raw_minutes.zfill(2))
times = [unidecode(time) for time in times]
return times
def reformat_date_time(string):
def get_num(num):
persian_num = persian_num_change(num)
persian_num.results()
return persian_num.num
string = string.replace('ربع ', '15 دقیقه')
string = string.replace('نیم ', '30 دقیقه')
""" ساعت 3 و بیست و دو دقیقه """
numbers = [x.group() for x in re.finditer(
r"ساعت ([\u0660-\u0669]|\d)+ و ((([\u0600-\u0659]|[\u0670-\u06EF])+ دقیقه)|(([\u0600-\u0659]|[\u0670-\u06EF])+ و ([\u0600-\u0659]|[\u0670-\u06EF])+ دقیقه))",
string)]
for number in numbers:
to_replace = number.replace('ساعت ', '')
number = number.replace('ساعت ', '')
number = number.replace(' دقیقه', '')
numbers = number.split(' و ', maxsplit=1)
string = string.replace(to_replace,
f'{str(unidecode(numbers[0])).zfill(2)}:{str(get_num(numbers[1])).zfill(2)}')
""" ساعت نه و 22 دقیقه """
numbers = [x.group() for x in
re.finditer(r"ساعت ([\u0600-\u0659]|[\u0670-\u06EF])+ و (([\u0660-\u0669]|\d)+ دقیقه)", string)]
for number in numbers:
to_replace = number.replace('ساعت ', '')
number = number.replace('ساعت ', '')
number = number.replace(' دقیقه', '')
numbers = number.split(' و ', maxsplit=1)
string = string.replace(to_replace,
f'{str(get_num(numbers[0])).zfill(2)}:{str(unidecode(numbers[1])).zfill(2)}')
""" ساعت نه و بیست و دو دقیقه """
numbers = [x.group() for x in re.finditer(
r"ساعت ([\u0600-\u0659]|[\u0670-\u06EF])+ و ((([\u0600-\u0659]|[\u0670-\u06EF])+ دقیقه)|(([\u0600-\u0659]|[\u0670-\u06EF])+ و ([\u0600-\u0659]|[\u0670-\u06EF])+ دقیقه))",
string)]
for number in numbers:
to_replace = number.replace('ساعت ', '')
number = number.replace('ساعت ', '')
number = number.replace(' دقیقه', '')
numbers = number.split(' و ', maxsplit=1)
string = string.replace(to_replace, f'{str(get_num(numbers[0])).zfill(2)}:{str(get_num(numbers[1])).zfill(2)}')
""" ساعت نه"""
numbers = [x.group() for x in re.finditer(r"ساعت ([\u0600-\u0659]|[\u0670-\u06EF])+", string)]
for number in numbers:
to_replace = number.replace('ساعت ', '')
number = number.replace('ساعت ', '')
try:
string = string.replace(to_replace, f'{str(get_num(number))}:00')
except KeyError:
pass
""" ساعت 9 و 22 دقیقه"""
numbers = [x.group() for x in re.finditer(r"ساعت ([\u0660-\u0669]|\d)+ و (([\u0660-\u0669]|\d)+ دقیقه)", string)]
for number in numbers:
to_replace = number.replace('ساعت ', '')
number = number.replace('ساعت ', '')
number = number.replace(' دقیقه', '')
numbers = number.split(' و ')
string = string.replace(to_replace,
f'{str(unidecode(numbers[0])).zfill(2)}:{str(unidecode(numbers[1])).zfill(2)}')
""" ساعت 5 """
numbers = [x.group() for x in re.finditer(r"ساعت ([\u0660-\u0669]|\d)+ ", string)]
for number in numbers:
to_replace = number.replace('ساعت ', '')
number = number.replace('ساعت ', '')
string = string.replace(to_replace, f'{str(unidecode(number)).zfill(2)}:00 ')
return string