python编程:提取word、pdf、excel信息的动词与名词,制作词云图

工具类参数:
text:数据
mode:word文档选 选word
excel或者pdf选 mode=‘pdf’

import os
import re
import jieba.posseg as jpg
from wordcloud import WordCloud
from matplotlib import pyplot as plt
import imageio
import pandas as pd
import xlrd
import xlwt
import shutil


class Util:

    def __init__(self, text, mode, file_name):
        # self.pdf_text = pdf_text
        self.mode = mode
        self.text = text
        self.file_name = file_name

    # 划分词性
    def cut(self, content):
        if not os.path.exists(self.file_name):  # 判断是否存在文件夹如果不存在则创建为文件夹
            a = os.makedirs(self.file_name)
            print(a)

        # os.mkdir(self.file_name)
        n = []
        v = []
        for x in content:
            if x.flag == 'v':
                p = re.split('/', str(x))
                v.append(p[0])
            if x.flag == 'n':
                p1 = re.split('/', str(x))
                n.append(p1[0])
        noun = ' '.join(n)
        verb = ' '.join(v)

        return noun, verb

    def tiqu_word(self):
        content = ""
        if self.text != '':
            for i in self.text:
                content += i
            # content = re.findall(r'四(.*?)五', content)
            content = jpg.cut(str(content))
            noun, verb = self.cut(content)
            print(noun, '\n', verb)
            return noun, verb
        else:
            print("传入数据==null")
            return 0

    def tiqu_pdf(self):

        if self.text != '':
            # content = re.findall(r'四(.*?)五', pdf_text)
            content = jpg.cut(str(text))
            noun, verb = self.cut(content)
            print(noun, '\n', verb)
            return noun, verb
        else:
            print("传入数据==null")
            return 0

    # 词云图
    def draw(self):
        image = imageio.imread('demo1.jpeg')

        def generate_wordcloud(text, sign):

            wordcloud1 = WordCloud(background_color='white', font_path='ArialUnicode.ttf', mask=image,
                                   width=800, height=600).generate(text)

            mage_1 = wordcloud1.to_image()
            wordcloud1.to_file(self.file_name + sign + '.png')
            shutil.move(self.file_name + sign + '.png', self.file_name + '/' + self.file_name + sign + '.png')
            # image1.save('/Users/hanze/Desktop/未命名文件夹/1a.png')
            # plt.axis('off')
            # plt.imshow(wordcloud)
            # plt.show()

        if self.mode == 'word':
            noun, verb = self.tiqu_word()
            generate_wordcloud(noun, 'n')
            generate_wordcloud(verb, 'v')

        if self.mode == 'pdf':
            noun, verb = self.tiqu_pdf()
            generate_wordcloud(noun, 'n')
            generate_wordcloud(verb, 'v')

    # vocabulary txt
    def write_vocabtxt(self):
        # path = '/Users/hanze/cloud'
        if self.mode == 'word':
            noun, verb = self.tiqu_word()
            # new_name = os.path.join(self.file_name)
            f = open(self.file_name + '_n.txt', 'w')
            f.write(noun)
            f.close()
            f1 = open(self.file_name + '_v.txt', 'w')
            f1.write(verb)
            f1.close()
            shutil.move(self.file_name + '_n.txt', self.file_name + '/' + self.file_name + '_n.txt')
            shutil.move(self.file_name + '_v.txt', self.file_name + '/' + self.file_name + '_v.txt')

        if self.mode == 'pdf':
            noun, verb = self.tiqu_pdf()
            # new_name = os.path.join(path, self.file_name)
            f = open(self.file_name + '_n.txt', 'w')
            f.write(noun)
            f.close()
            f1 = open(self.file_name + '_v.txt', 'w')
            f1.write(verb)
            f1.close()
            shutil.move(self.file_name + '_n.txt', self.file_name + '/' + self.file_name + '_n.txt')
            shutil.move(self.file_name + '_v.txt', self.file_name + '/' + self.file_name + '_v.txt')


if __name__ == '__main__':

    text = pd.read_excel('firstData_T.xls')
    content = []
    grade = ['0', '1']
    temp_content = ['', '']
    total_content = []
    for i in text.values:

        temp_list = [i[0], i[1]]
        content.append(temp_list)
		提取前两位数字
        temp_grade = re.findall('([0-9])', i[0])
		汇总每个学院数据
        if grade[0] == temp_grade[0] and temp_grade[1] == grade[1]:
            # print(temp_grade[0:2],grade[0:2])
            temp_content[0] = i[0][0:3] + 'total'
            # print(temp_content[0])
            temp_content[1] += i[1]
            # print(temp_content[0], len(temp_content[1]))

            # print(total_content[0], len(total_content[1]))
        if grade[0] != temp_grade[0] or temp_grade[1] != grade[1]:
            total_content.append([temp_content[0], temp_content[1]])
            # print(temp_content[0])

            # print('----下一个学院-----')
            grade = temp_grade
            temp_content[0] = ''
            temp_content[1] = ''
    total_content.append([temp_content[0], temp_content[1]])
    # print(temp_content)
    #
    for i in total_content:
        print(i[0], len(i[1]))
    # print(len(total_content))

    total_content += content
    for i in total_content:
        print(i[0], len(i[1]))
        print(len(total_content))
        print(len(content))

    for i in total_content:
        Util(i[1], 'pdf', i[0]).draw()
        Util(i[1], 'pdf', i[0]).write_vocabtxt()

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HanZee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值