Python:查找关键字后面的两个词
Python: finding the two words following a key word
我确定我在这里遗漏了一些明显的东西,但我已经盯着这段代码看了一段时间,但找不到问题的根源。
我想搜索许多字符串,找到某些关键字的所有出现,并针对这些匹配中的每一个,检索(并保存)关键字前后的两个词。
到目前为止,我已经找到了这些词的代码,但是当一个字符串中的关键字出现不止一次时,代码 returns 两个不同的列表。我如何在 observation/string 级别聚合这些列表(以便我可以将其匹配回字符串 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']]
当前结果(使用下面的代码)将是:
结果 = [[], ['I do not like this'], ['I do not']]
这里是代码(简化版):
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)
最后将所有有问题的单词放入 space 连接的字符串中:
result = ' '.join(words[i] for i in sorted(allindx))
现在我们当然可以将所有这些花絮放在一个函数中......:[=21=]
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
当然只是检查代码是否按您的要求工作:-)
编辑:实际上从这个例子来看,你有点荒谬地要求结果的项目是 lists 如果非空则有一个字符串项目,但如果其中的字符串为空列表会是空的。这个绝对奇怪的规范当然也可以满足......:[=21=]
results = [twoeachside(s, 'not') for s in review_list]
results = [[s] if s else [] for s in results]
它根本没有任何意义,但是嘿!,这是你的规格!-)
我确定我在这里遗漏了一些明显的东西,但我已经盯着这段代码看了一段时间,但找不到问题的根源。
我想搜索许多字符串,找到某些关键字的所有出现,并针对这些匹配中的每一个,检索(并保存)关键字前后的两个词。 到目前为止,我已经找到了这些词的代码,但是当一个字符串中的关键字出现不止一次时,代码 returns 两个不同的列表。我如何在 observation/string 级别聚合这些列表(以便我可以将其匹配回字符串 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']]
当前结果(使用下面的代码)将是: 结果 = [[], ['I do not like this'], ['I do not']]
这里是代码(简化版):
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)
最后将所有有问题的单词放入 space 连接的字符串中:
result = ' '.join(words[i] for i in sorted(allindx))
现在我们当然可以将所有这些花絮放在一个函数中......:[=21=]
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
当然只是检查代码是否按您的要求工作:-)
编辑:实际上从这个例子来看,你有点荒谬地要求结果的项目是 lists 如果非空则有一个字符串项目,但如果其中的字符串为空列表会是空的。这个绝对奇怪的规范当然也可以满足......:[=21=]
results = [twoeachside(s, 'not') for s in review_list]
results = [[s] if s else [] for s in results]
它根本没有任何意义,但是嘿!,这是你的规格!-)