2018-12-12 词频统计

Hamlet词频统计(含Hamlet原文文本)

#CalHamletV1.py

defgetText():

    txt =open("hamlet.txt", "r").read()

    txt =txt.lower()

    forch in'!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':

        txt =txt.replace(ch, " ")   #将文本中特殊字符替换为空格

    returntxt


hamletTxt =getText()

words  =hamletTxt.split()

counts ={}

forword inwords:          

    counts[word] =counts.get(word,0) +1

items =list(counts.items())

items.sort(key=lambdax:x[1], reverse=True) 

fori inrange(10):

    word, count =items[i]

    print("{0:<10}{1:>5}".format(word, count))


《三国演义》人物出场统计(上)(含《三国演义》原文文本)

#CalThreeKingdomsV1.py

importjieba

txt =open("threekingdoms.txt", "r", encoding='utf-8').read()

words  =jieba.lcut(txt)

counts ={}

forword inwords:

    iflen(word) ==1:

        continue

    else:

        counts[word] =counts.get(word,0) +1

items =list(counts.items())

items.sort(key=lambdax:x[1], reverse=True) 

fori inrange(15):

    word, count =items[i]

    print("{0:<10}{1:>5}".format(word, count))


《三国演义》人物出场统计(下)(含《三国演义》原文文本)

#CalThreeKingdomsV2.py

importjieba

excludes ={"将军","却说","荆州","二人","不可","不能","如此"}

txt =open("threekingdoms.txt", "r", encoding='utf-8').read()

words  =jieba.lcut(txt)

counts ={}

forword inwords:

    iflen(word) ==1:

        continue

    elifword =="诸葛亮"orword =="孔明曰":

        rword ="孔明"

    elifword =="关公"orword =="云长":

        rword ="关羽"

    elifword =="玄德"orword =="玄德曰":

        rword ="刘备"

    elifword =="孟德"orword =="丞相":

        rword ="曹操"

    else:

        rword =word

    counts[rword] =counts.get(rword,0) +1

forword inexcludes:

    delcounts[word]

items =list(counts.items())

items.sort(key=lambdax:x[1], reverse=True) 

fori inrange(10):

    word, count =items[i]

    print("{0:<10}{1:>5}".format(word, count))

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 个人学习批处理的初衷来源于实际工作;在某个迭代版本有个BS(安卓手游模拟器)大需求,从而在测试过程中就重复涉及到...
    Luckykailiu阅读 4,951评论 0 11
  • 用于python面试整理,主要来源于http://www.cnblogs.com/skiler/p/6952707...
    AIM外星人阅读 2,463评论 0 13
  • 个人健康管控
    abbapw阅读 168评论 1 0
  • 一、参考为什么不建议用 try catch try catch机制非常好。那些觉得try catch不行的人,是他...
    合肥黑阅读 2,629评论 0 0
  • “得不到的永远在骚动,被偏爱的都有恃无恐......”听着熟悉的旋律,一个人默默地走在午后的林荫路下,不自...
    Wolfstyle阅读 837评论 0 1

友情链接更多精彩内容