import os
import re
import sys
sys.stdout=open('f1.txt','w')
from collections import Counter
from glob import glob
def removegarbage(text):
text=re.sub(r'\W+',' ',text)
text=text.lower()
return text
folderpath='d:/induvidual-articles'
counter=Counter()
filepaths = glob(os.path.join(folderpath,'*.txt'))
num_files = len(filepaths)
with open('topics.txt','r') as filehandle:
lines = filehandle.read()
words = removegarbage(lines).split()
counter.update(words)
for word, count in counter.most_common():
probability=count//num_files
print('{} {} {}'.format(word,count,probability))
我得到一个零除错误:对于 lineprobability=count//num_files 的浮点数除以零
我该如何纠正它?
我需要我的输出形式为:单词、计数、概率
请帮忙!