2017-10-17 23 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,']) 
相關問題