diff --git a/lingua_franca/lang/parse_en.py b/lingua_franca/lang/parse_en.py index 4542c996..c3c8c2b6 100644 --- a/lingua_franca/lang/parse_en.py +++ b/lingua_franca/lang/parse_en.py @@ -1687,7 +1687,7 @@ def normalize_en(text, remove_articles=True): return EnglishNormalizer().normalize(text, remove_articles) -def extract_timespan_en(text, resolution=TimespanUnit.TIMEDELTA, replace_token=""): +def extract_timespan_en(text, unit=TimespanUnit.TIMEDELTA, replace_token=""): """ Convert an english phrase into a number of seconds Convert things like: @@ -1701,7 +1701,7 @@ def extract_timespan_en(text, resolution=TimespanUnit.TIMEDELTA, replace_token=" (300, "set a timer for"). Args: text (str): string containing a duration - resolution (TimespanUnit): format to return extracted duration on + unit (TimespanUnit): format to return extracted duration on replace_token (str): string to replace consumed words with Returns: (timedelta, str): @@ -1730,7 +1730,7 @@ def extract_timespan_en(text, resolution=TimespanUnit.TIMEDELTA, replace_token=" _replace_token = (replace_token + " " + replace_token) \ if replace_token else "" - if resolution == TimespanUnit.TIMEDELTA: + if unit == TimespanUnit.TIMEDELTA: si_units = { 'microseconds': None, 'milliseconds': None, @@ -1777,10 +1777,10 @@ def extract_timespan_en(text, resolution=TimespanUnit.TIMEDELTA, replace_token=" else: si_units[unit] = value duration = timedelta(**si_units) if any(si_units.values()) else None - elif resolution in [TimespanUnit.RELATIVEDELTA, - TimespanUnit.RELATIVEDELTA_APPROXIMATE, - TimespanUnit.RELATIVEDELTA_FALLBACK, - TimespanUnit.RELATIVEDELTA_STRICT]: + elif unit in [TimespanUnit.RELATIVEDELTA, + TimespanUnit.RELATIVEDELTA_APPROXIMATE, + TimespanUnit.RELATIVEDELTA_FALLBACK, + TimespanUnit.RELATIVEDELTA_STRICT]: relative_units = { 'microseconds': None, 'seconds': None, @@ -1831,7 +1831,7 @@ def extract_timespan_en(text, resolution=TimespanUnit.TIMEDELTA, replace_token=" # microsecond, month, year must be ints relative_units["microseconds"] = int(relative_units["microseconds"]) - if resolution == TimespanUnit.RELATIVEDELTA_FALLBACK: + if unit == TimespanUnit.RELATIVEDELTA_FALLBACK: for unit in ["months", "years"]: value = relative_units[unit] _leftover, _ = math.modf(value) @@ -1843,7 +1843,7 @@ def extract_timespan_en(text, resolution=TimespanUnit.TIMEDELTA, replace_token=" TimespanUnit.TIMEDELTA, replace_token) relative_units[unit] = int(value) - elif resolution == TimespanUnit.RELATIVEDELTA_APPROXIMATE: + elif unit == TimespanUnit.RELATIVEDELTA_APPROXIMATE: _leftover, year = math.modf(relative_units["years"]) relative_units["months"] += 12 * _leftover relative_units["years"] = int(year) @@ -1902,33 +1902,33 @@ def extract_timespan_en(text, resolution=TimespanUnit.TIMEDELTA, replace_token=" microseconds += value * 1000 * 1000 * 60 * 60 * 24 * \ DAYS_IN_1_YEAR * 1000 - if resolution == TimespanUnit.TOTAL_MICROSECONDS: + if unit == TimespanUnit.TOTAL_MICROSECONDS: duration = microseconds - elif resolution == TimespanUnit.TOTAL_MILLISECONDS: + elif unit == TimespanUnit.TOTAL_MILLISECONDS: duration = microseconds / 1000 - elif resolution == TimespanUnit.TOTAL_SECONDS: + elif unit == TimespanUnit.TOTAL_SECONDS: duration = microseconds / (1000 * 1000) - elif resolution == TimespanUnit.TOTAL_MINUTES: + elif unit == TimespanUnit.TOTAL_MINUTES: duration = microseconds / (1000 * 1000 * 60) - elif resolution == TimespanUnit.TOTAL_HOURS: + elif unit == TimespanUnit.TOTAL_HOURS: duration = microseconds / (1000 * 1000 * 60 * 60) - elif resolution == TimespanUnit.TOTAL_DAYS: + elif unit == TimespanUnit.TOTAL_DAYS: duration = microseconds / (1000 * 1000 * 60 * 60 * 24) - elif resolution == TimespanUnit.TOTAL_WEEKS: + elif unit == TimespanUnit.TOTAL_WEEKS: duration = microseconds / (1000 * 1000 * 60 * 60 * 24 * 7) - elif resolution == TimespanUnit.TOTAL_MONTHS: + elif unit == TimespanUnit.TOTAL_MONTHS: duration = microseconds / (1000 * 1000 * 60 * 60 * 24 * DAYS_IN_1_MONTH) - elif resolution == TimespanUnit.TOTAL_YEARS: + elif unit == TimespanUnit.TOTAL_YEARS: duration = microseconds / (1000 * 1000 * 60 * 60 * 24 * DAYS_IN_1_YEAR) - elif resolution == TimespanUnit.TOTAL_DECADES: + elif unit == TimespanUnit.TOTAL_DECADES: duration = microseconds / (1000 * 1000 * 60 * 60 * 24 * DAYS_IN_1_YEAR * 10) - elif resolution == TimespanUnit.TOTAL_CENTURIES: + elif unit == TimespanUnit.TOTAL_CENTURIES: duration = microseconds / (1000 * 1000 * 60 * 60 * 24 * DAYS_IN_1_YEAR * 100) - elif resolution == TimespanUnit.TOTAL_MILLENNIUMS: + elif unit == TimespanUnit.TOTAL_MILLENNIUMS: duration = microseconds / (1000 * 1000 * 60 * 60 * 24 * DAYS_IN_1_YEAR * 1000) else: diff --git a/lingua_franca/parse.py b/lingua_franca/parse.py index 988e8c5c..caf08703 100644 --- a/lingua_franca/parse.py +++ b/lingua_franca/parse.py @@ -129,7 +129,7 @@ def extract_duration(text, lang=''): @localized_function(run_own_code_on=[FunctionNotLocalizedError]) def extract_timespan(text, - resolution=TimespanUnit.TIMEDELTA, + unit=TimespanUnit.TIMEDELTA, replace_token="", lang=''): """ Convert an english phrase into a number of seconds @@ -159,7 +159,7 @@ def extract_timespan(text, be None if no duration is found. The text returned will have whitespace stripped from the ends. """ - if resolution == TimespanUnit.TIMEDELTA and replace_token == "": + if unit == TimespanUnit.TIMEDELTA and replace_token == "": return extract_duration(text, lang) raise FunctionNotLocalizedError(f"extract_timespan not implemented for {lang}") diff --git a/test/unittests/test_parse_en.py b/test/unittests/test_parse_en.py index db1a7dc8..661dac20 100644 --- a/test/unittests/test_parse_en.py +++ b/test/unittests/test_parse_en.py @@ -1684,7 +1684,7 @@ def test_extract_timespan_ambiguous(self): resolution=TimespanUnit.RELATIVEDELTA_STRICT) self.assertEqual( extract_timespan("1.3 months", - resolution=TimespanUnit.RELATIVEDELTA_FALLBACK), + unit=TimespanUnit.RELATIVEDELTA_FALLBACK), (timedelta(days=1.3 * DAYS_IN_1_MONTH), "")) @@ -1699,11 +1699,11 @@ def test_extract_timespan_ambiguous(self): self.assertEqual( extract_timespan("1.3 months", - resolution=TimespanUnit.RELATIVEDELTA_APPROXIMATE + unit=TimespanUnit.RELATIVEDELTA_APPROXIMATE )[0].months, 1) self.assertAlmostEquals( extract_timespan("1.3 months", - resolution=TimespanUnit.RELATIVEDELTA_APPROXIMATE + unit=TimespanUnit.RELATIVEDELTA_APPROXIMATE )[0].days, 0.3 * DAYS_IN_1_MONTH) def test_extract_timespan_en(self): @@ -1749,7 +1749,7 @@ def test_extract_timespan_en(self): (timedelta(days=DAYS_IN_1_MONTH), "")) self.assertEqual( extract_timespan("1 month", - resolution=TimespanUnit.TIMEDELTA), + unit=TimespanUnit.TIMEDELTA), (timedelta(days=DAYS_IN_1_MONTH), "")) self.assertEqual(extract_timespan("3 months"), @@ -1780,129 +1780,129 @@ def test_extract_timespan_en(self): def test_extract_timespan_delta_en(self): self.assertEqual( extract_timespan("10 seconds", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(seconds=10.0), "")) self.assertEqual( extract_timespan("5 minutes", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(minutes=5), "")) self.assertEqual( extract_timespan("2 hours", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(hours=2), "")) self.assertEqual( extract_timespan("3 days", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(days=3), "")) self.assertEqual( extract_timespan("25 weeks", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(weeks=25), "")) self.assertEqual( extract_timespan("seven hours", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(hours=7), "")) self.assertEqual( extract_timespan("7.5 seconds", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(seconds=7.5), "")) self.assertEqual( extract_timespan("eight and a half days thirty nine seconds", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(days=8.5, seconds=39), "")) self.assertEqual( extract_timespan("Set a timer for 30 minutes", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(minutes=30), "Set a timer for")) self.assertEqual( extract_timespan("Four and a half minutes until sunset", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(minutes=4.5), "until sunset")) self.assertEqual( extract_timespan("Nineteen minutes past the hour", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(minutes=19), "past the hour")) self.assertEqual( extract_timespan("wake me up in three weeks, four hundred " "ninety seven days, and three hundred 91.6 " "seconds", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(weeks=3, days=497, seconds=391.6), "wake me up in , , and")) self.assertEqual( extract_timespan("The movie is one hour, fifty seven" " and a half minutes long", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(hours=1, minutes=57.5), "The movie is , long")) self.assertEqual( extract_timespan("10-seconds", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(seconds=10.0), "")) self.assertEqual( extract_timespan("5-minutes", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(minutes=5), "")) self.assertEqual( extract_timespan("1 month", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(months=1), "")) self.assertEqual( extract_timespan("3 months", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(months=3), "")) self.assertEqual( extract_timespan("a year", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=1), "")) self.assertEqual( extract_timespan("1 year", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=1), "")) self.assertEqual( extract_timespan("5 years", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=5), "")) self.assertEqual( extract_timespan("a decade", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=10), "")) self.assertEqual( extract_timespan("1 decade", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=10), "")) self.assertEqual( extract_timespan("5 decades", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=10 * 5), "")) self.assertEqual( extract_timespan("1 century", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=100), "")) self.assertEqual( extract_timespan("a century", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=100), "")) self.assertEqual( extract_timespan("5 centuries", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=500), "")) self.assertEqual( extract_timespan("1 millennium", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=1000), "")) self.assertEqual( extract_timespan("5 millenniums", - resolution=TimespanUnit.RELATIVEDELTA), + unit=TimespanUnit.RELATIVEDELTA), (relativedelta(years=1000 * 5), "")) def test_extract_timespan_microseconds_en(self): def test_milliseconds(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_MICROSECONDS) + duration_str, unit=TimespanUnit.TOTAL_MICROSECONDS) self.assertEqual(remainder, expected_remainder) @@ -1946,7 +1946,7 @@ def test_extract_timespan_milliseconds_en(self): def test_milliseconds(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_MILLISECONDS) + duration_str, unit=TimespanUnit.TOTAL_MILLISECONDS) self.assertEqual(remainder, expected_remainder) @@ -2014,7 +2014,7 @@ def test_extract_timespan_seconds_en(self): def test_seconds(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_SECONDS) + duration_str, unit=TimespanUnit.TOTAL_SECONDS) self.assertEqual(remainder, expected_remainder) @@ -2066,7 +2066,7 @@ def test_extract_timespan_minutes_en(self): def test_minutes(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_MINUTES) + duration_str, unit=TimespanUnit.TOTAL_MINUTES) self.assertEqual(remainder, expected_remainder) @@ -2114,7 +2114,7 @@ def test_extract_timespan_hours_en(self): def test_hours(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_HOURS) + duration_str, unit=TimespanUnit.TOTAL_HOURS) self.assertEqual(remainder, expected_remainder) @@ -2163,7 +2163,7 @@ def test_extract_timespan_days_en(self): def test_days(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_DAYS) + duration_str, unit=TimespanUnit.TOTAL_DAYS) self.assertEqual(remainder, expected_remainder) @@ -2211,7 +2211,7 @@ def test_extract_timespan_weeks_en(self): def test_weeks(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_WEEKS) + duration_str, unit=TimespanUnit.TOTAL_WEEKS) self.assertEqual(remainder, expected_remainder) @@ -2257,7 +2257,7 @@ def test_extract_timespan_months_en(self): def test_months(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_MONTHS) + duration_str, unit=TimespanUnit.TOTAL_MONTHS) self.assertEqual(remainder, expected_remainder) @@ -2308,7 +2308,7 @@ def test_extract_timespan_years_en(self): def test_years(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_YEARS) + duration_str, unit=TimespanUnit.TOTAL_YEARS) self.assertEqual(remainder, expected_remainder) @@ -2355,7 +2355,7 @@ def test_extract_timespan_decades_en(self): def test_decades(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_DECADES) + duration_str, unit=TimespanUnit.TOTAL_DECADES) self.assertEqual(remainder, expected_remainder) @@ -2398,7 +2398,7 @@ def test_extract_timespan_centuries_en(self): def test_centuries(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_CENTURIES) + duration_str, unit=TimespanUnit.TOTAL_CENTURIES) self.assertEqual(remainder, expected_remainder) @@ -2442,7 +2442,7 @@ def test_extract_timespan_millennia_en(self): def test_millennium(duration_str, expected_duration, expected_remainder): duration, remainder = extract_timespan( - duration_str, resolution=TimespanUnit.TOTAL_MILLENNIUMS) + duration_str, unit=TimespanUnit.TOTAL_MILLENNIUMS) self.assertEqual(remainder, expected_remainder)