我正在编写一个代码,以计算包含大约 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*(no of files) 时才更新它,简而言之,我想读取整个文档中频率大于 10% 的单词的文件。错误是:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
TypeError: unorderable types: int() > Counter()
我该如何修改它?