13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
#
16
- import json
17
- from datetime import timedelta
16
+ from datetime import datetime , timedelta
18
17
19
- from lingua_franca .internal import resolve_resource_file
20
18
from lingua_franca .lang .common_data_fa import (_FARSI_BIG , _FARSI_HUNDREDS ,
21
19
_FARSI_ONES , _FARSI_TENS ,
22
20
_FORMAL_VARIANT )
23
- from lingua_franca .lang .parse_common import Normalizer
24
21
from lingua_franca .time import now_local
25
22
26
23
@@ -31,6 +28,7 @@ def _is_number(s):
31
28
except ValueError :
32
29
return False
33
30
31
+
34
32
def _parse_sentence (text ):
35
33
for key , value in _FORMAL_VARIANT .items ():
36
34
text = text .replace (key , value )
@@ -39,8 +37,8 @@ def _parse_sentence(text):
39
37
current_number = 0
40
38
current_words = []
41
39
s = 0
42
- step = 10
43
40
mode = 'init'
41
+
44
42
def finish_num ():
45
43
nonlocal current_number
46
44
nonlocal s
@@ -54,13 +52,14 @@ def finish_num():
54
52
current_number = 0
55
53
current_words = []
56
54
mode = 'init'
55
+
57
56
for x in ar :
58
57
if x == "و" :
59
58
if mode == 'num_ten' or mode == 'num_hundred' or mode == 'num_one' :
60
59
mode += '_va'
61
60
current_words .append (x )
62
61
elif mode == 'num' :
63
- current_words .append (x )
62
+ current_words .append (x )
64
63
else :
65
64
finish_num ()
66
65
result .append (x )
@@ -71,7 +70,7 @@ def finish_num():
71
70
elif x in _FARSI_ONES :
72
71
t = _FARSI_ONES .index (x )
73
72
if mode != 'init' and mode != 'num_hundred_va' and mode != 'num' :
74
- if not (t < 10 and mode == 'num_ten_va' ):
73
+ if not (t < 10 and mode == 'num_ten_va' ):
75
74
finish_num ()
76
75
current_words .append (x )
77
76
s += t
@@ -80,20 +79,20 @@ def finish_num():
80
79
if mode != 'init' and mode != 'num_hundred_va' and mode != 'num' :
81
80
finish_num ()
82
81
current_words .append (x )
83
- s += _FARSI_TENS .index (x )* 10
82
+ s += _FARSI_TENS .index (x ) * 10
84
83
mode = 'num_ten'
85
84
elif x in _FARSI_HUNDREDS :
86
85
if mode != 'init' and mode != 'num' :
87
86
finish_num ()
88
87
current_words .append (x )
89
- s += _FARSI_HUNDREDS .index (x )* 100
88
+ s += _FARSI_HUNDREDS .index (x ) * 100
90
89
mode = 'num_hundred'
91
90
elif x in _FARSI_BIG :
92
91
current_words .append (x )
93
92
d = _FARSI_BIG .index (x )
94
93
if mode == 'init' and d == 1 :
95
94
s = 1
96
- s *= 10 ** ( 3 * d )
95
+ s *= 10 ** ( 3 * d )
97
96
current_number += s
98
97
s = 0
99
98
mode = 'num'
@@ -120,6 +119,7 @@ def finish_num():
120
119
'هفته' : timedelta (weeks = 1 ),
121
120
}
122
121
122
+
123
123
def extract_duration_fa (text ):
124
124
"""
125
125
Convert an english phrase into a number of seconds
@@ -208,9 +208,8 @@ def extract_datetime_fa(text, anchorDate=None, default_time=None):
208
208
.replace ('سه شنبه' , 'سهشنبه' ) \
209
209
.replace ('چهار شنبه' , 'چهارشنبه' ) \
210
210
.replace ('پنج شنبه' , 'پنجشنبه' ) \
211
- .replace ('بعد از ظهر' , 'بعدازظهر' ) \
212
-
213
-
211
+ .replace ('بعد از ظهر' , 'بعدازظهر' )
212
+
214
213
if not anchorDate :
215
214
anchorDate = now_local ()
216
215
today = anchorDate .replace (hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
@@ -225,11 +224,11 @@ def extract_datetime_fa(text, anchorDate=None, default_time=None):
225
224
'یکشنبه' ,
226
225
]
227
226
daysDict = {
228
- 'پریروز' : today + timedelta (days = - 2 ),
229
- 'دیروز' : today + timedelta (days = - 1 ),
227
+ 'پریروز' : today + timedelta (days = - 2 ),
228
+ 'دیروز' : today + timedelta (days = - 1 ),
230
229
'امروز' : today ,
231
- 'فردا' : today + timedelta (days = 1 ),
232
- 'پسفردا' : today + timedelta (days = 2 ),
230
+ 'فردا' : today + timedelta (days = 1 ),
231
+ 'پسفردا' : today + timedelta (days = 2 ),
233
232
}
234
233
timesDict = {
235
234
'صبح' : timedelta (hours = 8 ),
@@ -307,34 +306,6 @@ def extract_datetime_fa(text, anchorDate=None, default_time=None):
307
306
remainder .append (x )
308
307
return (result , " " .join (remainder ))
309
308
310
- def is_fractional_fa (input_str , short_scale = True ):
311
- """
312
- This function takes the given text and checks if it is a fraction.
313
-
314
- Args:
315
- input_str (str): the string to check if fractional
316
- short_scale (bool): use short scale if True, long scale if False
317
- Returns:
318
- (bool) or (float): False if not a fraction, otherwise the fraction
319
-
320
- """
321
- if input_str .endswith ('s' , - 1 ):
322
- input_str = input_str [:len (input_str ) - 1 ] # e.g. "fifths"
323
-
324
- fracts = {"whole" : 1 , "half" : 2 , "halve" : 2 , "quarter" : 4 }
325
- if short_scale :
326
- for num in _SHORT_ORDINAL_FA :
327
- if num > 2 :
328
- fracts [_SHORT_ORDINAL_FA [num ]] = num
329
- else :
330
- for num in _LONG_ORDINAL_FA :
331
- if num > 2 :
332
- fracts [_LONG_ORDINAL_FA [num ]] = num
333
-
334
- if input_str .lower () in fracts :
335
- return 1.0 / fracts [input_str .lower ()]
336
- return False
337
-
338
309
339
310
def extract_numbers_fa (text , short_scale = True , ordinals = False ):
340
311
"""
0 commit comments