Combinations of strings and numbers should convert as expected. For instance:
num2words("text 1") should return "text one"
Actual Behaviour
"decimal.invalidOperation" exception is raised.
Steps to reproduce
Call num2words with any mix of characters and numerals.
It would seem to me the general approach here should be that if a string cannot be directly converted to decimal, the library should regex the numbers, extract them, convert them, and re-inject them. I was a little surprised because this to me would be a major use case of the library. In my particular case I was trying to canonicalize user searches.
Solution Sketch
I ended up doing this (which is certainly not a robust solution, and is English-language specific, etc. etc. but is a sort of rough template of what I had in mind). I am sure there are a number of edge cases that this doesn't handle properly, but just the same...
def num_wrapper(text):
""" Wraps num2words to allow mixed text-numeric types """
return re.sub(r"(([0-9]+[,.]?)+([,.][0-9]+)?)", num_wrapper_inner, text)
def num_wrapper_inner(match):
""" Inner wrapper feeds the string from the regex match to num2words """
return num2words(match.group())
num_wrapper("test 11... more 2000.95 numbers... 9-1-1")
# 'test eleven.. more two thousand point nine five numbers... nine-one-one'
Expected Behaviour
Combinations of strings and numbers should convert as expected. For instance:
num2words("text 1")
should return "text one"Actual Behaviour
"decimal.invalidOperation" exception is raised.
Steps to reproduce
Call num2words with any mix of characters and numerals.
It would seem to me the general approach here should be that if a string cannot be directly converted to decimal, the library should regex the numbers, extract them, convert them, and re-inject them. I was a little surprised because this to me would be a major use case of the library. In my particular case I was trying to canonicalize user searches.
Solution Sketch
I ended up doing this (which is certainly not a robust solution, and is English-language specific, etc. etc. but is a sort of rough template of what I had in mind). I am sure there are a number of edge cases that this doesn't handle properly, but just the same...