-4

EDIT: The "already answered" is not talking about what I am. My string already comes out in word format. I need to strip those words from my string into a list

I'm trying to work with phrase manipulation for a voice assistant in python.

When I speak something like:

"What is 15,276 divided by 5?"

It comes out formatted like this:

"what is fifteen thousand two hundred seventy six divided by five"

I already have a way to change a string to an int for the math part, so is there a way to somehow get a list like this from the phrase?

['fifteen thousand two hundred seventy six','five']
Jebby
  • 1,845
  • 1
  • 12
  • 25

1 Answers1

0

Go through the list of words and check each one for membership in a set of numerical words. Add adjacent words to a temporary list. When you find a non-number word, create a new list. Remember to account for non-number words at the beginning of the sentence and number words at the end of the sentence.

result = []
group = []
nums = set('one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty thirty forty fifty sixty seventy eighty ninety hundred thousand million billion trillion quadrillion quintillion'.split())
for word in "what is fifteen thousand two hundred seventy six divided by five".split():
    if word in nums:
        group.append(word)
    else:
        if group:
            result.append(group)
        group = []

if group:
    result.append(group)

Result:

>>> result
[['fifteen', 'thousand', 'two', 'hundred', 'seventy', 'six'], ['five']]

To join each sublist into a single string:

>>> list(map(' '.join, result))
['fifteen thousand two hundred seventy six', 'five']
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97