2

How can I find where a word is located in a sentence using python? For example the fourth word in the sentence.

sentence = "It's a beautiful world."
word = "world"

locate_word(word,sentence) = 4
yishairasowsky
  • 741
  • 1
  • 7
  • 21
  • Does this answer your question? [Python: Find a list of words in a text and return its index](https://stackoverflow.com/questions/14307313/python-find-a-list-of-words-in-a-text-and-return-its-index) – Kent Shikama Dec 19 '19 at 13:07
  • https://docs.python.org/3/library/stdtypes.html#str.index – Diptangsu Goswami Dec 19 '19 at 13:08
  • The problem is splitting the sentence into single words. A simple `sentence.split()` will give you `["It's", 'a', 'beautiful', 'world.']` and `"world."` is not the same as `"world"`. So you might want to remove punctuation from the string (`sentence = sentence.translate(str.maketrans('', '', string.punctuation))`) but that will change `"It's"` to `"Its"` too. – Matthias Dec 19 '19 at 13:12

5 Answers5

0

I believe this is a rather straightforward solution and easy to read;

sentence = "It's a beautiful world. Worldwide in the world, for the world"
word = "world"
ax = sentence.split(' ')

import re
for i in ax:
    if len(re.findall('\\world\\b',i)) > 0:
        print(ax.index(i))

Output:

3
7
10
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
0

You'll have to specify how you would like to split the sentence. I found a example from another answer. You can modify this to suit your need.

import re

sentence = "It's a beautiful world."
word = "world"

def locate_word(word, sentence):
    # https://stackoverflow.com/a/6181784/12565014
    wordList = re.sub("[^\w]", " ",  sentence).split()
    return wordList.index(word)

locate_word(word,sentence) # output 4
JxCode
  • 160
  • 5
0

Split the sentence(by default its splitting by spaces) then loop through the list that you got. If your word is in the list item it will give you back the index of it. And after this you add +1 to the index you've got because the countiong of lists starts from 0.

import string
def locate_word(word, sentence):
    splitted = sentence.split()
    indexes = []
    for x in splitted:
        if word in x and len(word) == len(x.strip(string.punctuation)):
            indexes.append(splitted.index(x) + 1)
    return print(indexes)

locate_word('worldwide', "It's a worldwide beautiful world. worldwide")
Cappi
  • 21
  • 4
  • This is literally my first attempt ( except the +1) and it fails for cases where `world` is included in the string, for example `worldwide` as @Reznik pointed. – Celius Stingher Dec 19 '19 at 13:20
  • Then you just strip the punctuation(with the help of the strings import), and match the lengths of the searched words, it will give the correct answer. I edited my code according to my comment, also added handling that if the world is in the sentence multiple times. – Cappi Dec 19 '19 at 13:43
0

For the above example it will work

import re
sentence = "It's a beautiful world."
word = "world"
lst = sentence.split()
for i in range(0,len(lst)):
    if "." in lst[i]:
        lst[i] = lst[i].replace(".", "")
print(lst.index(word))
Vaibhav Jadhav
  • 2,020
  • 1
  • 7
  • 20
-1

You can split the sentence at space, then lookup the word in the list.

Edit: based on the comment, you may also need to strip the punctuation.

import string


def locate_word(word, sentence):
    sentence = sentence.translate(str.maketrans('', '', string.punctuation))
    ListOfWord = sentence.split(" ")
    return ListOfWord.index(word)
Petru Tanas
  • 1,087
  • 1
  • 12
  • 36