jaraco / inflect

Correctly generate plurals, ordinals, indefinite articles; convert numbers to words
https://pypi.org/project/inflect
MIT License
972 stars 106 forks source link

inconsistency in handling spaces #26

Open Siddhant opened 9 years ago

Siddhant commented 9 years ago
>>> p.number_to_words('11 11')
'one thousand, one hundred and eleven'
>>> p.ordinal(11)
'11th'
>>> p.ordinal('11 ')
'11 th'
>>> p.ordinal('11  ')
'11  th'
>>> p.ordinal('11 11')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ssara29/others/Abin/src/inflect/inflect.py", line 2797, in ordinal
    n = int(num)
ValueError: invalid literal for int() with base 10: '11 11'

While number_to_words is removing all intervening spaces, ordinal ain't doing the same. Why the inconsistency? Why isn't number_to_words complaining about the space?

Siddhant commented 9 years ago

And this I expected to throw an error:

>>> p.number_to_words('1213jfkjsdn34235jnjn3432')
'one trillion, two hundred and thirteen billion, three hundred and forty-two million, three hundred and fifty-three thousand, four hundred and thirty-two'
pydsigner commented 9 years ago

number_to_words() strips out all non-numeric characters, presumably to clean up commas, while ordinal() accepts non-numeric numbers (i.e. 'forty') and thus cannot do that in the same way. Both functions could stand some improvement.

hugovk commented 9 years ago
>>> p.number_to_words('11 11')
'one thousand, one hundred and eleven'

This is probably the right thing to do.

Numbers can for example have commas for thousands separators (UK), fullstops (France), or spaces (scientific use). See http://wordpress.mrreid.org/2014/05/27/stop-putting-commas-in-your-numbers/

So it's probably best for the user to decide how to split text before giving it to number_to_words().

Perhaps ordinal() should also strip out non-numeric characters as well.