user_portrait_analysis.py 2.51 KB
Newer Older
李明杰's avatar
李明杰 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
# -*- coding: utf-8 -*-
# author:Li Mingjie time:2019/2/28
# Brief:User Portrait Analysis

from wordcloud import WordCloud
import cv2
import matplotlib.pyplot as plt
import bottom_function.data_read as dr
from bottom_function import normalization as norm
import json
from flask import Flask
from flask import request
from flask_cors import CORS


def portrait_plot(datatype, starttime, endtime):
    n_data = dr.read_data(datatype=datatype, starttime=starttime, endtime=endtime)
    if n_data.empty:
        return 0
    query_data = ''
    for q in n_data['query']:
        str_q = str(q)
        query_data = query_data + "," + str_q

    cut_text = norm.remove_special_characters(query_data)

    color_mask = cv2.imread('./bottom_function/data/gree_logo.jpg')

    cloud = WordCloud(
        # 设置字体,不指定就会出现乱码
        font_path=" C:\\Windows\\Fonts\\STXINGKA.TTF",
        # font_path=path.join(d,'simsun.ttc'),
        font_step=1,
        width=720,
        height=720,
        # 设置背景色
        background_color='white',
        # 词云形状
        mask=color_mask,
        # 允许最大词汇
        max_words=10000,
        # 最大号字体
        max_font_size=50,
        min_font_size=5
    )

    wCloud = cloud.generate(cut_text)
    # wCloud.to_file('../data/word_cloud/cloud.jpg')

    plt.imshow(wCloud, interpolation='bilinear')
    plt.axis('off')
    til = datatype + ' ' + starttime + endtime + 'user portrait of word cloud'
    plt.title(til)
    path = '/roobo/soft/phpmyadmin/plot_user.jpg'
    plt.savefig(path)
    return path


app = Flask(__name__)
CORS(app, supports_credentials=True)


@app.route('/SPDAS/user_portrait_analysis1', methods=['POST'])
def domain():
    param = ({"data_type": [{"value": "control"}, {"value": "application"}, {"value": "all"}],
              "time": "2019.01.01 00:00:00/2019.01.02 00:00:00"})
    return json.JSONEncoder().encode(param)


@app.route('/SPDAS/user_portrait_analysis2', methods=['POST'])
def domain_form():
    # 需要从request对象读取表单内容:
    data = request.get_data()
    json_re = json.loads(data)

    datatype = json_re['data_type']
    m_time = json_re['time']
    str_time = str(m_time)
    m_time = str_time.split('/')
    starttime = m_time[0]
    endtime = m_time[1]
    image_path = portrait_plot(datatype=datatype, starttime=starttime, endtime=endtime)
    path = {"user_image": image_path}
    return json.JSONEncoder().encode(path)


if __name__ == '__main__':
    app.run(debug=True, host='10.7.19.129', port=5000)