Divyansh_C
Divyansh_C

Reputation: 87

Find and select a required word from a sentence?

I am trying to get raw_input from user and then find a required word from that input. If the required word is there, then a function runs. So I tried .split to split the input but how do I find if the required word is in the list.

Upvotes: 2

Views: 2243

Answers (2)

be_good_do_good
be_good_do_good

Reputation: 4441

Harrison's way is one way. Here are other ways:

Way 1:

sentence = raw_input("enter input:")
words = sentence.split(' ')
desired_word = 'test'

if desired_word in words:
    # do required operations

Way 2:

import re

sentence = raw_input("enter input:")
desired_word = 'test'
if re.search('\s' + desired_word + '\s', sentence.strip()):
    # do required operations

Way 3 (especially if there are punctuations at the end of the word):

import re

sentence = raw_input("enter input:")
desired_word = 'test'
if re.search('\s' + desired_word + '[\s,:;]', sentence.strip()):
    # do required operations

Upvotes: 1

Harrison
Harrison

Reputation: 5386

It's really simple to get this done. Python has an in operator that does exactly what you need. You can see if a word is present in a string and then do whatever else you'd like to do.

sentence = 'hello world'
required_word = 'hello'

if required_word in sentence:
    # do whatever you'd like

You can see some basic examples of the in operator in action here.

Depending on the complexity of your input or lack of complexity of your required word, you may run into some problems. To deal with that you may want to be a little more specific with your required word.

Let's take this for example:

sentence = 'i am harrison'
required_word = 'is'

This example will evaluate to True if you were to doif required_word in sentence: because technically the letters is are a substring of the word "harrison".

To fix that you would just simply do this:

sentence = 'i am harrison'
required_word = ' is '

By putting the empty space before and after the word it will specifically look for occurrences of the required word as a separate word, and not as a part of a word.

HOWEVER, if you are okay with matching substrings as well as word occurrences then you can ignore what I previously explained.

If there's a group of words and if any of them is the required one, then what should I do? Like, the required word is either "yes" or "yeah". And the input by user contains "yes" or "yeah".

As per this question, an implementation would look like this:

sentence = 'yes i like to code in python'
required_words = ['yes', 'yeah']
                  ^   ^  ^    ^
# add spaces before and after each word if you don't 
# want to accidentally run into a chance where either word 
# is a substring of one of the words in sentence

if any(word in sentence for word in required_words):
    # do whatever you'd like

This makes use of the any operator. The if statement will evaluate to true as long as at least one of the words in required_words is found in sentence.

Upvotes: 6

Related Questions