简体   繁体   English

提取搜索词周围的词

[英]Extract words surrounding a search word

I have this script that does a word search in text.我有这个脚本可以在文本中进行单词搜索。 The search goes pretty good and results work as expected.搜索进行得非常好,结果按预期工作。 What I'm trying to achieve is extract n words close to the match.我想要实现的是提取接近匹配的n单词。 For example:例如:

The world is a small place, we should try to take care of it.世界很小,我们应该努力去照顾它。

Suppose I'm looking for place and I need to extract the 3 words on the right and the 3 words on the left.假设我在找place ,我需要提取右边的 3 个词和左边的 3 个词。 In this case they would be:在这种情况下,它们将是:

left -> [is, a, small]
right -> [we, should, try]

What is the best approach to do this?做到这一点的最佳方法是什么?

Thanks!谢谢!

def search(text,n):
    '''Searches for text, and retrieves n words either side of the text, which are retuned seperatly'''
    word = r"\W*([\w]+)"
    groups = re.search(r'{}\W*{}{}'.format(word*n,'place',word*n), text).groups()
    return groups[:n],groups[n:]

This allows you to specify how many words either side you want to capture.这允许您指定要捕获的任一侧的字数。 It works by constructing the regular expression dynamically.它通过动态构造正则表达式来工作。 With

t = "The world is a small place, we should try to take care of it."
search(t,3)
(('is', 'a', 'small'), ('we', 'should', 'try'))

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 = []在 [29] 中:邻居 = []

In [30]: sentence = 'The world is a small place place place, we should try to take care of it.'.split()在[30]中:sentence = '世界是一个小地方地方地方,我们应该尽量照顾它。'.split()

In [31]: sentence Out[31]: ['The', 'world', 'is', 'a', 'small', 'place', 'place', 'place,', 'we', 'should', 'try', 'to', 'take', 'care', 'of', 'it.'] In [31]: 句子 Out[31]: ['The', 'world', 'is', 'a', 'small', 'place', 'place', 'place,', 'we', '应该', '尝试', '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']]
import re
s='The world is a small place, we should try to take care of it.'
m = re.search(r'((?:\w+\W+){,3})(place)\W+((?:\w+\W+){,3})', s)
if m:
    l = [ x.strip().split() for x in m.groups()]
left, right = l[0], l[2]
print left, right

Output输出

['is', 'a', 'small'] ['we', 'should', 'try']

If you search for The , it yields:如果你搜索The ,它会产生:

[] ['world', 'is', 'a']

Find all of the words:找出所有单词:

import re

sentence = 'The world is a small place, we should try to take care of it.'
words = re.findall(r'\w+', sentence)

Get the index of the word that you're looking for:获取您要查找的单词的索引:

index = words.index('place')

And then use slicing to find the other ones:然后使用切片找到其他的:

left = words[index - 3:index]
right = words[index + 1:index + 4]

Handling the scenario where the search keyword appears multiple times.处理搜索关键字多次出现的场景。 For example below is the input text where search keyword : place appears 3 times例如下面是搜索关键字: place出现3次的输入文本

The world is a small place, we should try to take care of this small place by planting trees in every place wherever is possible

Here is the function这是函数

import re

def extract_surround_words(text, keyword, n):
    '''
    text : input text
    keyword : the search keyword we are looking
    n : number of words around the keyword
    '''
    #extracting all the words from text
    words = words = re.findall(r'\w+', text)
    
    #iterate through all the words
    for index, word in enumerate(words):

        #check if search keyword matches
        if word == keyword:
            #fetch left side words
            left_side_words = words[index-n : index]
            
            #fetch right side words
            right_side_words = words[index+1 : index + n + 1]
            
            print(left_side_words, right_side_words)

Calling the function调用函数

text = 'The world is a small place, we should try to take care of this small place by planting trees in every place wherever is possible'
keyword = "place"
n = 3
extract_surround_words(text, keyword, n)

output : 
['is', 'a', 'small'] ['we', 'should', 'try']
['we', 'should', 'try'] ['to', 'microsot', 'is']
['also', 'take', 'care'] ['googe', 'is', 'one']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM