4 ms·
If you don't want it approximate, but exact, this is still a tricky non-trivial ambiguous problem. And this problem often comes up in text normalization for NLP
by albertzeyer 7y ago
If you don't want it approximate, but exact, this is still a tricky non-trivial ambiguous problem. And this problem often comes up in text normalization for NLP task (e.g. speech recognition, or text-to-speech).
For Python, for English, there is the inflect library: https://pypi.org/project/inflect/ https://pypi.org/project/inflect/
- benibela 7y agoI also wrote a function in Pascal to convert integers to natural language exactly. E.g. here is the output for 100000! https://gist.github.com/benibela/f0163b02562f647e4d2f https://gist.github.com/benibela/f0163b02562f647e4d2f
- jagged-chisel 7y agoIt's 8.8MB of plaintext English output describing the ridiculously long number, for those on mobile (who can't see the full file), or who don't want to click through the 'file is truncated...' link
- benibela 7y agoAs an approximation, one could just say three duoquinquagintacentillinonagintacentillion
- pbhjpbhj 7y agoIsn't "one hundred thousand factorial" a more useful conversion? Would your conversion ever find utility? Aside, this is why French style punctuation with a space before ? or ! is superior.
- benibela 7y agoI did not write it to be useful. I just wanted to say it can handle large integers. I am implementing the XPath standard. The standard says there need to be a function to convert integers to English, but it does not specify an upper limit on the integers
- pbhjpbhj 7y agoCool, IMO that makes it potentially useful (but being useful isn't required of course). Out of curiosity, what part of the spec does that implement? Is it to do with fn:format-integer.
- benibela 7y agoHere is the function standard: https://www.w3.org/TR/xpath-functions-31/#func-format-integer https://www.w3.org/TR/xpath-functions-31/#func-format-intege... Number formatting is really complicated
- tyingq 7y agoPerl does a reasonable job of it in around 250 lines: https://github.com/neilb/Lingua-EN-Numbers/blob/master/lib/Lingua/EN/Numbers.pm https://github.com/neilb/Lingua-EN-Numbers/blob/master/lib/L...
- feute 7y agoFor Common Lisp, there's a FORMAT directive: CL-USER> (format t "~r" 4096) four thousand ninety-six
- cormacrelf 7y agoI didn't know Common Lisp spoke American English
- philjohn 7y agoI've got it (albeit limited to 7 digit numbers) in an interview before. Lots of fun edge cases to handle.