统计英语6级试题中所有单词的词频,并返回一个如下样式的字典
{
'and':100,'abandon':5}
英语6级试题的文件路径./artical.txt
Tip: 读取文件的方法
def get_artical(artical_path):
with open(artical_path) as fr:
data = fr.read()
return data
get_artical('./artical.txt')
# 请根据处理要求下面区域完成代码的编写。
def get_artical(artical_path):
with open(artical_path) as fr:
data = fr.read().lower()
return data
# get_artical()为自定义函数,可用于读取指定位置的试题内容。
mystr=get_artical('./artical.txt')
str1=['.', ',', '!', '?', ';', '\'', '\"', '/', '-', '(', ')']
str2=['1','2','3','4','5','6','7','8','9','0']
for ch in str1:
mystr=mystr.replace(ch," ")
for ch in str2:
mystr=mystr.replace(ch," ")
words=mystr.split()
counts={
}
for w in words:
if w!=' ':
counts[w]=counts.get(w,0)+1
for key in counts:
print("{} , {}".format(key, counts[key]))