• 文件方式实现完整的英文词频统计实例(9.27)


    1.读入待分析的字符串

    2.分解提取单词 

    3.计数字典

    4.排除语法型词汇

    5.排序

    6.输出TOP(20)

    文本代码如下:

    girl='''Remembering me, Discover and see All over the world, She's known as a girl To those who a free, The mind shall be key Forgotten as the past 'Cause history will last
    
    God is a girl, Wherever you are, Do you believe it, can you recieve it? God is a girl, Whatever you say, Do you believe it, can you recieve it? God is a girl.'''

    实现代码如下:

    fo=open('daili.txt','r')
    girl=fo.read()
    girl='''Remembering me, Discover and see All over the world, She's known as a girl To those who a free, The mind shall be key Forgotten as the past 'Cause history will last
    
    God is a girl, Wherever you are, Do you believe it, can you recieve it? God is a girl, Whatever you say, Do you believe it, can you recieve it? God is a girl.'''
    exc={'','a','the','and','is','as','you','me','do'}
    
    girl=girl.lower()
    for i in ',?': 
     girl=girl.replace(i,' ')
    words=girl.split(' ')
    print('歌词:
    ',words)
    
    dict={}
    keys=set(words)
    keys=keys-exc
    print('最终单词:
    ',keys)
    
    
    for i in words:
     dict[i]=words.count(i)
    print('统计单词结果:
    ',dict)
    
    
    dai=list(dict.items())
    dai.sort(key=lambda x:x[1],reverse=True)
    print('排序结果:
    ')
    
    
    for i in range(20):
     print(dai[i])
    
    fo.close()

    程序结果如下:

  • 相关阅读:
    pm2进阶使用
    javascript装饰器模式
    pupeteer初体验
    重构:从Promise到Async/Await
    # electron-vue 尝试做个网易云音乐
    Kafka监控:主要性能指标
    生产环境Rabbitmq集群安装部署与配置
    Java同步块(synchronized block)
    RabbitMQ高可用镜像队列
    kafka-0.9消费者新API
  • 原文地址:https://www.cnblogs.com/laidaili/p/7595295.html
Copyright © 2020-2023  润新知