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

Stricter parsing, support more phrases, use doctest #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

exogen
Copy link

@exogen exogen commented Jul 9, 2016

OK this one is intentional. :)

First off I know the point of this snippet is that it's tiny and simple. It's super handy and easy to modify. So you don't gotta accept this PR but I bet someone out there will find it useful.

My two big issues with the current snippet:

  • Any combination of nonsense numbers are supported (they're just added together), e.g.:
    • text2num("one one") == 2
    • text2num("eleven three") == 14
    • text2num("thousand eighty hundred twelve hundred") == 801200
    • text2num("million three twenty") == 23
  • Numbers with "implied hundreds" aren't parsed as one would expect, e.g.:
    • text2num("nineteen eighty four") == 103
    • text2num("one fifty") == 51

This PR fixes all those cases to do what you'd expect (throw an error or give a more sensible number). They could be guarded behind a strict flag if you wanna support both.

And some minor stuff:

  • Adds support for "and" in appropriate places and , in the input string – this means we can round-trip output from the equally-handy num2words package. e.g. two thousand, one hundred and three.
  • Adds a ton of new tests in doctest format in the docstring.

Thanks for the excellent snippet @ghewgill!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant