我有这个脚本在文本中进行单词搜索。搜索结果非常好,结果按预期工作。我想要实现的是提取接近匹配的n
个单词。例如:
世界是一个小地方,我们应该尽力照顾它。
假设我正在寻找place
,我需要提取右侧的3个单词和左侧的3个单词。在这种情况下,他们将是:
left -> [is, a, small]
right -> [we, should, try]
这样做的最佳方法是什么?
谢谢!
答案 0 :(得分:14)
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'))
答案 1 :(得分:3)
找到所有单词:
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]
答案 2 :(得分:3)
虽然正则表达式可行,但我认为这个问题太过分了。你最好有两个列表理解:
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]中:句子='世界是一个小地方的地方,我们应该尝试照顾它。'。split()
在[31]中:句子 出[31]: [ '的', '世界', “是”, '一个', '小', '地点', '地点', '地点,', '我们', '应该', '尝试', '至', '采取', '关心', '的', '它。']
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']]
答案 3 :(得分:3)
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']
答案 4 :(得分:3)
处理搜索关键字多次出现的场景。例如下面是搜索关键字:地方出现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']