Uncategorized

python – Extract words surrounding a search word


While regex would work, I think it’s overkill for this problem. You’re better off with two list comprehensions:

sentence="The world is a small place, we should try to take care of it.".split()
indices = (i for i,word in enumerate(sentence) if word=="place")
neighbors = []
for ind in indices:
    neighbors.append(sentence[ind-3:ind]+sentence[ind+1:ind+4])

Note that if the word that you’re looking for appears multiple times consecutively in the sentence, then this algorithm will include the consecutive occurrences as neighbors.
For example:

In [29]: neighbors = []

In [30]: sentence=”The world is a small place place place, we should try to take care of it.”.split()

In [31]: sentence
Out[31]:
[‘The’,
‘world’,
‘is’,
‘a’,
‘small’,
‘place’,
‘place’,
‘place,’,
‘we’,
‘should’,
‘try’,
‘to’,
‘take’,
‘care’,
‘of’,
‘it.’]

In [32]: indices = [i for i,word in enumerate(sentence) if word == 'place']

In [33]: for ind in indices:
   ....:     neighbors.append(sentence[ind-3:ind]+sentence[ind+1:ind+4])


In [34]: neighbors
Out[34]: 
[['is', 'a', 'small', 'place', 'place,', 'we'],
 ['a', 'small', 'place', 'place,', 'we', 'should']]



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *