Skip to content

Stricter parsing, support more phrases, use doctest#7

Open
exogen wants to merge 1 commit intoghewgill:masterfrom
exogen:the-strictening
Open

Stricter parsing, support more phrases, use doctest#7
exogen wants to merge 1 commit intoghewgill:masterfrom
exogen:the-strictening

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