You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.1 KiB
77 lines
2.1 KiB
import random
|
|
import jieba
|
|
import sqlite3
|
|
import numpy as np
|
|
from PIL import Image
|
|
from wordcloud import WordCloud
|
|
from matplotlib import pyplot as plt
|
|
|
|
def random_color_func(word=None, font_size=None, position=None, orientation=None, font_path=None, random_state=None):
|
|
h = random.randint(150, 250)
|
|
s = int(100.0 * 255.0 / 255.0)
|
|
l = int(100.0 * float(random.randint(60, 120)) / 255.0)
|
|
return "hsl({}, {}%, {}%)".format(h, s, l)
|
|
# 起始年份
|
|
year = 2010
|
|
# 存放字符的字符串
|
|
con = sqlite3.connect('D:\\Desktop\\nba1\\NBA球员数据库.db')
|
|
c = con.cursor()
|
|
text = ''
|
|
for i in range(year, 2020):
|
|
sql = '''
|
|
select name from 球员数据''' + str(i) + '''年
|
|
'''
|
|
data = c.execute(sql)
|
|
for item in data:
|
|
text += item[0]
|
|
cut = jieba.cut(text)
|
|
string = ",".join(cut)
|
|
print(string)
|
|
img = Image.open(r'D:\Desktop\nba1\static\assets\img\NBA.png') # 打开图片
|
|
img_array = np.array(img) # 将图片转化为数组
|
|
wc = WordCloud(
|
|
background_color='blue',
|
|
mask=img_array,
|
|
font_path="/font/msyh.ttc",
|
|
color_func=random_color_func
|
|
)
|
|
wc.generate_from_text(string)
|
|
# 绘制图片
|
|
fig = plt.figure(1)
|
|
plt.imshow(wc)
|
|
plt.axis('off')
|
|
plt.savefig(r'D:\Desktop\nba1\static\assets\img\namecloud.png', dpi=1000)
|
|
|
|
# 起始年份
|
|
year = 2010
|
|
con = sqlite3.connect("D:\\Desktop\\nba1\\NBA球员数据库.db")
|
|
c = con.cursor()
|
|
# 存放字符的字符串
|
|
text = ''
|
|
for i in range(year, 2020):
|
|
sql = '''
|
|
select teamname from 球员数据''' + str(i) + '''年
|
|
'''
|
|
data = c.execute(sql)
|
|
for item in data:
|
|
text += item[0]
|
|
cut = jieba.cut(text)
|
|
string = ",".join(cut)
|
|
print(string)
|
|
c.close()
|
|
con.close()
|
|
img = Image.open(r'D:\Desktop\nba1\static\assets\img\乔1.jpg') # 打开图片
|
|
img_array = np.array(img) # 将图片转化为数组
|
|
wc = WordCloud(
|
|
background_color='white',
|
|
mask=img_array,
|
|
font_path="/font/msyh.ttc",
|
|
color_func=random_color_func
|
|
)
|
|
wc.generate_from_text(string)
|
|
# 绘制图片
|
|
fig = plt.figure(2)
|
|
plt.imshow(wc)
|
|
plt.axis('off')
|
|
plt.savefig(r'D:\Desktop\nba1\static\assets\img\teamcloud.png', dpi=900)
|
|
#plt.show() |