zoukankan      html  css  js  c++  java
  • 统计文本词频

    方法一:

    #将文本内容转换为字典进行统计
    file01 = open('art.txt','r')
    list = file01.read().replace(',','').replace('.','').replace(';','').split()    #读取文件去除文本中的特殊符号并切片
    list01 = {}
    for i in list:  #生成字典,单词为keys,出现的次数为value
        if i in list01.keys():
            list01[i] = list01[i] + 1
        else:
            list01[i] = 1
    
    a = sorted(list01.items(), key=lambda va:va[1],reverse=True)    #排序
    count = 0
    for j in a:
        if count <5:
            print('单词 %s 出现了 %d 次' % (j[0],j[1]))   #打印前5名
            count += 1
        else:
            break
    file01.close()

    方法二:

    #将文本内容转换为列表进行统计
    from collections import Counter
    file = open('art.txt','r')
    list01 =  file.read().replace(',','').replace('.','').replace(';','').split()   #读取文件去除文本中的特殊符号并切片
    a = Counter(list01)     #排序
    b = a.most_common(5)    #取出前5名
    for i in b:
        print('单词 %s 出现了 %d 次' % (i[0], i[1]))
    file01.close()
    

    输出结果:

    单词 the 出现了 6 次
    单词 of 出现了 5 次
    单词 in 出现了 3 次
    单词 to 出现了 3 次
    单词 something 出现了 3 次
    

      

  • 相关阅读:
    进程与线程的区别与联系
    c 指针兼容性问题
    柔性数组
    Makefile之wildcard
    shell编程笔记1
    linux下gcc编译的参数详细说明
    的理解
    URL与URI的区别
    Log4J积累
    linux 查看磁盘、文件夹、文件大小(df du)
  • 原文地址:https://www.cnblogs.com/jacky-zhao/p/8244117.html
Copyright © 2011-2022 走看看