1.txt、2.txt などの番号が付けられた約 20000 個のテキスト ファイルがあります。
今、ファイル5.txt、10.txt、15.txtなどへのファイルパスを含む辞書dを作成しています..
d[value]=filepath
ex:
d[5]=d:/articles/5.txt
d[45]=d:/articles/45.txt
そして、500単語のリストを含むテキストファイル「temp.txt」があります
vs
mln
money
等々..
辞書「d」の各テキスト ファイルについて、リスト内のすべての単語の出現頻度を記録する必要があります。
そこで、d2[word][file]=count という形式のネストされた辞書を作成します (これは正しいアプローチですか?)
where, d2[vs][5]=number of times "vs" occurs in 5.txt
つまり、すべてのファイルについて、単語のリストを繰り返し処理し、その出現回数を数えます。
d2 を作成するにはどうすればよいですか?
私の悪いコードは次のとおりです。
import collections, sys, os, re
sys.stdout=open('3.txt','w')
from collections import Counter
from glob import glob
folderpath='d:/individual-articles'
folderpaths='d:/individual-articles/'
counter=Counter()
filepaths = glob(os.path.join(folderpath,'*.txt'))
# returns the next word in the file
def words_generator(fileobj):
for line in fileobj:
for word in line.split():
yield word
d= collections.defaultdict(list)
#to print the filenames:(creation of d)
with open('topics.txt','r') as f:
for line in f.readlines():
value=(line.split('~')[0])
if int(value)%5==0:
file=folderpaths+value+'.txt'
d[value].append(file)
d2= collections.defaultdict(list)
for file in filepaths:
f = open(file,"r")
words = words_generator(f)
for word in words:
if file in d[file]:
d2[word][file]+= 1
#i have no idea how to go further, beyond this point.
助けてください!!