期末综合大作业:词频统计

#1
bigFile= open('Back to December.txt', mode='r', encoding='utf-8')
bigText=bigFile.read()
bigFile.read()

#2
replaceList=['?',',','.','!',':',"'",'"',' ','\ufeff','\n']
for c in replaceList:
    bigText=bigText.replace(c,' ')
print(bigText)

#3
print(bigText.split(' '))
bigList=bigText.split(' ')


#4
bigSet=set(bigList)
print(bigSet)
bigDict={}
for word in bigSet:
    bigDict[word]=bigList.count(word)
print(bigDict)
for d in bigDict:
    print(d,bigDict[d])

#5
wordCountList=list(bigDict.items())
print(wordCountList)
wordCountList.sort(key=lambda x:x[1],reverse=True)
print(wordCountList)

#6
for i in range(20):
    print(wordCountList[i])

#7
bigCountFile=open('bigCount.txt',mode='a',encoding='utf-8')
for i in range(len(wordCountList)):
    bigCountFile.write(str(wordCountList[i][1])+' '+wordCountList[i][0]+'\n')
bigCountFile.close()

期末综合大作业:词频统计_第1张图片

你可能感兴趣的:(期末综合大作业:词频统计)