Pythonを使用して、ドキュメントのすべてのフレーズをカウントします

ファイル内のすべてのフレーズの出現回数と出現回数をカウントし、最大から最小に並べ替えて、上位10個の最大のフレーズと回数を出力します。
ここに画像の説明を挿入します

test6.pyファイル

# 统计词组频率
# 1:文字章节
# 2:建立空字典——用于存放词频的计算
# 3:对文本每一行计算词频
# 4:从字典中获取数据对到列表中
# 5:对列表中的数据对交换位置,并且从大到小进行排序
# 6:输出结果
# 设置最后显示的统计前10的词组
count= 10
# 空字典,用于存放统计的词组,和词组次数
wordCounts = {
    
    }
# 统计函数()
def processLine(line,wordCounts):
    # 更换标点符号为空格
    line = replacePunctuations(line)
    # 使用空格为切割符号分组
    words = line.split()
    # 遍历词组,有则增加1,无则添加
    # 词组为键
    # 频次为值
    for w in words:
        if w in wordCounts:
            wordCounts[w] +=1
        else:
            wordCounts[w] =1
# 把该行的标点符号更换为空格
def replacePunctuations(line):
    for i in line:
        if i in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'""":
            line = line.replace(i," ")
        return line
# 打开文件函数
def main():
    # 第一种自定义文件
    # fileName = input("请输入需要统计的文件名字:").strip()
    # 第二种指定文件 在相同的目录下
    fileName = "intel.txt"
    openfile = open(fileName,"r")
    # 1.遍历 每一行在文件中,
    # 2.转换每一行的大写为小写
    # 3.wordCounts为一个空字典
    for line in openfile:
        processLine(line.lower(),wordCounts)
    # 经过上面的处理之后,我们得到一个统计出来的词组,和词组次数
    # 因为字典是无序的,所以我们需要把它转换为一个有序的列表
    # paris为该字典转换出来的列表数据,
    # 得到一个列表里面包含多个元组的数据
    # 格式如[('intel', 2), ('technologies', 1)]
    pairs = list(wordCounts.items())
    # 转换这里面的每个元组前后顺序
    # 得到一个列表里面包含多个列表的数据
    # 格式如[[2, 'intel'], [1, 'technologies']]
    items = [[a,b]for(b,a)in pairs]
    # 从小到大排序
    # 格式如[[1, 'a'], [1, 'absolutely']]
    items.sort()

    # 统计出最多出现频次的前10个词
    # range()
    print("这里面一共有多少个不同的词:",len(items))
    # len(items)为总长度如67
    # len(items)-1为66
    # len(items)-count-1为56
    # 因为是range(a,b,c)    
    # a,b参数为下标  
    # c的参数为排列顺序,-1为倒叙,并且每个相隔1个参数
        # c的参数为排列顺序,-2为倒叙,并且每个相隔2个参数
    # range(66,56,-1)
    for i in range(len(items)-1,len(items)-count-1,-1):
        # 上面已经设置了词组排序词组在后频次在前,所以这里需要设置下每个列表的前后顺序
        print(items[i][1]+"\t"+str(items[i][0]))
    openfile.close()
main()

intel.txtファイル

Intel technologies may require enabled hardware, software or service activation. // No product or component can be absolutely secure. // Your costs and results may vary. // Performance varies by use, configuration and other factors. // See our complete legal notices and disclaimers. // Intel is committed to respecting human rights and avoiding complicity in human rights abuses. See Intel’s Global Human Rights Principles. Intel’s products and software are intended only to be used in applications that do not cause or contribute to a violation of an internationally recognized human right.
Intel technologies may require enabled hardware, software or service activation. // No product or component can be absolutely secure. // Your costs and results may vary. // Performance varies by use, configuration and other factors. // See our complete legal notices and disclaimers. // Intel is committed to respecting human rights and avoiding complicity in human rights abuses. See Intel’s Global Human Rights Principles. Intel’s products and software are intended only to be used in applications that do not cause or contribute to a violation of an internationally recognized human right.

おすすめ

転載: blog.csdn.net/weixin_47021806/article/details/113982823