python 用字符串生成词云图
1.大致步骤
1. 从mysql读取数据
2. 分词分
3. 生成词云图
2.用到的库
- jieba 分词
- wordcloud 词云生成和保存
3.代码如下:
#连接数据库
def connect_mysql():
conn = pymysql.connect(
host='localhost',
db='TESTDB',
user='pymysql',
passwd='123123',
charset='utf8',
)
cursor = conn.cursor()
return cursor
#拿到标题和新闻内容
def process(cursor):
sql="SELECT news,title FROM southcity where title<>'广告'"
length=cursor.execute(sql) #返回一个int数据为记录数目
result=cursor.fetchall() #返回一个tuple
news=result
return news,length
#分词并返回以空格分隔的分词字符串
def cut_word(news,length):
blacklist=[' ',' ','(',')','(',')','']
contents=[]
for i in range(length):
seg_list = jieba.cut(str(news[i][0]),cut_all=False)
seg_list=list(seg_list)
# for j in range(len(seg_list)):
# word=str(seg_list[j]
# # if len(word)<=1 or word in blacklist:
# # del seg_list[j]
content= " ".join(seg_list) #精确模式
contents.append(content)
return contents
#生成词云图
def wordcloud(contents,news):
print(len(contents))
for i in range(len(contents)):
wc = WordCloud(font_path=u"static/fonts/simhei.ttf",
max_words=2000,
width=1920,
height=1080,
background_color="black",
margin=5)
try:
wc.generate(contents[i])
wc.to_file('result2/%s.png'%news[i][1])
except:
print("四BAI")
3.可参考内容: