Python学习49:词频统计

类型:文件‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬


描述‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

《谁动了我的奶酪?》是美国作家斯宾塞·约翰逊创作的一个寓言故事,该书首次出版于1998年。书中主要讲述4个“人物”—两只小老鼠“嗅嗅(Sniff)”、“匆匆(Scurry)”和两个小矮人“哼哼(Hem)”、“唧唧(Haw)”找寻奶酪的故事。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

文件“Who Moved My Cheese.txt”中包含这个故事的中英文,格式如下:

请按照函数的注释,补充程序中缺失部分语句,按要求实现如下程序功能:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

read_file()函数将文件中的内容读为字符串,过滤掉中文,只保留文件中的英文字母和西文符号(只保留ASCII码字符)。所有字符转为小写,将其中所有标点、符号替换为空格。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

count_of_words()函数统计read_file()函数返回的字符串中的单词数量和不重复的单词数量。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

top_ten_words()函数分行依次输出出现次数最多的n个单词及其出现次数。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

word_frequency()函数统计并以字典类型返回每个单词出现的次数。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

top_ten_words_no_excludes()函数统计并输出去除常见的冠词、代词、系动词和连接词后,出现次数最多的 cnt个单词及其出现次数。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

根据用户输入的指令和任务完成程序:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

若输入指令为“1”,则在下一行中输入一个非负整数n,并输出read_file()函数返回值的前n个字符。 若输入指令为“2”,并调用count_of_words()函数统计输出read_file()函数返回的字符串中的单词数量和不重复的单词数量,格式参考下面的输入输出示例,其中的XXXX需用统计出的实际数值替换。 若输入指令为“3”,则在下一行中输入一个正整数n,并调用top_ten_words()函数分行依次输出出现次数最多的n个单词及其出现次数,单词和次数之间以空格间隔。 若输入指令为“4”,则在下一行中输入一个非负整数n,并调用top_ten_words_no_excludes()函数从词频统计结果中去除常见的冠词、代词、系动词和连接词后统计词频,再输出出现次数最多的n个单词及其出现次数,单词和次数之间以空格间隔。需排除的单词包括:['a', 'an', 'the', 'i', 'he', 'she', 'his', 'my', 'we','or', 'is', 'was', 'do', and', 'at', 'to', 'of', 'it', 'on', 'that', 'her', 'c','in', 'you', 'had', 's', 'with', 'for', 't', 'but', 'as', 'not', 'they', 'be', 'were', 'so', 'our', 'all', 'would', 'if', 'him', 'from', 'no', 'me', 'could', 'when', 'there', 'them', 'about', 'this', 'their', 'up', 'been', 'by', 'out', 'did', 'have']

参考资料:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

【ASCII 码表】‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

0000-007F(0-127):C0控制符及基本拉丁文 (C0 Control and Basic Latin) 0080-00FF(128-255):C1控制符及拉丁文补充-1 (C1 Control and Latin 1 Supplement)

 

 

import string


def read_file(file):
    """接收文件名为参数,将文件中的内容读为字符串,
    只保留文件中的英文字母和西文符号,
    过滤掉中文(中文字符及全角符号Unicode编码都大于256)
    将所有字符转为小写,
    将其中所有标点、符号替换为空格,返回字符串
    """
    with open(file,'r',encoding='utf-8') as novel:
        txt=novel.read()
        etxt=''.join(x for x in txt if ord(x) < 256)#只有英文及符号
        etxt=etxt.lower()
        for c in etxt:
            if c in string.punctuation:
                etxt=etxt.replace(c,' ')
        # for c in string.punctuation:
        #     etxt=etxt.replace(c,' ')
        return etxt



def count_of_words(txt):
    """接收去除标点、符号的字符串,统计并返回其中单词数量和不重复的单词数量"""
    txtls=txt.split()
    n1=len(txtls)
    n2=len(set(txtls))
    return n1,n2



def word_frequency(txt):
    """接收去除标点、符号的字符串,统计并返回每个单词出现的次数
    返回值为字典类型,单词为键,对应出现的次数为值"""
    txtls=txt.split()
    fre=dict()
    for word in txtls:
        fre[word]=fre.get(word,0)+1
    return fre



def top_ten_words(frequency, cnt):
    """接收词频字典,输出出现次数最多的cnt个单词及其出现次数"""
    dc=frequency
    dc1=sorted(dc.items(),key=lambda x:x[1],reverse=True)
    for w,c in dc1[0:cnt]:
        print(w,c)



def top_ten_words_no_excludes(frequency, cnt):
    """接收词频字典,去除常见的冠词、代词、系动词和连接词后,输出出现次数最多的
    cnt个单词及其出现次数,需排除的单词如下:
    excludes_words = ['a', 'an', 'the', 'i', 'he', 'she', 'his', 'my', 'we',
    'or', 'is', 'was', 'do', 'and', 'at', 'to', 'of', 'it', 'on', 'that', 'her',
    'c','in', 'you', 'had','s', 'with', 'for', 't', 'but', 'as', 'not', 'they', 
    'be', 'were', 'so', 'our','all', 'would', 'if', 'him', 'from', 'no', 'me', 
    'could', 'when', 'there','them', 'about', 'this', 'their', 'up', 'been', 
    'by', 'out', 'did', 'have']
    """
    excludes_words = ['a', 'an', 'the', 'i', 'he', 'she', 'his', 'my', 'we',
    'or', 'is', 'was', 'do', 'and', 'at', 'to', 'of', 'it', 'on', 'that', 'her',
    'c','in', 'you', 'had','s', 'with', 'for', 't', 'but', 'as', 'not', 'they',
    'be', 'were', 'so', 'our','all', 'would', 'if', 'him', 'from', 'no', 'me',
    'could', 'when', 'there','them', 'about', 'this', 'their', 'up', 'been',
    'by', 'out', 'did', 'have']
    for word in excludes_words:
        frequency.pop(word)
    f = sorted(frequency.items(), key=lambda x: x[1], reverse=True)
    for word, counts in f[:cnt]:
        print(word, counts)



# 取消这段和代码最后二行注释可以绘制词云,仅供参考
# def draw_cloud(frequency):
#     """绘制词云,传入参数为词频,设定图片的宽度600,高度400,背景白色、字体最大值150、图片边缘为5。"""
#     wc = WordCloud(max_words=80,              # 设置显示高频单词数量
#                   width=600,                 # 设置图片的宽度
#                   height=400,                # 设置图片的高度
#                   background_color='White',  # 设置背景颜色
#                   max_font_size=150,         # 设置字体最大值
#                   margin=5,                  # 设置图片的边缘
#                   scale=1.5)                 # 按照比例进行放大画布,如设置为1.5,则长和宽都是原来画布的1.5倍。
#     wc.generate_from_frequencies(frequency)   # 根据文本内容直接生成词云
#     plt.imshow(wc)                            # 负责对图像进行处理,并显示其格式,但是不能显示。
#     plt.axis("off")                           # 不显示坐标轴
#     wc.to_file('My Cheese.png')               # 词云保存为图片
#     plt.show()                                # 显示图像



if __name__ == '__main__':
    filename = 'Who Moved My Cheese.txt'  # 文件名
    content = read_file(filename)  # 调用函数返回字典类型的数据
    frequency_result = word_frequency(content)  # 统计词频
    cmd = input()
    if cmd == '1':
        n = int(input())
        print(content[:n])
    elif cmd == '2':
        amount_results = count_of_words(content)
        print('文章共有单词{}个,其中不重复单词{}个'.format(*amount_results))
    elif cmd == '3':
        n = int(input())
        top_ten_words(frequency_result, n)
    elif cmd == '4':
        n = int(input())
        top_ten_words_no_excludes(frequency_result, n)
        # frequency_no_excludes = top_ten_words_no_excludes(frequency_result)
        # draw_cloud(frequency_no_excludes)
        
        

 

  • 10
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用Python实现中文文本的词频统计,可以按照以下步骤进行: 1. 导入必要的库,包括`jieba`用于中文分词。 2. 定义一个函数,命名为`word_frequency`,该函数接受一个中文文本字符串作为参数。 3. 在函数内部,使用`jieba`库对文本进行分词,得到词语列表。 4. 初始化一个空字典`freq_dict`用于存储词频统计结果。 5. 遍历词语列表,对每个词语进行统计: - 如果词语已经在`freq_dict`中,将其计数加1; - 如果词语不在`freq_dict`中,将其添加到`freq_dict`并设置计数为1。 6. 返回词频统计结果`freq_dict`。 以下是用Python实现中文文本词频统计的代码示例: ```python import jieba def word_frequency(text): word_list = jieba.lcut(text) # 中文分词 freq_dict = {} for word in word_list: if word in freq_dict: freq_dict[word] += 1 else: freq_dict[word] = 1 return freq_dict ``` 你可以调用`word_frequency`函数并传入一个中文文本字符串来进行测试,例如: ```python text = "今天天气真好,出去玩了一天。明天继续努力学习!" freq_result = word_frequency(text) print(freq_result) ``` 输出结果将为: ``` {'今天': 1, '天气': 1, '真好': 1, ',': 1, '出去': 1, '玩': 1, '了': 1, '一天': 1, '。': 1, '明天': 1, '继续': 1, '努力': 1, '学习': 1, '!': 1} ``` 这样就完成了使用Python实现中文文本词频统计的过程。请注意,在进行中文分词时,你可能需要提前安装`jieba`库,并且需要下载`jieba`的中文分词词库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值