我正在编写一个代码,用于计算包含大约20,000个文件的文档中出现单词的频率,我能够获得文档中单词的整体频率和 到目前为止我的代码是:
import os
import re
import sys
sys.stdout=open('f2.txt','w')
from collections import Counter
from glob import iglob
def removegarbage(text):
text=re.sub(r'\W+',' ',text)
text=text.lower()
return text
folderpath='d:/articles-words'
counter=Counter()
d=0
for filepath in iglob(os.path.join(folderpath,'*.txt')):
with open(filepath,'r') as filehandle:
d+=1
r=round(d*0.1)
for filepath in iglob(os.path.join(folderpath,'*.txt')):
with open(filepath,'r') as filehandle:
words=set(removegarbage(filehandle.read()).split())
if r > counter:
counter.update()
for word,count in counter.most_common():
print('{} {}'.format(word,count))
但是,我想修改我的计数器,只有当计数大于r = 0.1 *(没有文件)时才更新它 简而言之,我想阅读整个文档中频率大于文档数量10%的单词。 错误是:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
TypeError: unorderable types: int() > Counter()
如何修改它?
答案 0 :(得分:2)
这样的事情怎么样?
from glob import glob # (instead of iglob)
...
filepaths = glob(os.path.join(folderpath,'*.txt'))
num_files = len(filepaths)
# Add all words to counter
for filepath in filepaths):
with open(filepath,'r') as filehandle:
lines = filehandle.read()
words = removegarbage(lines).split()
counter.update(words)
# Display most common
for word, count in counter.most_common():
# Break out if the frequency is less than 0.1 * the number of files
if count < 0.1*num_files:
break
print('{} {}'.format(word,count))
使用Counter.iteritems()
:
>>> from collections import Counter
>>> c = Counter()
>>> c.update(['test', 'test', 'test2'])
>>> c.iteritems()
<dictionary-itemiterator object at 0x012F4750>
>>> for word, count in c.iteritems():
... print word, count
...
test 2
test2 1