【Python】词频统计

前端之家收集整理的这篇文章主要介绍了【Python】词频统计前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  • 需求:一篇文章,出现了哪些词?哪些词出现得最多?

英文文本词频统计

英文文本:Hamlet 分析词频

统计英文词频分为两步:

  • 文本去噪及归一化
  • 使用字典表达词频

代码

  1. #CalHamletV1.py
  2. def getText():
  3. txt = open("hamlet.txt","r").read()
  4. txt = txt.lower()
  5. for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
  6. txt = txt.replace(ch," ") #将文本中特殊字符替换为空格
  7. return txt
  8. hamletTxt = getText()
  9. words = hamletTxt.split()
  10. counts = {}
  11. for word in words:
  12. counts[word] = counts.get(word,0) + 1
  13. items = list(counts.items())
  14. items.sort(key=lambda x:x[1],reverse=True)
  15. for i in range(10):
  16. word,count = items[i]
  17. print ("{0:<10}{1:>5}".format(word,count))

运行结果:

  1. the 1138
  2. and 965
  3. to 754
  4. of 669
  5. you 550
  6. i 542
  7. a 542
  8. my 514
  9. hamlet 462
  10. in 436

中文文本词频统计

中文文本:《三国演义》分析人物

统计中文词频分为两步:

  • 中文文本分词
  • 使用字典表达词频
  1. #CalThreeKingdomsV1.py
  2. import jieba
  3. txt = open("threekingdoms.txt","r",encoding='utf-8').read()
  4. words = jieba.lcut(txt)
  5. counts = {}
  6. for word in words:
  7. if len(word) == 1:
  8. continue
  9. else:
  10. counts[word] = counts.get(word,reverse=True)
  11. for i in range(15):
  12. word,count))

运行结果:

  1. 曹操 953
  2. 孔明 836
  3. 将军 772
  4. 却说 656
  5. 玄德 585
  6. 关公 510
  7. 丞相 491
  8. 二人 469
  9. 不可 440
  10. 荆州 425
  11. 玄德曰 390
  12. 孔明曰 390
  13. 不能 384
  14. 如此 378
  15. 张飞 358

能很明显的看到有一些不相关或重复的信息

优化版本

统计中文词频分为三步:

  • 中文文本分词
  • 使用字典表达词频
  • 扩展程序解决问题

我们将不相关或重复的信息放在 excludes 集合里面进行排除。

  1. #CalThreeKingdomsV2.py
  2. import jieba
  3. excludes = {"将军","却说","荆州","二人","不可","不能","如此"}
  4. txt = open("threekingdoms.txt",encoding='utf-8').read()
  5. words = jieba.lcut(txt)
  6. counts = {}
  7. for word in words:
  8. if len(word) == 1:
  9. continue
  10. elif word == "诸葛亮" or word == "孔明曰":
  11. rword = "孔明"
  12. elif word == "关公" or word == "云长":
  13. rword = "关羽"
  14. elif word == "玄德" or word == "玄德曰":
  15. rword = "刘备"
  16. elif word == "孟德" or word == "丞相":
  17. rword = "曹操"
  18. else:
  19. rword = word
  20. counts[rword] = counts.get(rword,0) + 1
  21. for word in excludes:
  22. del counts[word]
  23. items = list(counts.items())
  24. items.sort(key=lambda x:x[1],count))

考研英语词频统计

将词频统计应用到考研英语中,我们可以统计出出现次数较多的关键单词。
文本链接: https://pan.baidu.com/s/1Q6uVy-wWBpQ0VHvNI_DQxA 密码: fw3r

  1. # CalHamletV1.py
  2. def getText():
  3. txt = open("86_17_1_2.txt"," ") #将文本中特殊字符替换为空格
  4. return txt
  5. pyTxt = getText() #获得没有任何标点的txt文件
  6. words = pyTxt.split() #获得单词
  7. counts = {} #字典,键值对
  8. excludes = {"the","a","of","to","and","in","b","c","d","is",\
  9. "was","are","have","were","had","that","for","it",\
  10. "on","be","as","with","by","not","their","they",\
  11. "from","more","but","or","you","at","has","we","an",\
  12. "this","can","which","will","your","one","he","his","all","people","should","than","points","there","i","what","about","new","if","”",\
  13. "its","been","part","so","who","would","answer","some","our","may","most","do","when","1","text","section","2","many","time","into",\
  14. "10","no","other","up","following","【答案】","only","out","each","much","them","such","world","these","sheet","life","how","because","3","even",\
  15. "work","directions","use","could","now","first","make","years","way","20","those","over","also","best","two","well","15","us","write","4","5","being","social","read","like","according","just","take","paragraph","any","english","good","after","own","year","must","american","less","her","between","then","children","before","very","human","long","while","often","my","too",\
  16. "40","four","research","author","questions","still","last","business","education","need","information","public","says","passage","reading","through","women","she","health","example","help","get","different","him","mark","might","off","job","30","writing","choose","words","economic","become","science","society","without","made","high","students","few","better","since","6","rather","however","great","where","culture","come",\
  17. "both","three","same","government","old","find","number","means","study","put","8","change","does","today","think","future","school","yet","man","things","far","line","7","13","50","used","states","down","12","14","16","end","11","making","9","another","young","system","important","letter","17","chinese","every","see","s","test","word","century","language","little",\
  18. "give","said","25","state","problems","sentence","food","translation","given","child","18","longer","question","back","don’t","19","against","always","answers","know","having","among","instead","comprehension","large","35","want","likely","keep","family","go","why","41","home","law","place","look","day","men","22","26","45","it’s","others","companies","countries","once","money","24","though",\
  19. "27","29","31","say","national","ii","23","based","found","28","32","past","living","university","scientific","–","36","38","working","around","data","right","21","jobs","33","34","possible","feel","process","effect","growth","probably","seems","fact","below","37","39","history","technology","never","sentences","47","true","scientists","power","thought","during","48","early","parents",\
  20. "something","market","times","46","certain","whether","000","did","enough","problem","least","federal","age","idea","learn","common","political","pay","view","going","attention","happiness","moral","show","live","until","52","49","ago","percent","stress","43","44","42","meaning","51","e","iii","u","60","anything","53","55","cultural","nothing","short","100","water","car","56","58","【解析】","54","59","57","v","。","63","64","65","61","62","66","70","75","f","【考点分析】","67","here","68","71","72","69","73","74","选项a","ourselves","teachers","helps","参考范文","gdp","yourself","gone","150"}
  21. for word in words:
  22. if word not in excludes:
  23. counts[word] = counts.get(word,count))
  24. x = len(counts)
  25. print(x)
  26. r = 0
  27. next = eval(input("1继续"))
  28. while next == 1:
  29. r += 100
  30. for i in range(r,r+100):
  31. word,count = items[i]
  32. print ("\"{}\"".format(word),end = ",")
  33. next = eval(input("1继续"))

猜你在找的Python相关文章