m0_73841443 2023-06-05 22:28 采纳率: 100%
浏览 111
已结题

python文本词汇出现次数统计

从云南大学校园网上下载一段关于云南大学的介绍文本,将文本保存在名称为“云南大学.txt”文本文件中,注意保存时文件编码要选“UTF-8”。编写程序完成对“云南大学.txt”文件中词汇的出现次数的统计,要求将长度为1的词去掉,并去掉'高校','大学'两个词,按各词出现次数从高到低排序后输出前20项。

img

  • 写回答

7条回答 默认 最新

  • 语言-逆行者 2023-06-05 23:26
    关注

    jieba实现,应该符合你的要求:

    img

    import jieba
    from collections import Counter
    
    # 打开文件
    with open('云南大学.txt', 'r', encoding='utf-8') as f:
        # 读取文件内容为字符串
        text = f.read()
    
    # 对文本进行分词并去除长度为1的词和'高校'、'大学'两个词
    words = []
    for word in jieba.cut(text):
        if len(word) > 1 and word not in ('高校', '大学'):
            words.append(word)
    
    # 统计词频并排序
    word_counts = Counter(words)
    sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
    
    # 输出前20项
    for word, count in sorted_word_counts[:20]:
        print(f'{word}: {count}')
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
  • threenewbee 2023-06-05 22:43
    关注

    这个需要分词,可以用jieba分词
    然后统计就简单了,参考

    # -*-coding:utf-8 -*-
     
    import os
    import csv
    import codecs
    import jieba
    import jieba.posseg as pseg
     
    os.chdir(r'C:\Users\zhuchaoming\Desktop\任务\其他文件\分词')
    txt = open("大学.txt", encoding="gb18030").read()
     
    #词频统计
    stopwords = [line.strip() for line in open("stopword.txt",encoding="gb18030").readlines()]
    cixing = pseg.lcut(txt)
    count  = jieba.lcut(txt)
    word_count = {}
    word_flag = {}
    all=[]
     
     
    with codecs.open(filename='daxue_word_count_cixing.csv', mode='w', encoding='gb18030')as f:
        write = csv.writer(f, dialect='excel')
        write.writerow(["word","count","flag"])
     
        #词性统计
        for w in cixing:
            word_flag[w.word] = w.flag
     
        #词频统计
        for word in count:
            if word not in stopwords:
                    # 不统计字数为一的词
                    if len(word) == 1:
                        continue
                    else:
                        word_count[word] = word_count.get(word, 0) + 1
     
     
        items = list(word_count.items())
        #按词频排序
        items.sort(key=lambda x: x[1], reverse=True)
        #查询词频字典里关键字的词性
        for i in range(len(items)):
            word=[]
            word.append(items[i][0])
            word.append(items[i][1])
            # 若词频字典里,该关键字有分辨出词性,则记录,否则为空
            if items[i][0] in word_flag.keys():
                word.append(word_flag[items[i][0]])
            else:
                word.append("")
            all.append(word)
     
     
        for res in all:
            write.writerow(res)
    

    https://blog.csdn.net/zcmlimi/article/details/90671005

    评论
  • 哦里刻录机 2023-06-05 22:46
    关注
    
    from collections import Counter
    
    with open('云南大学.txt', 'r', encoding='utf-8') as f:
        text = f.read()
    
    text = re.sub('\s+[a-zA-Z]\s+', ' ', text)
    text = re.sub('高校|大学', '', text)
    words = text.split()
    word_count = Counter(words)
    top20 = word_count.most_common(20)
    
    print(top20)
    
    评论
  • 全栈若城 Python领域新星创作者 2023-06-05 22:55
    关注

    运行效果如图所示:

    img

    核心代码 采用结巴分词 :

     # 统计每个词语的出现次数
    word_count = {}
    for word in words:
        if len(word) > 1 and word not in ['高校', '大学']:
            if word in word_count:
                word_count[word] += 1
            else:
                word_count[word] = 1
    

    完整代码

    # 词频统计
    import jieba
    
    # 读取文本
    with open('云南大学.txt', 'r', encoding='utf-8') as f:
        text = f.read()
    
    # 对文本进行分词
    words = jieba.cut(text)
    
    # 统计每个词语的出现次数
    word_count = {}
    for word in words:
        if len(word) > 1 and word not in ['高校', '大学']:
            if word in word_count:
                word_count[word] += 1
            else:
                word_count[word] = 1
    
    # 对词语出现次数进行排序
    sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
    
    # 输出前20项
    for i in range(20):
        print(sorted_word_count[i][0], sorted_word_count[i][1])
    
    
    评论 编辑记录
  • Python小叮当 2023-06-06 08:17
    关注

    jieba包你值得拥有

    评论
  • 故事不长丨 2023-06-06 09:30
    关注
    import re
    from collections import Counter
    
    # 读取文件内容
    with open('云南大学.txt', 'r', encoding='utf-8') as f:
        text = f.read()
    
    # 将文本中的标点符号替换为空格
    text = re.sub(r'[^\u4e00-\u9fa5a-zA-Z0-9]+', ' ', text)
    
    # 将文本分词,去除长度为1的词和'高校'、'大学'
    words = [word for word in text.split() if len(word) > 1 and word != '高校' and word != '大学']
    
    # 统计词频并排序
    word_count = Counter(words)
    sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
    
    # 输出前20个词汇及其出现次数
    for i in range(20):
        print(sorted_word_count[i][0], sorted_word_count[i][1])
    

    其中,re.sub()函数用于替换文本中的标点符号为空格,Counter()函数用于统计词频,sorted()函数用于对词频进行排序。最终输出前20个词汇及其出现次数。

    评论
  • MR_Bone 2023-06-07 10:57
    关注

    根据您的要求,您想要编写一个程序来统计文件"云南大学.txt"中词汇的出现次数,并按照出现次数从高到低排序后输出前20个词。以下是实现这个任务的步骤:

    1. 读取文件:使用编程语言(如Python)中的文件读取功能,打开并读取"云南大学.txt"文件中的文本内容。
    2. 文本处理:对读取的文本内容进行处理,包括分词和去除指定的词汇。可以使用分词工具(如jieba分词库)对文本进行分词,并去除长度为1的词和指定的词汇(如'高校'和'大学')。
    3. 统计词频:使用字典或哈希表数据结构,遍历处理后的词汇列表,统计每个词汇的出现次数。
    4. 排序输出:对词频进行排序,并输出前20个出现次数最多的词汇。
    5. 保存结果:将排序后的词汇和词频结果保存到一个文件中,以便后续查看和使用。

    请注意,在实际编写程序时,您需要选择适合您所使用的编程语言和相关库的方法和函数来完成上述步骤。

    评论
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 6月14日
  • 已采纳回答 6月6日
  • 请采纳用户回复 6月6日
  • 请回答用户的提问 6月5日
  • 展开全部

悬赏问题

  • ¥15 eclipse无法正常运行
  • ¥15 定义了函数,但是无法根据函数定义触发器
  • ¥20 5变量卡诺图化简得出与非门电路图
  • ¥20 位置依赖的碱基序列独热编码
  • ¥15 Python爬取交通拥堵指数数据
  • ¥15 使用vba抓取重定向网页问题
  • ¥20 付费需求测试程序(细谈)。
  • ¥15 为什么这段c++代码会报这么多语法错误?
  • ¥20 如何利用C语言实现用最小二乘法选配两个经验公式
  • ¥50 vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?