Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new parse_fraction function for simple fractions added with test data #60

Merged
merged 11 commits into from
Mar 24, 2021
9 changes: 5 additions & 4 deletions number_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def parse_number(input_string, language=None):
number_built = _build_number(normalized_tokens, lang_data)
if len(number_built) == 1:
return int(number_built[0])

Gallaecio marked this conversation as resolved.
Show resolved Hide resolved
return None

Gallaecio marked this conversation as resolved.
Show resolved Hide resolved
def parse_fraction(input_string, language=None):
Expand All @@ -275,9 +276,9 @@ def parse_fraction(input_string, language=None):
if language is None:
language = _valid_tokens_by_language(input_string)

fraction_separators = ["divided by", "over", "by", "/"]
FRACTION_SEPARATORS = ["divided by", "over", "by", "/"]

for separator in fraction_separators:
for separator in FRACTION_SEPARATORS:
position_of_separator = input_string.find(separator)

if position_of_separator == -1:
Expand All @@ -289,10 +290,10 @@ def parse_fraction(input_string, language=None):
number_before_separator = parse_number(string_before_separator, language)
number_after_separator = parse_number(string_after_separator, language)

if number_before_separator == None or number_after_separator == None:
if number_before_separator is None or number_after_separator is None:
return None

return str(number_before_separator) + '/' + str(number_after_separator)
return f'{number_before_separator}/{number_after_separator}'

return None

Expand Down