zoukankan      html  css  js  c++  java
  • Python 读取docx/txt根据词频生成云图

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from wordcloud import WordCloud, STOPWORDS
    from imageio import imread
    from sklearn.feature_extraction.text import CountVectorizer
    import jieba
    import csv
    import docx2txt
    
    # 读docx文档
    # text = docx2txt.process("file.docx")
    # contents = text
    # outFile = open("file.txt", "w", encoding='utf-8')
    # outFile.write(text)

    # 获取文章内容 with open("djstl.txt",encoding='utf-8') as f: contents = f.read() print("contents变量的类型:", type(contents)) # 使用jieba分词,获取词的列表 contents_cut = jieba.cut(contents) print("contents_cut变量的类型:", type(contents_cut)) contents_list = " ".join(contents_cut) print("contents_list变量的类型:", type(contents_list)) # 制作词云图,collocations避免词云图中词的重复,mask定义词云图的形状,图片要有背景色 wc = WordCloud(stopwords=STOPWORDS.add("一个"), collocations=False, background_color="white", font_path=r"C:WindowsFontssimhei.ttf", width=400, height=300, random_state=42, mask=imread('timg.jpg',pilmode="RGB")) wc.generate(contents_list) wc.to_file("ciyun.png") # 使用CountVectorizer统计词频 cv = CountVectorizer() contents_count = cv.fit_transform([contents_list]) # 词有哪些 list1 = cv.get_feature_names() # 词的频率 list2 = contents_count.toarray().tolist()[0] # 将词与频率一一对应 contents_dict = dict(zip(list1, list2)) # 输出csv文件,newline="",解决输出的csv隔行问题 with open("caifu_output.csv", 'w', newline="") as f: writer = csv.writer(f) for key, value in contents_dict.items(): writer.writerow([key, value])
  • 相关阅读:
    php_sphinx安装使用
    获取数据库中所有表名
    总结thinkphp快捷查询getBy、getField、getFieldBy用法及场景
    打印机复印身份证方法
    svn 删除、移动和改名
    MySQL中REGEXP正则表达式使用大全
    高铁在高速运行时的电力是如何提供的?
    2016亚洲大学排名
    Mac下安装HBase及详解
    HBase Mac OSX 安装笔记
  • 原文地址:https://www.cnblogs.com/ouzai/p/13784407.html
Copyright © 2011-2022 走看看