工具类参数:
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()