• Python中文分词、词频统计并制作词云图


    中文分词、词频统计并制作词云图是统计数据常用的功能,这里用到了三个模块快速实现这个功能。

    中文分词、词频统计

    import jieba
    from collections import Counter
    
    # 1. 读取文本内容并进行分词
    with open('demo.txt', mode='r', encoding='gbk') as f:
        report = f.read()
    words = jieba.cut(report)
    
    # 2. 按指定长度提取词
    report_words = []
    for word in words:
        if len(word) >= 4:
            report_words.append(word)
    print(report_words)
    
    # 3. 统计高频词汇
    result = Counter(report_words).most_common(50)
    print(result)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    上面代码用jieba模块进行分词,用collections进行词频统计。
    jieba是一个优秀的第三方中文词库,用于中文分词。中文分词指的是将一个汉字序列切分成一个一个单独的词。jieba可以帮助你快速高效地完成中文分词,支持三种分词模式:精确模式、全模式和搜索引擎模式。

    collections是Python标准库中的一个模块,提供了一些额外的容器类型,以提供Python标准内建容器dictlistsettuple的替代选择。这些容器类型包括namedtupledequeCounter等。

    简单词云图

    import jieba.posseg as pseg
    from collections import Counter
    from wordcloud import WordCloud
    
    # 1. 读取文本内容并进行分词
    with open('demo.txt', mode='r', encoding='gbk') as f:
        report = f.read()
    words = pseg.cut(report)
    
    # 2. 按指定长度和词性提取词
    report_words = []
    for word, flag in words:
        if (len(word) >= 4) and ('n' in flag):
            report_words.append(word)
    # print(report_words)
    
    # 3. 统计高频词汇
    result = Counter(report_words).most_common(50)
    # print(result)
    
    # 4. 绘制词云图
    content = dict(result)
    # print(content)
    wc = WordCloud(font_path='PINGFANG MEDIUM.TTF', background_color='white', width=1000, height=600)
    wc.generate_from_frequencies(content)
    wc.to_file('词云图1.png')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    这里用到了wordcloud模块来生成词云图。

    按照图片绘制词云图

    import jieba.posseg as pseg
    from collections import Counter
    from PIL import Image
    import numpy as np
    from wordcloud import WordCloud
    
    # 1. 读取文本内容并进行分词
    with open('demo.txt', mode='r', encoding='gbk') as f:
        report = f.read()
    words = pseg.cut(report)
    
    # 2. 按指定长度和词性提取词
    report_words = []
    for word, flag in words:
        if (len(word) >= 4) and ('n' in flag):
            report_words.append(word)
    # print(report_words)
    
    # 3. 统计高频词汇
    result = Counter(report_words).most_common(300)
    # print(result)
    
    # 4. 绘制词云图
    mask_pic = Image.open('map.png')
    mask_data = np.array(mask_pic)
    print(mask_data)
    content = dict(result)
    wc = WordCloud(font_path='PINGFANG MEDIUM.TTF', background_color='white', mask=mask_data)
    wc.generate_from_frequencies(content)
    wc.to_file('词云图2.png')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    这里给WordCloud加了mask遮罩参数。

    按照图片绘制渐变词云图

    import jieba.posseg as pseg
    from collections import Counter
    from PIL import Image
    import numpy as np
    from wordcloud import WordCloud, ImageColorGenerator
    
    # 1. 读取文本内容并进行分词
    with open('demo.txt', mode='r', encoding='gbk') as f:
        report = f.read()
    words = pseg.cut(report)
    
    # 2. 按指定长度和词性提取词
    report_words = []
    for word, flag in words:
        if (len(word) >= 4) and ('n' in flag):
            report_words.append(word)
    # print(report_words)
    
    # 3. 统计高频词汇
    result = Counter(report_words).most_common(300)
    # print(result)
    
    # 4. 绘制词云图
    mask_pic = Image.open('map.png')
    mask_data = np.array(mask_pic)
    content = dict(result)
    wc = WordCloud(font_path='PINGFANG MEDIUM.TTF', background_color='white', mask=mask_data)
    wc.generate_from_frequencies(content)
    mask_colors = ImageColorGenerator(mask_data)
    wc.recolor(color_func=mask_colors)
    wc.to_file('词云图3.png')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    这里用recolor重绘了颜色。

  • 相关阅读:
    2024 年的 13 个 AI 趋势
    【无标题】
    关于架构的认知
    【Redis】事务和锁机制
    西门子PLC编程之模拟量输入具体实现方法
    OpUtils局域网唤醒:远程引导计算机
    Doris workload group实战
    2023年天津财经大学珠江学院专升本报名缴费准考证下载考试须知
    python--数据容器--列表
    暑期结束为你的新学期立下Flag吧
  • 原文地址:https://blog.csdn.net/lilongsy/article/details/134011500