哈姆雷特 hamlet.txt文本分析


 

哈姆雷特文本分析记录:

def getText():
    txt = open("hamlet.txt","r").read()  #打开文件 r 读权限
    txt = txt.lower()                    #把英文字母全部变成小写
    for ch in '!"$%&()*+,-./:;<=>?@[\\]^_{|}~':
        txt = txt.replace(ch," ")        #特殊符号替换为空格
    return txt
 
hamletTxt = getText()         
words = hamletTxt.split()      #split默认以空格为分隔符,返回列表
counts = {}             #定义一个空字典类型,因为一个单词和对应的出现次数
for word in words:  #循环取出单词放到空字典当作key
    counts[word] = counts.get(word,0) +1  #用key查询出现次数,每出现一次+1(如果不存在返回0)
items = list(counts.items())  #取出字典的键和值 并返回列表类型
print(items)            #[('the', 1138), ('tragedy', 3)]
items.sort(key=lambda x:x[1],reverse=True) #排序字典中的value,出现次数
for i in range(10):
    word,count = items[i]
    print("{0:<6}{1:>9}".format(word,count))

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM