2017-10-17 24 views
1

我有话文件中查找文本的语词(最多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) 

输出:

团购:固执冲动买这样的水果项目,

时间:自己花费更多的时间你的手机

玻璃上:许多不同的玻璃款式齐全,

购买:固执求购物品,如水果,

时间:自己花费更多的时间你的手机

玻璃上:许多不同的玻璃款式可供选择,

团购:固执冲动买这样的水果项目,

时间:自己花费更多的时间在手机上

玻璃:许多不同的玻璃款式可供选择,

.. 。

+1

我编辑我的职务,检查出来 – mdolata

回答

2

与列表中的名称一些错误。试试看:

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: 
      lst_index = 0 
      for i in range(0, corpus.count(keywords)): 
       inde = corpus.index(keywords, lst_index) 
       contexts = keywords+": "+ ' '.join(corpus[inde-3:inde+4]) 
       lst_index = inde+1 
       print(contexts) 
     else: 
      pass 

编辑:据OP编辑,该程序的打印文字出现的所有

3
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 = corpus.index(keywords) 
       contexts = "'" + keywords + "' : " + " ".join(corpus[index - 3 : index + 4]) 
       print(contexts) 
      else: 
       pass 

输出:

'buy' : stubborn urge to buy items like fruits, 
'time' : yourself spending more time on your phone 
'glass' : as many different glass styles are available, 
1
def keywordsContext(file, fileName): 

    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 = corpus.index(keywords) 
       contexts = keywords+":", corpus[index-3:index+4] 
       print(contexts) 
      else: 
       pass 

输出

('buy:', ['stubborn', 'urge', 'to', 'buy', 'items', 'like', 'fruits,']) 
('time:', ['yourself', 'spending', 'more', 'time', 'on', 'your', 'phone']) 
('glass:', ['as', 'many', 'different', 'glass', 'styles', 'are', 'available,']) 
相关问题