mvp.py 28 KB

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