Hacker News new | past | comments | ask | show | jobs | submit login

Really useful, thanks for making this!

How is the "three and a half" part implemented? Is that just a set of regexes looking for a couple of common patterns? For example, can it parse something like "three and three thirds"?




Sure, I'm glad you like it! :)

str2num parses words by iterating through them once and keeping state about the result so far and what I call the "magnitude". So, for example, when you feed it "three and three thirds", it initializes magnitude and result to zero, chomps the first "three", and then sets magnitude = 3.

If the next word were "million", then it would know that magnitude refers to million, and so it would multiply 3 by 1 million. But the next word is "and", which flushes the magnitude to result, (ie, result += magnitude), and resets the magnitude to zero.

The next word is "three", which, as before, sets magnitude to three. Then the next word is "thirds", so you know that magnitude refers to "thirds", and so you multiply the magnitude by Fraction(1, 3) and add it to result.

  >>> str2num("three and three thirds")
  Fraction(4, 1)
The source for this is here: https://github.com/naftaliharris/numutil/blob/master/numutil...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: