[英]Python: finding the two words following a key word
我确定我在这里遗漏了一些明显的东西,但是我盯着这段代码已经有一段时间了,却找不到问题的根源。
我想搜索许多字符串,查找所有出现的某些关键字,并针对每个匹配,以检索(并保存)关键字前后的两个单词。 到目前为止,我在代码中找到了这些单词,但是当一个字符串中出现多个关键字时,该代码将返回两个不同的列表。 如何在观察/字符串级别汇总这些列表(以便我可以将其匹配回字符串i)?
这是一个示例和所需结果的模拟示例。 关键字是“ not”:
review_list=['I like this book.', 'I do not like this novel, no, I do not.']
results= [[], ['I do not like this I do not']]
当前结果(使用下面的代码)将是:results = [[],['我不喜欢这个'],['我不喜欢]]]
这是代码(简化版):
for i in review_list:
if (" not " or " neither ") in i:
z = i.split(' ')
for x in [x for (x, y) in enumerate(z) if find_not in y]:
neg_1=[(' '.join(z[max(x-numwords,0):x+numwords+1]))]
neg1.append(neg_1)
elif (" not " or " neither ") not in i:
neg_1=[]
neg1.append(neg_1)
同样,我确定这是基本的,但是作为Python新用户,我们将不胜感激。 谢谢!
从例如字符串中仅提取单词(删除标点符号)
'I do not like this novel, no, I do not.'
我建议使用正则表达式:
import re
words = re.findall(r'\w+', somestring)
查找一个词not
等于的所有索引:
indices = [i for i, w in enumerate(words) if w=='not']
为了同时获得前两个词和以下两个词,我建议使用set
删除重复项:
allindx = set()
for i in indices:
for j in range(max(0, i-2), min(i+3, len(words))):
allindx.add(j)
最后将所有疑问词放入一个空格连接的字符串中:
result = ' '.join(words[i] for i in sorted(allindx))
现在,我们当然可以将所有这些花絮放到一个函数中了……:
import re
def twoeachside(somestring, keyword):
words = re.findall(r'\w+', somestring)
indices = [i for i, w in enumerate(words) if w=='not']
allindx = set()
for i in indices:
for j in range(max(0, i-2), min(i+3, len(words)):
allindx.add(j)
result = ' '.join(words(i) for i in sorted(allindx))
return result
当然,此功能仅适用于单个句子。 从句子列表中列出结果:
review_list = ['I like this book.', 'I do not like this novel, no, I do not.']
results = [twoeachside(s, 'not') for s in review_list]
assert results == [[], ['I do not like this I do not']]
最后一个assert
当然只是检查代码是否按您期望的方式工作:-)
编辑:实际上,从示例来看,您有点荒谬地要求结果的项目为带有单个字符串项的列表 (如果非空,则为空),但如果它们中的字符串为空,则为空列表。 当然也可以满足这个绝对怪异的规范...:
results = [twoeachside(s, 'not') for s in review_list]
results = [[s] if s else [] for s in results]
这根本没有任何意义,但是,嘿,这是您的规范!-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.