Search code examples
pythonvariablesartificial-intelligenceextractspelling

Is there a way to extract text after a word is stated?


So I am trying to make a virtual assistant and am working on a spelling feature like the one on a google home.

An example of what I am trying to achieve is when I say "hey google spell cat" it will say C A T How would I get cat into a variable?

I know how to split it


Solution

  • If I understand you correctly, you're saying that you have a string and wish to store the last word in it. This can be achieved by split as you said and then assignment:

    text = 'hey google spell cat'
    last_word = text.split()[-1]
    

    If you instead want the word after spell you can just index spell and add one:

    text = 'hi google spell cat for me'
    split = text.split()
    split[split.index('spell')+1]