Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to find frequency of the keys in a dictionary across multiple text files?
    text
    copied!<p>I am supposed to count the frequency of all the key values of dictionary "d" across all the files in the document "individual-articles' Here,the document "individual-articles' has around 20000 txt files,with filenames 1,2,3,4... for ex: suppose d[Britain]=[5,76,289] must return the number of times Britain appears in the files 5.txt,76.txt,289.txt belonging to the document "induvidual articles", and also i need to find its frequency across all the files in the same document. i need to store these values in another d2 for the same example, d2 must contain (Britain,26,1200) where 26 is the frequency of the word Britain in the files 5.txt,76.txt and 289.txt and 1200 is the frequency of the word Britain in all the files. I am a python newbie, and i have tried little! please help!!</p> <pre><code>import collections import sys import os import re sys.stdout=open('dictionary.txt','w') from collections import Counter from glob import glob def removegarbage(text): text=re.sub(r'\W+',' ',text) text=text.lower() sorted(text) return text folderpath='d:/individual-articles' counter=Counter() filepaths = glob(os.path.join(folderpath,'*.txt')) d2={} with open('topics.txt') as f: d = collections.defaultdict(list) for line in f: value, *keys = line.strip().split('~') for key in filter(None, keys): d[key].append(value) for filepath in filepaths: with open(filepath,'r') as filehandle: lines = filehandle.read() words = removegarbage(lines).split() for k in d.keys(): d2[k] = words.count(k) for i in d2.items(): print(i) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload