【发布时间】:2014-07-21 12:59:26
【问题描述】:
我正在从事一个文本搜索项目,并使用文本 blob 从文本中搜索句子。 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)
【问题讨论】:
-
您的代码中是否存在一些缩进错误?
-
我建议,看看'nltk'
-
@cengizkrbck TextBlob 似乎比 nltk 工作得更好。我一个,一个不知道前后一个句子。
-
尝试使用索引:
[map(str, blob.sentences[i-1:i+2]) for i, s in enumerate(blob.sentences) if search_words & set(s.words)] -
@tobias_k 非常感谢 Tobias :)