繁体   English   中英

提取搜索词周围的词

[英]Extract words surrounding a search word

我有这个脚本可以在文本中进行单词搜索。 搜索进行得非常好,结果按预期工作。 我想要实现的是提取接近匹配的n单词。 例如:

世界很小,我们应该努力去照顾它。

假设我在找place ,我需要提取右边的 3 个词和左边的 3 个词。 在这种情况下,它们将是:

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

做到这一点的最佳方法是什么?

谢谢!

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:]

这允许您指定要捕获的任一侧的字数。 它通过动态构造正则表达式来工作。

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

虽然正则表达式可以工作,但我认为这对于这个问题来说太过分了。 最好使用两个列表推导式:

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])

请注意,如果您要查找的单词在句子中连续出现多次,则该算法会将连续出现的单词作为邻居包含在内。
例如:

在 [29] 中:邻居 = []

在[30]中:sentence = '世界是一个小地方地方地方,我们应该尽量照顾它。'.split()

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

输出

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

如果你搜索The ,它会产生:

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

找出所有单词:

import re

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

获取您要查找的单词的索引:

index = words.index('place')

然后使用切片找到其他的:

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

处理搜索关键字多次出现的场景。 例如下面是搜索关键字: 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

这是函数

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)

调用函数

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