zoukankan      html  css  js  c++  java
  • 综合练习:英文词频统计

    # -*- coding:UTF-8 -*-
    # -*- author:deng -*-
    news = '''
    The only problem unconsciously assumed by all Chinese philosophers to be of
    any importance is:How shall we enjoy life, and who can best enjoy life? No
    perfectionism, no straining after the unattainable, no postulating of he
    unknowable; but taking poor, modal human nature as it is, how shall we
    organize our life so that we can woke peacefully, endure nobly and live happily?
    Who are we? That is first question. It is a question almost impossible to answer.
    But we all agree with the busy self occupied in our daily activities is not quite
    the real self. We are quite sure we have lost something in the mere pursuit of living.
    When we watch a person running about looking for something in a field, the wise man can
    set a puzzle for all the spectator to solve: what has that person lost? Some one thinks
    is a watch; another thinks it is a diamond brooch; and others will essay other guesses.
    After all the guesses have failed, the wise man who really doesn't know what the person
    is seeking after, tells the company:" I'll tell you. He has lost some breath." And no one
    can deny that he is right. So we often forget our true self in the pursuit of living, like
    a bird forgetting its own danger in pursuit of a mantis which again forgets its own danger
    in pursuit of another.
    '''
    # 将分隔符替换为空格
    symbol=[",",".","!","?",":","'"]
    for i in range(len(symbol)):
    news=news.replace(symbol[i]," ")
    print(news)
    wordList = news.lower().split()

    # 将所有大写转换为小写
    news = news.lower()
    print(news)

    # 生成单词列表
    news = news.split()
    print(news)


    # 生成词频统计

    dict = {}
    for w in wordList:
    dict[w] = dict.get(w,0)+1
    print(dict)

    # 排除语法型词汇,代词、冠词、连词
    word = ['the', 'by', 'to', 'be', 'of', 'and', 'with', 'not']
    for i in word:
    del dict[i]
    print(dict)

    # 输出词频top10
    dict1 = sorted(dict.items(), key=lambda x: x[1], reverse=True)
    for i in range(10):
    print(dict1[i])
  • 相关阅读:
    OpenGL简介及编程入门
    大数阶乘 我的ACM的第一步!
    解释Windows7“上帝模式”的原理
    斯特林[striling]公式(求阶乘(n!)的位数)
    VC中借助内嵌资源实现Flash动画播放
    VC MFC 精品文章收集!
    这应该是UFC有史以来最快的KO记录了
    深以为然
    PS3欧洲延期!全球同步发售幻想破灭
    Windows Vista RC1 NVIDIA drivers include OpenGL ICD
  • 原文地址:https://www.cnblogs.com/dfq621/p/8655087.html
Copyright © 2011-2022 走看看