mvp.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. from mysql_db import MysqlDB
  2. from excel_util import ExcelUtil
  3. import time
  4. from entity import PeopleInfo
  5. class Mvp:
  6. """
  7. ce mvp 答题数据统计
  8. 城市特例 北京市,上海市, 重庆市,天津市
  9. """
  10. age_dict = {
  11. '00-04年生': '00后',
  12. '05-09年生': '05后',
  13. '50-59年生': '50后',
  14. '60-69年生': '60后',
  15. '70-74年生': '70后',
  16. '75-79年生': '75后',
  17. '80-84年生': '80后',
  18. '85-89年生': '85后',
  19. '90-94年生': '90后',
  20. '95-99年生': '95后'
  21. }
  22. age_list = ['85后', '95后']
  23. city_list = ['上海市', '上海周边']
  24. tag_table = {
  25. '用户画像-审美偏好': ['mvp_crowd_info_aesthetic_preference', 'aesthetic_preference'],
  26. '用户画像-行为兴趣': ['mvp_crowd_info_behavior', 'behavioral_interest'],
  27. '用户画像-观念': ['mvp_crowd_info_consumer_concept', ''],
  28. '用户画像-消费特征': ['mvp_crowd_info_consumer_structure', ''],
  29. '空间需求图谱-功能关联': ['mvp_crowd_info_functional_module', ''],
  30. '性别比例': ['mvp_crowd_info_gender_rate', ''],
  31. '用户画像-生活方式': ['mvp_crowd_info_life_style', ''],
  32. '人群占比': ['mvp_crowd_info_rate', ''],
  33. '用户画像-社交模式': ['mvp_crowd_info_social_mode', ''],
  34. '用户画像-行业': ['mvp_crowd_info_trade', ''],
  35. '用户画像-出行方式': ['mvp_crowd_info_trip_mode', ''],
  36. '空间需求图谱-基础模块分值': ['mvp_innovate_space_base_module', ''],
  37. '空间需求图谱-色相': ['mvp_innovate_space_color_prefer', 'color'],
  38. '空间需求图谱-精装关注点': ['mvp_innovate_space_hardcover_focus', 'hardcover_focus'],
  39. '空间需求图谱-色调': ['mvp_innovate_space_hue_prefer', 'hue'],
  40. '空间需求图谱-单品偏好': ['mvp_innovate_space_item_preference', 'item_preference'],
  41. '空间需求图谱-材质': ['mvp_innovate_space_material_prefer', 'material'],
  42. '空间需求图谱-空间特性偏好': ['mvp_innovate_space_space_prefer', 'space_preference'],
  43. '空间需求图谱-空间拓普图': ['mvp_innovate_space_space_top', ''],
  44. '模块分数': ['mvp_crowd_info_module', 'module_name']
  45. }
  46. crowd_info_1 = {
  47. '1973': 'A',
  48. '1974': 'B',
  49. '1975': 'C',
  50. '1976': 'D',
  51. '1977': 'E',
  52. '1978': 'F',
  53. '1979': 'G',
  54. '1813': 'A',
  55. '1814': 'B',
  56. '1815': 'C',
  57. '1816': 'D',
  58. '1817': 'E',
  59. '1818': 'F',
  60. '1819': 'G'
  61. }
  62. base_insert_sql = 'insert into {}(crowd_info_id, {}, standard_value, status) values(%s, %s, %s, '\
  63. '1) '
  64. def get_table_name(self, name):
  65. """
  66. 获取表名
  67. :param name:
  68. :return:
  69. """
  70. params = self.tag_table.get(name)
  71. if params:
  72. return self.tag_table.get(name)[0]
  73. def get_insert_sql(self, tag_type_name):
  74. """
  75. 根据标签分类名称获取相应表的插入sql
  76. :param tag_type_name:
  77. :return:
  78. """
  79. params = self.tag_table.get(tag_type_name)
  80. if params:
  81. return self.base_insert_sql.format(params[0], [1])
  82. crowd = ['A', 'B', 'C', 'D', 'E', 'F']
  83. # 获取答题记录中城市列表
  84. sql_1 = 'select city from f_t_daren_score_2 group by city'
  85. # 获取父选项和父题id
  86. sql_2 = 'select a.id, a.content, b.id, b.name from bq_option a left join bq_question b on a.question_id = b.id ' \
  87. 'where a.serial_number = %s and b.serial_number = %s and a.status = b.status = 1 '
  88. # 获取答题人的年龄段集合
  89. sql_4 = 'select nld from f_t_daren_score_2 group by nld'
  90. # 根据城市,年龄段,人群分类统计答题记录数
  91. sql_5 = 'select testcase_id, COUNT(uuid) from f_t_daren_score_2 where uuid in %s group by testcase_id '
  92. # 根据父选项获取子选项id列表
  93. sql_6 = 'SELECT c.id, c.sub_question_id, c.content FROM bq_sub_option c WHERE c.father_id in (SELECT a.id FROM ' \
  94. 'bq_option a ' \
  95. 'LEFT JOIN bq_question b ON a.question_id = b.id WHERE a.serial_number = %s AND b.serial_number = %s ' \
  96. 'and a.status = 1 and b.status = 1) and c.status = 1 '
  97. # 根据子题id获取包含子题id的测试
  98. sql_7 = 'select id from bq_testcase where status = 1 and FIND_IN_SET(%s, question_ids)'
  99. # 根据子选项id统计答题数
  100. sql_8 = '''
  101. SELECT
  102. count(1)
  103. FROM
  104. f_t_daren_score_2 a
  105. LEFT JOIN d_shangju_tiku_02 b ON a.sub_question_id = b.sub_question_id
  106. AND (
  107. a.score = b.score
  108. OR a.score = b.sub_option_id
  109. )
  110. AND a.testcase_id = b.testcase_id
  111. WHERE
  112. b.sub_option_id IN % s
  113. AND a.uuid IN % s
  114. '''
  115. # 获取一个uuid下答题的子选项id列表
  116. sql_10 = 'select DISTINCT uuid, GROUP_CONCAT(DISTINCT b.sub_option_id) from f_t_daren_score_2 a left join ' \
  117. 'd_shangju_tiku_02 b on a.sub_question_id = b.sub_question_id and (a.score = b.score or a.score = ' \
  118. 'b.sub_option_id) where a.status = ' \
  119. 'b.status = 1 group by uuid '
  120. # 向表mvp_crowd_info插入数据
  121. sql_11 = 'insert into mvp_crowd_info(age_area, city_name, crowd_type, status) values(%s, %s, %s, 1)'
  122. # 向表mvp_crowd_info_behavior中插入数据
  123. sql_12 = 'insert into mvp_crowd_info_behavior(crowd_info_id, behavioral_interest, standard_value, status) values(' \
  124. '%s, %s, ' \
  125. '%s, 1) '
  126. # 向表mvp_crowd_info_module中插入数据
  127. sql_13 = 'insert into mvp_crowd_info_module(crowd_info_id, module_name, standard_value, status) values (%s, %s, ' \
  128. '%s, 1) '
  129. sql_14 = 'select a.id, a.age_area, a.city_name, a.crowd_type from mvp_crowd_info a where a.status = 1'
  130. # 获取答题城市信息from city
  131. sql_15 = '''
  132. SELECT
  133. a.uuid,
  134. IFNULL(GROUP_CONCAT(DISTINCT a.city, a.province), 00) AS city,
  135. IFNULL(GROUP_CONCAT(DISTINCT a.nld), 00) AS nld,
  136. IFNULL(GROUP_CONCAT(DISTINCT a.sex), 00) AS sex,
  137. IFNULL(GROUP_CONCAT(DISTINCT b.sub_option_id), 00) as sub_option_ids,
  138. IFNULL(GROUP_CONCAT(DISTINCT a.testcase_id), 00) as testcase_ids
  139. FROM
  140. f_t_daren_score_2 a
  141. LEFT JOIN d_shangju_tiku_02 b ON a.testcase_id = b.testcase_id
  142. WHERE
  143. a.testcase_id = b.testcase_id
  144. AND a.sub_question_id = b.sub_question_id
  145. AND (
  146. a.score = b.score
  147. OR a.score = b.sub_option_id
  148. )
  149. GROUP BY
  150. a.uuid
  151. '''
  152. # 根据用户uuid获取城市信息
  153. sql_16 = '''
  154. SELECT
  155. a.uuid,
  156. b.sub_option_content
  157. FROM
  158. f_t_daren_score_2 a
  159. LEFT JOIN d_shangju_tiku_02 b ON a.testcase_id = b.testcase_id
  160. WHERE
  161. a.sub_question_id = b.sub_question_id
  162. AND (
  163. a.score = b.score
  164. OR a.score = b.sub_option_id
  165. )
  166. AND a.uuid = %s
  167. AND b.father_id = 249
  168. AND a. STATUS = b. STATUS = 1
  169. '''
  170. # 答题人人群分类信息
  171. sql_17 = '''
  172. SELECT
  173. a.uuid,
  174. b.sub_option_id
  175. FROM
  176. f_t_daren_score_2 a
  177. LEFT JOIN d_shangju_tiku_02 b ON a.testcase_id = b.testcase_id
  178. WHERE
  179. a.sub_question_id = b.sub_question_id
  180. AND (
  181. a.score = b.score
  182. OR a.score = b.sub_option_id
  183. )
  184. AND a.uuid = %s
  185. AND b.father_id = 236
  186. AND a. STATUS = b. STATUS = 1
  187. '''
  188. def __init__(self, path=None):
  189. self.shangju_db = MysqlDB('shangju')
  190. self.marketing_db = MysqlDB('bi_report')
  191. # self.shangju_db.truncate('mvp_standard_score')
  192. self.tag_data = ExcelUtil(file_name=path).init_mvp_data()
  193. self.crowd_info = ExcelUtil(file_name=path, sheet_name='选项-人群分类对应表').init_crowd_info()
  194. self.citys = self.init_city()
  195. self.age = self.init_age()
  196. self.people_sub_option_ids = self.marketing_db.select(self.sql_10)
  197. self.crowd_contain_sub_option_ids = self.get_crowd_contain_sub_option_ids()
  198. self.module_scores = ExcelUtil(file_name='set-behavior-tag.xlsx', sheet_name='算法关系表').init_module_info()
  199. # self.scores_tag = ExcelUtil(file_name='行为与模块分值汇总.xlsx', sheet_name='行为').init_scores()
  200. # self.score_module = ExcelUtil(file_name='行为与模块分值汇总.xlsx', sheet_name='模块').init_scores()
  201. self.scores_tag = None
  202. self.score_module = None
  203. def close(self):
  204. self.shangju_db.close()
  205. self.marketing_db.close()
  206. def init_city(self):
  207. """
  208. 获取答题数据中的城市。
  209. :return:
  210. """
  211. citys = ['上海市', '上海周边']
  212. # citys_info = self.marketing_db.select(self.sql_1)
  213. # citys.extend([x[0] for x in citys_info if x[0] is not None])
  214. return citys
  215. def query_behavioral_info(self, city=None, age=None, crowd=None):
  216. """
  217. 查询行为兴趣信息
  218. :return:
  219. """
  220. # datas = []
  221. # for key in self.tag_data.keys():
  222. # values = self.tag_data[key]
  223. # for value in values:
  224. # question = value[0].split('-')[0]
  225. # option = value[0].split('-')[1]
  226. # corr = value[1]
  227. # data = self.shangju_db.select(self.sql_2, [option, question])
  228. # if len(data) > 0:
  229. # print([question, option, data[0][3], data[0][1], key, corr])
  230. # datas.append([question, option, data[0][3], data[0][1], key, corr])
  231. # self.shangju_db.truncate('mvp_question_classification')
  232. # self.shangju_db.add_some(self.sql_3, datas)
  233. scores_behavioral = self.city_age_crowd(city, age, crowd)
  234. # scores_module = self.module_score(crowd, city, age, scores_behavioral['score'])
  235. # result = {'行为兴趣分值': scores_behavioral['score'], '模块分值': scores_module}
  236. print('update finished!!!')
  237. return scores_behavioral
  238. def people_info(self):
  239. """
  240. 答题人个人信息获取
  241. :return:
  242. """
  243. people_info_city = self.marketing_db.select(self.sql_15)
  244. people_infos = []
  245. for people in people_info_city:
  246. uuid = people[0]
  247. city = people[1]
  248. nld = people[2]
  249. sex = people[3]
  250. sub_option_ids_1 = people[4]
  251. testcaseid = people[5]
  252. if str(city).find('市') != -1:
  253. city = str(city).split('市')[0] + '市'
  254. if str(nld).find(',') != -1:
  255. nld_1 = list(str(nld).split(','))
  256. if len(nld_1) > 0:
  257. nld = nld_1[0]
  258. else:
  259. pass
  260. crowd = []
  261. if testcaseid:
  262. testcastids = list(map(int, str(testcaseid).split(',')))
  263. if len(testcastids) > 0:
  264. gt_75 = [x for x in testcastids if x in [75, 76, 77, 78]]
  265. if city is None and len(gt_75) > 0:
  266. # 从答题结果中获取城市信息
  267. citys = self.marketing_db.select(self.sql_16, [uuid])
  268. if len(citys) > 0:
  269. for info in citys:
  270. city_name = info[1]
  271. if city_name in ['一线', '二线', '其他']:
  272. city = city_name
  273. else:
  274. city = '二线'
  275. # 根据用户子选项id集合,获取用户的人群分类
  276. if len(gt_75) > 0:
  277. # 特定的测试人群分类从答题结果中获取
  278. sub_option_ids = self.marketing_db.select(self.sql_17, [uuid])
  279. for option in sub_option_ids:
  280. crowd_type = self.crowd_info_1.get(option[1])
  281. if crowd_type:
  282. crowd.append(crowd_type)
  283. else:
  284. crowd.append('A')
  285. else:
  286. if sub_option_ids_1 is not None:
  287. crowd.extend(self.get_people_uuid_by_sub_option_ids(sub_option_ids_1))
  288. if city is None:
  289. city = '一线'
  290. people_info = PeopleInfo(uuid, city, nld, sex, crowd)
  291. people_infos.append(people_info)
  292. # people_infos.append([uuid, city, nld, sex, crowd])
  293. return people_infos
  294. def people_filter(self, city, nld, crowd):
  295. uuids = []
  296. for people in self.people_info_1:
  297. if people.city == city and people.age == nld and crowd in people.crowd:
  298. uuids.append(people.uuid)
  299. return uuids
  300. def get_people_uuid_by_sub_option_ids(self, sub_ids):
  301. types = []
  302. for key in self.crowd_contain_sub_option_ids.keys():
  303. type_sub_option_ids = self.crowd_contain_sub_option_ids[key]
  304. sub_option_ids = list(map(int, str(sub_ids).split(',')))
  305. # list(set(a).intersection(set(b)))
  306. if len(list(set(sub_option_ids).intersection(set(type_sub_option_ids)))) > 0 and key not in types:
  307. types.append(key)
  308. return types
  309. def update_data(self):
  310. """
  311. 定时更新分值
  312. :return:
  313. """
  314. for city in self.city_list:
  315. for age in self.age_list:
  316. for crowd in self.crowd:
  317. result = self.city_age_crowd(city, age, crowd)
  318. self.insert_score_to_db(result)
  319. print('{}数据关系完成...'.format(time.time()))
  320. def insert_score_to_db(self, scores):
  321. """
  322. 行为、模块分数写入数据库
  323. :return:
  324. """
  325. ids = self.query_data()
  326. behavior_score = scores['behavior_score']
  327. module_score = scores['module_score']
  328. module_insert_sql = self.get_insert_sql('模块分数')
  329. if module_insert_sql:
  330. module_insert_data = []
  331. for module in module_score:
  332. city_2 = module[0]
  333. age_2 = module[1]
  334. crowd_2 = module[2]
  335. module_name_2 = module[3]
  336. module_score_2 = module[4]
  337. for id in ids:
  338. city_1 = id[2]
  339. age_1 = id[1]
  340. crowd_1 = id[3]
  341. id_1 = id[0]
  342. if city_2 == city_1 and self.age_dict[age_2] == age_1 and crowd_2 == crowd_1:
  343. module_insert_data.append([id_1, module_name_2, module_score_2])
  344. # 先清空之前的数据
  345. table_name = self.get_table_name('模块分数')
  346. if table_name:
  347. self.shangju_db.truncate(table_name)
  348. self.shangju_db.add_some(module_insert_sql, module_insert_data)
  349. print('模块分数更新完成...')
  350. for b_score in behavior_score:
  351. for key in b_score.keys():
  352. insert_sql = self.get_insert_sql(key)
  353. if insert_sql:
  354. insert_data = []
  355. score = b_score[key]
  356. for data in score:
  357. city = data[0]
  358. age = data[1]
  359. tag_name = data[2]
  360. crowd = data[3]
  361. tag_score = data[4]
  362. for id in ids:
  363. city_1 = id[2]
  364. age_1 = id[1]
  365. crowd_1 = id[3]
  366. id_1 = id[0]
  367. if city == city_1 and self.age_dict[age] == age_1 and crowd == crowd_1:
  368. insert_data.append([id_1, tag_name, tag_score])
  369. if len(insert_data) > 0:
  370. table_name = self.get_table_name(key)
  371. if table_name:
  372. self.shangju_db.truncate(table_name)
  373. self.shangju_db.add_some(insert_sql, insert_data)
  374. else:
  375. print('未找到对应的表,数据无法插入...')
  376. print('行为分数更新完成...')
  377. def module_score(self, crowd, city, age, scores):
  378. """
  379. 模块分数计算
  380. 城市 年龄 人群分类 模块名称 分数
  381. :return:
  382. """
  383. # import json
  384. # print(json.dumps(scores, ensure_ascii=False))
  385. modules = self.module_scores[crowd]
  386. result = []
  387. for key in modules.keys():
  388. values = modules[key]
  389. module_name = key
  390. score = 0
  391. for value in values:
  392. behavioral_name = value[0]
  393. weight = float(value[2])
  394. standard_score = [x[4] for x in scores if x[2] == behavioral_name]
  395. if len(standard_score) > 0:
  396. score += standard_score[0] * weight
  397. result.append([city, age, crowd, module_name, score])
  398. return result
  399. # def insert_data(self, scores_behavioral, scores_module):
  400. def insert(self):
  401. """
  402. 计算数据写入数据库中,供接口查看
  403. :return:
  404. """
  405. infos = []
  406. for city in ['上海市', '宁波市', '苏州市', '杭州市', ' 无锡市']:
  407. for age in ['50-59年生', '60-69年生', '70-74年生', '75-79年生', '80-84年生', '85-89年生', '90-94年生', '95-99年生', '00'
  408. '-04年生',
  409. '05-09年生', '10-14年生', '15-19年生']:
  410. for c_type in ['A', 'B', 'C', 'D', 'E', 'F']:
  411. age_area = self.age_dict.get(age)
  412. if age_area:
  413. infos.append([age_area, city, c_type])
  414. self.shangju_db.add_some(self.sql_11, infos)
  415. def query_data(self):
  416. ids = self.shangju_db.select(self.sql_14)
  417. return ids
  418. def shanghai_85_module_score_insert(self):
  419. """
  420. 上海市,85后模块分数计算
  421. :return:
  422. """
  423. result = []
  424. for crowd in self.crowd:
  425. modules = self.module_scores[crowd]
  426. for key in modules.keys():
  427. values = modules[key]
  428. module_name = key
  429. score = 0
  430. for value in values:
  431. behavioral_name = value[0]
  432. weight = float(value[2])
  433. # standard_score = [x[4] for x in scores if x[2] == behavioral_name]
  434. standard_score = float(value[1])
  435. if standard_score is not None:
  436. score += standard_score * weight
  437. result.append(['上海市', '85后', crowd, module_name, score])
  438. return {'score': result, 'data': self.module_scores}
  439. def tag_module_score_insert(self):
  440. """
  441. 标签模块分数写入数据库
  442. :return:
  443. """
  444. ids = self.query_data()
  445. insert_data = []
  446. insert_data_1 = []
  447. for tag, module in zip(self.scores_tag, self.score_module):
  448. city = tag[0]
  449. age = tag[1]
  450. crowd = tag[2]
  451. tag_name = tag[3]
  452. tag_score = tag[4]
  453. city_2 = module[0]
  454. age_2 = module[1]
  455. crowd_2 = module[2]
  456. module_name_2 = module[3]
  457. module_score_2 = module[4]
  458. for id in ids:
  459. city_1 = id[2]
  460. age_1 = id[1]
  461. crowd_1 = id[3]
  462. id_1 = id[0]
  463. if city == city_1 and self.age_dict[age] == age_1 and crowd == crowd_1:
  464. insert_data.append([id_1, tag_name, tag_score])
  465. if city_2 == city_1 and self.age_dict[age_2] == age_1 and crowd_2 == crowd_1:
  466. insert_data_1.append([id_1, module_name_2, module_score_2])
  467. self.shangju_db.add_some(self.sql_12, insert_data)
  468. self.shangju_db.add_some(self.sql_13, insert_data_1)
  469. def init_age(self):
  470. """
  471. 获取答题数据中的年龄
  472. """
  473. return ['95后', '85后']
  474. # age_info = self.marketing_db.select(self.sql_4)
  475. # # print([x[0] for x in age_info])
  476. # return [x[0] for x in age_info if x[0] is not None]
  477. def city_age_crowd(self, city=None, age=None, crowd=None):
  478. data_start = []
  479. result = []
  480. module_scores = []
  481. self.people_info_1 = self.people_info()
  482. if city is not None and age is not None and crowd is not None:
  483. print('获取指定城市,年龄段,人群类型的数据...')
  484. # people_uuids = self.get_people_uuid_by_type(crowd)
  485. people_uuids = self.people_filter(city, age, crowd)
  486. behavior_data = None
  487. if len(people_uuids) > 0:
  488. print('{}-{}-{}'.format(city, age, crowd))
  489. datas = self.behavior_tag_init(city, age, people_uuids)
  490. data_start.append(datas)
  491. all_data, behavior_data_1 = self.calculation_standard_score(datas, city, age, crowd)
  492. result.append(all_data)
  493. behavior_data = behavior_data_1
  494. if behavior_data:
  495. module_scores.extend(self.module_score(crowd, city, age, behavior_data))
  496. # data_list = []
  497. # for e in data_start:
  498. # for key in e.keys():
  499. # values = e[key]
  500. # for sub_e in values:
  501. # ele = [key]
  502. # ele.extend(sub_e)
  503. # data_list.append(ele)
  504. # pass
  505. return {'behavior_score': result, 'module_score': module_scores}
  506. # return {'score': result, 'data': data_list}
  507. def scores(self):
  508. behavior_score = []
  509. module_scores = []
  510. for city in self.city_list:
  511. for age in self.age_list:
  512. for crowd in self.crowd:
  513. data = self.city_age_crowd(city, age, crowd)
  514. behavior_score.extend(data['behavior_score'])
  515. module_scores.extend(data['module_score'])
  516. return {'behavior_score': behavior_score, 'module_score': module_scores}
  517. def behavior_tag_init(self, city, age, people_uuids):
  518. result = {}
  519. self.group_type_count = self.marketing_db.select(self.sql_5, [people_uuids])
  520. # 表名
  521. for key in self.tag_data.keys():
  522. values = self.tag_data[key]
  523. result_sub = {}
  524. # 标签
  525. for key_tag_name in values.keys():
  526. questions = values[key_tag_name]
  527. elements = []
  528. for value in questions:
  529. question = value[0].split('-')[0]
  530. option = value[0].split('-')[1]
  531. corr = value[1]
  532. fz, fm = self.molecular_value(question, option, city, age, people_uuids)
  533. if fm == 0:
  534. c = 0
  535. else:
  536. c = fz / fm
  537. elements.append([question, option, corr, fz, fm, c])
  538. result_sub[key_tag_name] = elements
  539. result[key] = self.indicator_calculation_d_e(result_sub)
  540. return result
  541. def molecular_value(self, queston, option, city, age, people_uuids):
  542. # 获取当前父选项包含的子选项id和子题id列表
  543. result = self.shangju_db.select(self.sql_6, [option, queston])
  544. sub_option_ids = []
  545. group_types = []
  546. for rt in result:
  547. sub_option_id, sub_question_id, content = rt[0], rt[1], rt[2]
  548. grouptypes = self.shangju_db.select(self.sql_7, [sub_question_id])
  549. for g_t in grouptypes:
  550. if str(g_t[0]) not in group_types:
  551. group_types.append(str(g_t[0]))
  552. sub_option_ids.append(sub_option_id)
  553. # 计算子选项在答题记录中的点击数
  554. sub_options_count = 0
  555. if len(sub_option_ids) > 0:
  556. result_1 = self.marketing_db.select(self.sql_8, [sub_option_ids, people_uuids])
  557. sub_options_count = result_1[0][0]
  558. # 计算父选项包含的子选项对应的子题所在的测试gt包含的点击数。
  559. denominator_value = 0
  560. for info in self.group_type_count:
  561. if str(info[0]) in group_types:
  562. denominator_value += info[1]
  563. return sub_options_count, denominator_value
  564. def indicator_calculation_d_e(self, data):
  565. result = {}
  566. for key in data.keys():
  567. values = data[key]
  568. c_list = []
  569. for x in values:
  570. _x = x[5]
  571. if _x is not None and x != 0:
  572. c_list.append(_x)
  573. fm_list = [x[4] for x in values]
  574. sum_c = sum(fm_list)
  575. if len(c_list) == 0:
  576. min_c = 0
  577. else:
  578. min_c = min(c_list)
  579. elements = []
  580. for value in values:
  581. _value = []
  582. c = value[5]
  583. if sum_c == 0:
  584. d = 0
  585. else:
  586. d = c / sum_c
  587. e = c - min_c
  588. _value.extend(value)
  589. _value.append(d)
  590. _value.append(e)
  591. elements.append(_value)
  592. result[key] = elements
  593. return result
  594. def calculation_standard_score(self, datas, city, age, crowd_type):
  595. scores = {}
  596. for key_tag_type in datas.keys():
  597. print(key_tag_type)
  598. tag_type_data = datas[key_tag_type]
  599. scores_sub = []
  600. for key_tag in tag_type_data.keys():
  601. key_tag_data = tag_type_data[key_tag]
  602. print(key_tag)
  603. print(' 父题序号 父选项序号 相关系系数 分子值 分母值 百分比 人数权重 偏离值')
  604. values = [x[5] for x in key_tag_data]
  605. min_c = min(values)
  606. f = min_c
  607. for value in key_tag_data:
  608. print(' {}'.format(value))
  609. if value[2] is not None and value[7] is not None:
  610. f += float(value[2] * value[7])
  611. print(' 标准分:{}'.format(f))
  612. scores_sub.append([city, age, key_tag, crowd_type, f])
  613. scores[key_tag_type] = scores_sub
  614. # self.shangju_db.add_some(self.sql_9, scores)
  615. return scores, scores['用户画像-行为兴趣']
  616. def people_data(self):
  617. result = self.people_info()
  618. a = 0
  619. b = 0
  620. c = 0
  621. d = 0
  622. e = 0
  623. f = 0
  624. for rt in result:
  625. crowds = rt.crowd
  626. if 'A' in crowds:
  627. a += 1
  628. if 'B' in crowds:
  629. b += 1
  630. if 'C' in crowds:
  631. c += 1
  632. if 'D' in crowds:
  633. d += 1
  634. if 'E' in crowds:
  635. e += 1
  636. if 'F' in crowds:
  637. f += 1
  638. return {'A': a, 'B': b, 'C': b, 'D': d, 'E': e, 'F': f}
  639. def get_crowd_people(self):
  640. result = {}
  641. for type in self.crowd:
  642. uuids = self.get_people_uuid_by_type(type)
  643. result[type] = len(uuids)
  644. return result
  645. def get_people_uuid_by_type(self, type):
  646. uuids = []
  647. type_sub_option_ids = self.crowd_contain_sub_option_ids[type]
  648. for people in self.people_sub_option_ids:
  649. uuid = people[0]
  650. sub_option_ids = list(map(int, str(people[1]).split(',')))
  651. # list(set(a).intersection(set(b)))
  652. if len(list(set(sub_option_ids).intersection(set(type_sub_option_ids)))) > 0 and uuid not in uuids:
  653. uuids.append(uuid)
  654. return uuids
  655. def get_crowd_contain_sub_option_ids(self):
  656. """
  657. 获取ABCDEF人群包含的子选项id
  658. :return:
  659. """
  660. infos = {}
  661. for key in self.crowd_info.keys():
  662. values = self.crowd_info[key]
  663. sub_option_ids = []
  664. for value in values:
  665. if value is not None:
  666. vals = str(value).split('-')
  667. option, question = vals[1], vals[0]
  668. query_result = self.shangju_db.select(self.sql_6, [option, question])
  669. for qr in query_result:
  670. sub_option_id, sub_question_id, content = qr[0], qr[1], qr[2]
  671. sub_option_ids.append(int(sub_option_id))
  672. infos[key] = sub_option_ids
  673. return infos
  674. if __name__ == '__main__':
  675. mvp = Mvp()
  676. mvp.people_info()