作業內容
統計英語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]))

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/258358.html
標籤:python
