使用Python进行文本搜索
我正在做一个文本搜索的项目,使用一个叫做TextBlob的工具来从文本中搜索句子。TextBlob可以很有效地找到包含关键词的所有句子。不过,为了更好地研究,我还想提取出关键词前面和后面各一个句子,但我还没有找到办法。
下面是我正在使用的代码:
def extraxt_sents(Text,word):
search_words = set(word.split(','))
sents = ''.join([s.lower() for s in Text])
blob = TextBlob(sents)
matches = [str(s) for s in blob.sentences if search_words & set(s.words)]
print search_words
print(matches)
1 个回答
1
如果你想获取匹配项前后的行,可以选择创建一个循环并记住前一行,或者使用切片,就像在blob.sentences
列表上使用[from:to]
那样。
最好的方法可能是使用enumerate
这个内置函数。
match_region = [map(str, blob.sentences[i-1:i+2]) # from prev to after next
for i, s in enumerate(blob.sentences) # i is index, e is element
if search_words & set(s.words)] # same as your condition
在这里,blob.sentences[i-1:i+2]
会提取一个子列表,从索引i-1
(包含)到索引i+2
(不包含)。而map
则会把这个列表中的元素转换成字符串。
注意:其实,你可能想把i-1
替换成max(0, i-1)
;否则i-1
可能会变成-1
,这样Python会把它当成最后一个元素,导致得到一个空的切片。另一方面,如果i+2
超过了列表的长度,这就不会是个问题。