Skip to content

Commit

Permalink
port timezone fixes for time utils MycroftAI#209
Browse files Browse the repository at this point in the history
add to_system utility

fix extract_datetime_fr timezone bug  MycroftAI#218

fix farsi tests (ignoring timezone due to not being rebased before merge)

remove unimplemented farsi code (is_fractional and normalize)

split english tests into their own files like the other languages
  • Loading branch information
JarbasAl committed Jan 18, 2022
1 parent 64e09cb commit 7bcb35c
Show file tree
Hide file tree
Showing 8 changed files with 1,790 additions and 1,624 deletions.
61 changes: 16 additions & 45 deletions lingua_franca/lang/parse_fa.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
from datetime import timedelta
from datetime import datetime, timedelta

from lingua_franca.internal import resolve_resource_file
from lingua_franca.lang.common_data_fa import (_FARSI_BIG, _FARSI_HUNDREDS,
_FARSI_ONES, _FARSI_TENS,
_FORMAL_VARIANT)
from lingua_franca.lang.parse_common import Normalizer
from lingua_franca.time import now_local


Expand All @@ -31,6 +28,7 @@ def _is_number(s):
except ValueError:
return False


def _parse_sentence(text):
for key, value in _FORMAL_VARIANT.items():
text = text.replace(key, value)
Expand All @@ -39,8 +37,8 @@ def _parse_sentence(text):
current_number = 0
current_words = []
s = 0
step = 10
mode = 'init'

def finish_num():
nonlocal current_number
nonlocal s
Expand All @@ -54,13 +52,14 @@ def finish_num():
current_number = 0
current_words = []
mode = 'init'

for x in ar:
if x == "و":
if mode == 'num_ten' or mode == 'num_hundred' or mode == 'num_one':
mode += '_va'
current_words.append(x)
elif mode == 'num':
current_words.append(x)
current_words.append(x)
else:
finish_num()
result.append(x)
Expand All @@ -71,7 +70,7 @@ def finish_num():
elif x in _FARSI_ONES:
t = _FARSI_ONES.index(x)
if mode != 'init' and mode != 'num_hundred_va' and mode != 'num':
if not(t < 10 and mode == 'num_ten_va'):
if not (t < 10 and mode == 'num_ten_va'):
finish_num()
current_words.append(x)
s += t
Expand All @@ -80,20 +79,20 @@ def finish_num():
if mode != 'init' and mode != 'num_hundred_va' and mode != 'num':
finish_num()
current_words.append(x)
s += _FARSI_TENS.index(x)*10
s += _FARSI_TENS.index(x) * 10
mode = 'num_ten'
elif x in _FARSI_HUNDREDS:
if mode != 'init' and mode != 'num':
finish_num()
current_words.append(x)
s += _FARSI_HUNDREDS.index(x)*100
s += _FARSI_HUNDREDS.index(x) * 100
mode = 'num_hundred'
elif x in _FARSI_BIG:
current_words.append(x)
d = _FARSI_BIG.index(x)
if mode == 'init' and d == 1:
s = 1
s *= 10**(3*d)
s *= 10 ** (3 * d)
current_number += s
s = 0
mode = 'num'
Expand All @@ -120,6 +119,7 @@ def finish_num():
'هفته': timedelta(weeks=1),
}


def extract_duration_fa(text):
"""
Convert an english phrase into a number of seconds
Expand Down Expand Up @@ -208,9 +208,8 @@ def extract_datetime_fa(text, anchorDate=None, default_time=None):
.replace('سه شنبه', 'سهشنبه') \
.replace('چهار شنبه', 'چهارشنبه') \
.replace('پنج شنبه', 'پنجشنبه') \
.replace('بعد از ظهر', 'بعدازظهر') \


.replace('بعد از ظهر', 'بعدازظهر')

if not anchorDate:
anchorDate = now_local()
today = anchorDate.replace(hour=0, minute=0, second=0, microsecond=0)
Expand All @@ -225,11 +224,11 @@ def extract_datetime_fa(text, anchorDate=None, default_time=None):
'یکشنبه',
]
daysDict = {
'پریروز': today + timedelta(days= -2),
'دیروز': today + timedelta(days= -1),
'پریروز': today + timedelta(days=-2),
'دیروز': today + timedelta(days=-1),
'امروز': today,
'فردا': today + timedelta(days= 1),
'پسفردا': today + timedelta(days= 2),
'فردا': today + timedelta(days=1),
'پسفردا': today + timedelta(days=2),
}
timesDict = {
'صبح': timedelta(hours=8),
Expand Down Expand Up @@ -307,34 +306,6 @@ def extract_datetime_fa(text, anchorDate=None, default_time=None):
remainder.append(x)
return (result, " ".join(remainder))

def is_fractional_fa(input_str, short_scale=True):
"""
This function takes the given text and checks if it is a fraction.
Args:
input_str (str): the string to check if fractional
short_scale (bool): use short scale if True, long scale if False
Returns:
(bool) or (float): False if not a fraction, otherwise the fraction
"""
if input_str.endswith('s', -1):
input_str = input_str[:len(input_str) - 1] # e.g. "fifths"

fracts = {"whole": 1, "half": 2, "halve": 2, "quarter": 4}
if short_scale:
for num in _SHORT_ORDINAL_FA:
if num > 2:
fracts[_SHORT_ORDINAL_FA[num]] = num
else:
for num in _LONG_ORDINAL_FA:
if num > 2:
fracts[_LONG_ORDINAL_FA[num]] = num

if input_str.lower() in fracts:
return 1.0 / fracts[input_str.lower()]
return False


def extract_numbers_fa(text, short_scale=True, ordinals=False):
"""
Expand Down
3 changes: 1 addition & 2 deletions lingua_franca/lang/parse_fr.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,8 +944,7 @@ def date_found():
if not hasYear:
temp = datetime.strptime(datestr, "%B %d")
if extractedDate.tzinfo:
temp = temp.replace(tzinfo=gettz("UTC"))
temp = temp.astimezone(extractedDate.tzinfo)
temp = temp.replace(tzinfo=extractedDate.tzinfo)
temp = temp.replace(year=extractedDate.year)
if extractedDate < temp:
extractedDate = extractedDate.replace(year=int(currentYear),
Expand Down
34 changes: 22 additions & 12 deletions lingua_franca/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def now_utc():
Returns:
(datetime): The current time in Universal Time, aka GMT
"""
return to_utc(datetime.utcnow())
return datetime.utcnow().replace(tzinfo=gettz("UTC"))


def now_local(tz=None):
Expand All @@ -58,8 +58,7 @@ def now_local(tz=None):
Returns:
(datetime): The current time
"""
if not tz:
tz = default_timezone()
tz = tz or default_timezone()
return datetime.now(tz)


Expand All @@ -71,11 +70,10 @@ def to_utc(dt):
Returns:
(datetime): time converted to UTC
"""
tzUTC = gettz("UTC")
if dt.tzinfo:
return dt.astimezone(tzUTC)
else:
return dt.replace(tzinfo=gettz("UTC")).astimezone(tzUTC)
tz = gettz("UTC")
if not dt.tzinfo:
dt = dt.replace(tzinfo=default_timezone())
return dt.astimezone(tz)


def to_local(dt):
Expand All @@ -87,8 +85,20 @@ def to_local(dt):
(datetime): time converted to the local timezone
"""
tz = default_timezone()
if dt.tzinfo:
return dt.astimezone(tz)
else:
return dt.replace(tzinfo=gettz("UTC")).astimezone(tz)
if not dt.tzinfo:
dt = dt.replace(tzinfo=default_timezone())
return dt.astimezone(tz)


def to_system(dt):
"""Convert a datetime to the system's local timezone
Args:
dt (datetime): A datetime (if no timezone, assumed to be UTC)
Returns:
(datetime): time converted to the operation system's timezone
"""
tz = tzlocal()
if not dt.tzinfo:
dt = dt.replace(tzinfo=default_timezone())
return dt.astimezone(tz)
Loading

0 comments on commit 7bcb35c

Please sign in to comment.