我有话文件中查找文本的语词(最多3个位置左右)多个词的一致性文本
words文件:
买
时间
玻璃
家
红
文本文件:
经过近十年的网上卖杂货,亚马逊一直未能就其自身的重大凹痕,因为消费者都表现出了顽强的冲动购买的物品,如水果,蔬菜和肉本人。 您是否发现自己在手机上花费的时间超过了您...无意中通过盯着手机定期传递时间? 过程可以是充满了焦虑,因为许多不同的玻璃款式可供选择,并且对什么是正确的和必要的观点交锋点
脚本:
def keywordsContext(file, fileName):
#file: text file
#fileName: words file
with open(file, "r") as f, open(fileName, "r") as fi:
corpus = f.read().split()
pivot = fi.read().split()
for keywords in pivot:
if keywords in corpus:
index = pivot.index(keywords)
contexts = keywords+":", pivot[index-3:index], pivot[index+1:index+4]
print(contexts)
else:
pass
输出:
('buy:',[],['time','glass','home'])
('time:',[],['glass','home','red']] )
( '玻璃:',[],[ '家', '红'])
无
输出我想:
'买入':固执冲动买如水果
“时间”的项目:自己花费更多的时间你的手机
“玻璃”上:许多不同的GLAS小号款式可供选择
编辑
而且......如果同一个词多次出现?我做了一个测试(在语料库中再加一个句子来重复“玻璃”一词)。我试着放一段时间len(语料库)!= 0,但它是一个循环,重复相同的输出...
def keywordsContext(file, fileName):
with open(file, "r") as f, open(fileName, "r") as fi:
corpus = f.read().split()
pivot = fi.read().split()
while len(corpus) != 0:
for keywords in pivot:
if keywords in corpus:
inde = corpus.index(keywords)
contexts = keywords+": "+ ' '.join(corpus[inde-3:inde+4])
print(contexts)
输出:
团购:固执冲动买这样的水果项目,
时间:自己花费更多的时间你的手机
玻璃上:许多不同的玻璃款式齐全,
购买:固执求购物品,如水果,
时间:自己花费更多的时间你的手机
玻璃上:许多不同的玻璃款式可供选择,
团购:固执冲动买这样的水果项目,
时间:自己花费更多的时间在手机上
玻璃:许多不同的玻璃款式可供选择,
.. 。
我编辑我的职务,检查出来 – mdolata