Преглед на файлове

flask_app: 增加模块分数计算

Signed-off-by: binren <zhangbr@elab-plus.com>
binren преди 5 години
родител
ревизия
b2d655e2c2
променени са 3 файла, в които са добавени 80 реда и са изтрити 6 реда
  1. 80 6
      flask_app.py
  2. BIN
      resources/mvp.xlsx
  3. BIN
      resources/set-behavior-tag.xlsx

+ 80 - 6
flask_app.py

@@ -76,12 +76,13 @@ class Mvp:
         self.shangju_db = MysqlDB('shangju')
         self.marketing_db = MysqlDB('bi_report')
         # self.shangju_db.truncate('mvp_standard_score')
-        self.tag_data = ExcelUtil(path=path).init_mvp_data()
-        self.crowd_info = ExcelUtil(path=path, sheet_name='选项-人群分类对应表').init_crowd_info()
+        self.tag_data = ExcelUtil(file_name=path).init_mvp_data()
+        self.crowd_info = ExcelUtil(file_name=path, sheet_name='选项-人群分类对应表').init_crowd_info()
         self.citys = self.init_city()
         self.age = self.init_age()
         self.people_sub_option_ids = self.marketing_db.select(self.sql_10)
         self.crowd_contain_sub_option_ids = self.get_crowd_contain_sub_option_ids()
+        self.module_scores = ExcelUtil(file_name='set-behavior-tag.xlsx', sheet_name='算法关系表').init_module_info()
 
     def init_city(self):
         """
@@ -111,7 +112,9 @@ class Mvp:
         #             datas.append([question, option, data[0][3], data[0][1], key, corr])
         # self.shangju_db.truncate('mvp_question_classification')
         # self.shangju_db.add_some(self.sql_3, datas)
-        result = self.city_age_crowd(city, age, crowd)
+        scores_behavioral = self.city_age_crowd(city, age, crowd)
+        scores_module = self.module_score(crowd, city, age, scores_behavioral)
+        result = {'行为兴趣分值': scores_behavioral, '模型分值': scores_module}
         print('update finished!!!')
         return result
 
@@ -133,6 +136,27 @@ class Mvp:
                             scores = self.calculation_standard_score(datas, city, age, crowd_type)
                             scores_all.extend(scores)
 
+    def module_score(self, crowd, city, age, scores):
+        """
+            模块分数计算
+            城市 年龄 人群分类 模块名称 分数
+        :return:
+        """
+        modules = self.module_scores[crowd]
+        result = []
+        for key in modules.keys():
+            values = modules[key]
+            module_name = key
+            score = 0
+            for value in values:
+                behavioral_name = value[0]
+                weight = float(value[2])
+                standard_score = [x[4] for x in scores if x[2] == behavioral_name]
+                if len(standard_score) > 0:
+                    score += standard_score[0] * weight
+            result.append([city, age, crowd, module_name, score])
+        return result
+
     def insert(self, scores):
         """
             计算数据写入数据库中,供接口查看
@@ -306,9 +330,9 @@ class ExcelUtil:
         解析excel文件
     """
 
-    def __init__(self, sheet_name=None, path=None):
-        if path:
-            self.path = path
+    def __init__(self, sheet_name=None, file_name=None):
+        if file_name:
+            self.path = os.path.join(self.dir_path, file_name)
         else:
             self.path = os.path.join(self.dir_path, 'mvp.xlsx')
         if sheet_name:
@@ -386,6 +410,34 @@ class ExcelUtil:
             result[name] = orders
         return result
 
+    def init_module_info(self):
+        work_sheet = self.read_excel_by_ox()
+        max_column = work_sheet.max_column
+        rows = [row for row in work_sheet.rows][3:]
+        crowd_name = None
+        datas = []
+        for row in rows:
+            crowd = row[1].value
+            if crowd is not None:
+                crowd_name = crowd
+            behavior = row[2].value
+            score = row[4].value
+            for index in range(6, max_column - 1, 2):
+                module_name = row[index].value
+                if module_name is not None:
+                    weight = row[index + 1].value
+                    datas.append([crowd_name, behavior, score, module_name, weight])
+        results = {}
+        for name, items in groupby(datas, key=lambda obj: obj[0]):
+            sub_results = {}
+            for name_1, itmes_1 in groupby(items, key=lambda obj: obj[3]):
+                sub_data = []
+                for n in itmes_1:
+                    sub_data.append([n[1], n[2], n[4]])
+                sub_results[name_1] = sub_data
+            results[name] = sub_results
+        return results
+
 
 class MysqlDB:
     """
@@ -441,6 +493,10 @@ class MysqlDB:
 
 @app.route('/behavioral_statistics', methods=['GET', 'POST'])
 def behavioral_statistics():
+    """
+        父选项对应的标准化值
+    :return:
+    """
     city = request.args.get('city', default=None, type=str)
     age = request.args.get('age', default=None, type=str)
     crowd = request.args.get('crowd', default=None, type=str)
@@ -454,6 +510,10 @@ def behavioral_statistics():
 
 @app.route('/infos', methods=["GET", 'POST'])
 def get_city_age_crowd():
+    """
+        测试数据中城市 年龄 人群分类信息
+    :return:
+    """
     mvp = Mvp()
     infos = {'城市': mvp.citys, '年龄段': mvp.age}
     mvp.shangju_db.close()
@@ -463,6 +523,10 @@ def get_city_age_crowd():
 
 @app.route('/crowd_people', methods=['GET', 'POST'])
 def crowd_people():
+    """
+        人群分类人数统计
+    :return:
+    """
     mvp = Mvp()
     people_count = mvp.get_crowd_people()
     mvp.shangju_db.close()
@@ -470,6 +534,16 @@ def crowd_people():
     return json.dumps(people_count, ensure_ascii=False)
 
 
+@app.route('/set_behavior_tag', methods=['GET', 'POST'])
+def set_behavior_tag():
+    """
+        模块标准化值
+    :return:
+    """
+    mvp = Mvp()
+    return json.dumps(mvp.module_scores, ensure_ascii=False)
+
+
 if __name__ == '__main__':
     app.run(
         host='0.0.0.0',

BIN
resources/mvp.xlsx


BIN
resources/set-behavior-tag.xlsx