Is there a way to convert number words to Integers?

前端 未结 16 1981
北恋
北恋 2020-11-22 06:14

I need to convert one into 1, two into 2 and so on.

Is there a way to do this with a library or a class or anythi

16条回答
  •  旧巷少年郎
    2020-11-22 06:42

    This could be easily be hardcoded into a dictionary if there's a limited amount of numbers you'd like to parse.

    For slightly more complex cases, you'll probably want to generate this dictionary automatically, based on the relatively simple numbers grammar. Something along the lines of this (of course, generalized...)

    for i in range(10):
       myDict[30 + i] = "thirty-" + singleDigitsDict[i]
    

    If you need something more extensive, then it looks like you'll need natural language processing tools. This article might be a good starting point.

提交回复
热议问题