|
@@ -91,6 +91,11 @@ class Mvp:
|
|
|
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()
|
|
|
+ # self.scores_tag = ExcelUtil(file_name='行为与模块分值汇总.xlsx', sheet_name='行为').init_scores()
|
|
|
+ # self.score_module = ExcelUtil(file_name='行为与模块分值汇总.xlsx', sheet_name='模块').init_scores()
|
|
|
+ self.scores_tag = None
|
|
|
+ self.score_module = None
|
|
|
+
|
|
|
|
|
|
def init_city(self):
|
|
|
"""
|
|
@@ -122,7 +127,7 @@ class Mvp:
|
|
|
# self.shangju_db.add_some(self.sql_3, datas)
|
|
|
scores_behavioral = self.city_age_crowd(city, age, crowd)
|
|
|
# scores_module = self.module_score(crowd, city, age, scores_behavioral['score'])
|
|
|
- #result = {'行为兴趣分值': scores_behavioral['score'], '模块分值': scores_module}
|
|
|
+ # result = {'行为兴趣分值': scores_behavioral['score'], '模块分值': scores_module}
|
|
|
print('update finished!!!')
|
|
|
return scores_behavioral
|
|
|
|
|
@@ -147,25 +152,80 @@ class Mvp:
|
|
|
result.append([city, age, crowd, module_name, score])
|
|
|
return result
|
|
|
|
|
|
- def insert_data(self, scores_behavioral, scores_module):
|
|
|
+ # def insert_data(self, scores_behavioral, scores_module):
|
|
|
+ def insert(self):
|
|
|
"""
|
|
|
计算数据写入数据库中,供接口查看
|
|
|
- :param scores_module:
|
|
|
- :param scores_behavioral:
|
|
|
:return:
|
|
|
"""
|
|
|
- # todo
|
|
|
infos = []
|
|
|
- for city in self.citys:
|
|
|
- for age in self.age:
|
|
|
- for c_type in self.crowd:
|
|
|
+ for city in ['上海市', '宁波市', '苏州市', '杭州市', ' 无锡市']:
|
|
|
+ for age in ['50-59年生', '60-69年生', '70-74年生', '75-79年生', '80-84年生', '85-89年生', '90-94年生', '95-99年生', '00'
|
|
|
+ '-04年生', '05-09年生']:
|
|
|
+ for c_type in ['A', 'B', 'C', 'D', 'E', 'F']:
|
|
|
age_area = self.age_dict.get(age)
|
|
|
if age_area:
|
|
|
infos.append([age_area, city, c_type])
|
|
|
self.shangju_db.add_some(self.sql_11, infos)
|
|
|
|
|
|
+ def query_data(self):
|
|
|
ids = self.shangju_db.select(self.sql_14)
|
|
|
- pass
|
|
|
+ return ids
|
|
|
+
|
|
|
+ def shanghai_85_module_score_insert(self):
|
|
|
+ """
|
|
|
+ 上海市,85后模块分数计算
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ result = []
|
|
|
+ for crowd in self.crowd:
|
|
|
+ modules = self.module_scores[crowd]
|
|
|
+ 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]
|
|
|
+ standard_score = float(value[1])
|
|
|
+ if standard_score is not None:
|
|
|
+ score += standard_score * weight
|
|
|
+ result.append(['上海市', '85后', crowd, module_name, score])
|
|
|
+ return {'score': result, 'data': self.module_scores}
|
|
|
+
|
|
|
+ def tag_module_score_insert(self):
|
|
|
+ """
|
|
|
+ 标签模块分数写入数据库
|
|
|
+ :return:
|
|
|
+ """
|
|
|
+ ids = self.query_data()
|
|
|
+ insert_data = []
|
|
|
+ insert_data_1 = []
|
|
|
+ for tag, module in zip(self.scores_tag, self.score_module):
|
|
|
+ city = tag[0]
|
|
|
+ age = tag[1]
|
|
|
+ crowd = tag[2]
|
|
|
+ tag_name = tag[3]
|
|
|
+ tag_score = tag[4]
|
|
|
+
|
|
|
+ city_2 = module[0]
|
|
|
+ age_2 = module[1]
|
|
|
+ crowd_2 = module[2]
|
|
|
+ module_name_2 = module[3]
|
|
|
+ module_score_2 = module[4]
|
|
|
+
|
|
|
+ for id in ids:
|
|
|
+ city_1 = id[2]
|
|
|
+ age_1 = id[1]
|
|
|
+ crowd_1 = id[3]
|
|
|
+ id_1 = id[0]
|
|
|
+ if city == city_1 and self.age_dict[age] == age_1 and crowd == crowd_1:
|
|
|
+ insert_data.append([id_1, tag_name, tag_score])
|
|
|
+ if city_2 == city_1 and self.age_dict[age_2] == age_1 and crowd_2 == crowd_1:
|
|
|
+ insert_data_1.append([id_1, module_name_2, module_score_2])
|
|
|
+ self.shangju_db.add_some(self.sql_12, insert_data)
|
|
|
+ self.shangju_db.add_some(self.sql_13, insert_data_1)
|
|
|
|
|
|
def init_age(self):
|
|
|
"""
|
|
@@ -195,14 +255,18 @@ class Mvp:
|
|
|
# for city in [city]:
|
|
|
for age in self.age:
|
|
|
for crowd_type in self.crowd:
|
|
|
- # print(' {}{}'.format(city, age))
|
|
|
- people_uuids = self.get_people_uuid_by_type(crowd_type)
|
|
|
- if len(people_uuids) > 0:
|
|
|
- print('{}-{}-{}'.format(city, age, crowd_type))
|
|
|
- datas = self.behavior_tag_init(city, age, people_uuids)
|
|
|
- data_start.append(datas)
|
|
|
- result.extend(self.calculation_standard_score(datas, city, age, crowd_type))
|
|
|
- module_scores.extend(self.module_score(crowd_type, city, age, result))
|
|
|
+ if age == '85-89年生' and city == '上海市':
|
|
|
+ print('上海市85后数据导入人工值,无需计算...')
|
|
|
+ pass
|
|
|
+ else:
|
|
|
+ # print(' {}{}'.format(city, age))
|
|
|
+ people_uuids = self.get_people_uuid_by_type(crowd_type)
|
|
|
+ if len(people_uuids) > 0:
|
|
|
+ print('{}-{}-{}'.format(city, age, crowd_type))
|
|
|
+ datas = self.behavior_tag_init(city, age, people_uuids)
|
|
|
+ data_start.append(datas)
|
|
|
+ result.extend(self.calculation_standard_score(datas, city, age, crowd_type))
|
|
|
+ module_scores.extend(self.module_score(crowd_type, city, age, result))
|
|
|
# return result
|
|
|
# data_list = []
|
|
|
# for e in data_start:
|
|
@@ -434,6 +498,15 @@ class ExcelUtil:
|
|
|
result[name] = orders
|
|
|
return result
|
|
|
|
|
|
+ def init_scores(self):
|
|
|
+ work_sheet = self.read_excel_by_ox()
|
|
|
+ rows = [row for row in work_sheet.rows]
|
|
|
+ datas = []
|
|
|
+ for row in rows[1:]:
|
|
|
+ if row[0].value is not None:
|
|
|
+ datas.append([row[0].value, row[1].value, row[2].value, row[3].value, row[4].value])
|
|
|
+ return datas
|
|
|
+
|
|
|
def init_module_info(self):
|
|
|
work_sheet = self.read_excel_by_ox()
|
|
|
max_column = work_sheet.max_column
|
|
@@ -452,11 +525,17 @@ class ExcelUtil:
|
|
|
weight = row[index + 1].value
|
|
|
datas.append([crowd_name, behavior, score, module_name, weight])
|
|
|
results = {}
|
|
|
+ datas.sort(key=lambda obj: obj[0])
|
|
|
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_list = []
|
|
|
+ for it in items:
|
|
|
+ sub_list.append([x for x in it])
|
|
|
+ sub_list.sort(key=lambda obj: obj[3])
|
|
|
+ for name_1, itmes_1 in groupby(sub_list, key=lambda obj: obj[3]):
|
|
|
sub_data = []
|
|
|
for n in itmes_1:
|
|
|
+ # print(' {}'.format(n[1]))
|
|
|
sub_data.append([n[1], n[2], n[4]])
|
|
|
sub_results[name_1] = sub_data
|
|
|
results[name] = sub_results
|
|
@@ -568,6 +647,28 @@ def set_behavior_tag():
|
|
|
return json.dumps(mvp.module_scores, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
+@app.route('/insert_into', methods=['GET', 'POST'])
|
|
|
+def insert_info():
|
|
|
+ mvp = Mvp()
|
|
|
+ mvp.insert()
|
|
|
+ query_data = mvp.query_data()
|
|
|
+ return json.dumps(query_data, ensure_ascii=False)
|
|
|
+
|
|
|
+
|
|
|
+@app.route('/insert_score', methods=['GET', 'POST'])
|
|
|
+def insert_score():
|
|
|
+ mvp = Mvp()
|
|
|
+ mvp.tag_module_score_insert()
|
|
|
+ return '!!!分数添加成功!!!'
|
|
|
+
|
|
|
+
|
|
|
+@app.route('/shanghai_85', methods=['GET', 'POST'])
|
|
|
+def shanghai_85():
|
|
|
+ mvp = Mvp()
|
|
|
+ data = mvp.shanghai_85_module_score_insert()
|
|
|
+ return json.dumps(data, ensure_ascii=False)
|
|
|
+
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
app.run(
|
|
|
host='0.0.0.0',
|