2016-12-15 193 views
0

我有一個包含一些其他文件夾的文件夾,每個文件夾都包含大量文本文件,大約32214個文件。我想在特定單詞前後打印5個單詞,我的代碼應該讀取所​​有這些文件。下面的代碼工作正常,但大約需要8個小時才能閱讀所有文件並提取語句。我如何更改代碼以便在幾分鐘之內讀取和打印句子? (語言爲波斯語)在文本文件中的特定單詞之前和之後獲取單詞

. 
. 
. 
def extact_sentence(): 
    f= open ("پاکت", "w", encoding = "utf-8") 
    y = "پاکت" 
    text= normal_text(folder_path) # the first function to normalize the files 
    for i in text: 
     for line in i: 
      split_line = line.split() 
      if y in split_line: 
       index = split_line.index(y) 
       d = (' '.join(split_line[max(0,index-5):min(index+6,len(split_line))])) 
       f.write(d + "\n") 
    f.close() 

enter image description here

+0

什麼讓您認爲每個文件可以在1/1000秒內處理? –

+0

我不確定你能做些什麼來加速代碼本身,我認爲瓶頸可能是硬盤驅動器訪問,它可以加快asyncio的速度,但我懷疑它會大大提升速度 –

+0

''normal_text'是否一次將所有文件加載到內存中?你寫了'normal_text',那裏的優化可能是可行的。 –

回答

0

使用os.walk訪問所有文件。然後在每個文件上使用滾動窗口,並檢查每個窗口的中間詞:

import os 


def getRollingWindow(seq, w): 
    win = [next(seq) for _ in range(window_size)] 
    yield win 
    for e in seq: 
     win[:-1] = win[1:] 
     win[-1] = e 
     yield win 


def extractSentences(rootDir, searchWord): 
    with open("پاکت", "w", encoding="utf-8") as outfile: 
     for root, _dirs, fnames in os.walk(rootDir): 
      for fname in fnames: 
       print("Looking in", os.path.join(root, fname)) 
       with open(os.path.join(root, fname)) as infile: 
        for window in getRollingWindow(word for line in infile for word in line.split(), 11): 
         if window[5] != searchWord: continue 
         outfile.write(' '.join(window)) 
+0

NameError:name'infile'未定義 – sara

+0

@sara:哎呀!現在修正了 – inspectorG4dget

+0

對不起,但是又有一個錯誤:win = [seq.next()for cnt in range(11)] AttributeError:'generator'object has no attribute'next' – sara

相關問題