mvp.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 = '''
  94. SELECT
  95. c.id,
  96. c.sub_question_id,
  97. c.content
  98. FROM
  99. bq_sub_option c
  100. WHERE
  101. c.father_id IN (
  102. SELECT
  103. a.id
  104. FROM
  105. bq_option a
  106. LEFT JOIN bq_question b ON a.question_id = b.id
  107. WHERE
  108. a.serial_number = % s
  109. AND b.serial_number = % s
  110. AND a. STATUS = 1
  111. AND b. STATUS = 1
  112. )
  113. AND c. STATUS = 1
  114. '''
  115. # 根据子题id获取包含子题id的测试
  116. sql_7 = 'select id from bq_testcase where status = 1 and FIND_IN_SET(%s, question_ids)'
  117. # 根据子选项id统计答题数
  118. sql_8 = '''
  119. SELECT
  120. count(1)
  121. FROM
  122. f_t_daren_score_2 a
  123. LEFT JOIN d_shangju_tiku_02 b ON 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. AND a.testcase_id = b.testcase_id
  129. WHERE
  130. b.sub_option_id IN % s
  131. AND a.uuid IN % s
  132. '''
  133. # 获取一个uuid下答题的子选项id列表
  134. sql_10 = 'select DISTINCT uuid, GROUP_CONCAT(DISTINCT b.sub_option_id) from f_t_daren_score_2 a left join ' \
  135. 'd_shangju_tiku_02 b on a.sub_question_id = b.sub_question_id and (a.score = b.score or a.score = ' \
  136. 'b.sub_option_id) where a.status = ' \
  137. 'b.status = 1 group by uuid '
  138. # 向表mvp_crowd_info插入数据
  139. sql_11 = 'insert into mvp_crowd_info(age_area, city_name, crowd_type, status) values(%s, %s, %s, 1)'
  140. # 向表mvp_crowd_info_behavior中插入数据
  141. sql_12 = 'insert into mvp_crowd_info_behavior(crowd_info_id, behavioral_interest, standard_value, status) values(' \
  142. '%s, %s, ' \
  143. '%s, 1) '
  144. # 向表mvp_crowd_info_module中插入数据
  145. sql_13 = 'insert into mvp_crowd_info_module(crowd_info_id, module_name, standard_value, status) values (%s, %s, ' \
  146. '%s, 1) '
  147. sql_14 = 'select a.id, a.age_area, a.city_name, a.crowd_type from mvp_crowd_info a where a.status = 1'
  148. # 获取答题城市信息from city
  149. sql_15 = '''
  150. SELECT
  151. a.uuid,
  152. IFNULL(GROUP_CONCAT(DISTINCT a.city, a.province), 00) AS city,
  153. IFNULL(GROUP_CONCAT(DISTINCT a.nld), 00) AS nld,
  154. IFNULL(GROUP_CONCAT(DISTINCT a.sex), 00) AS sex,
  155. IFNULL(GROUP_CONCAT(DISTINCT b.sub_option_id), 00) as sub_option_ids,
  156. IFNULL(GROUP_CONCAT(DISTINCT a.testcase_id), 00) as testcase_ids
  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.testcase_id = b.testcase_id
  162. AND a.sub_question_id = b.sub_question_id
  163. AND (
  164. a.score = b.score
  165. OR a.score = b.sub_option_id
  166. )
  167. GROUP BY
  168. a.uuid
  169. '''
  170. # 根据用户uuid获取城市信息
  171. sql_16 = '''
  172. SELECT
  173. a.uuid,
  174. CASE b.sub_option_content
  175. WHEN '一线' THEN
  176. '上海市'
  177. WHEN '二线' THEN
  178. '上海周边'
  179. WHEN '其他' THEN
  180. '其他'
  181. END AS city
  182. FROM
  183. f_t_daren_score_2 a
  184. LEFT JOIN d_shangju_tiku_02 b ON a.testcase_id = b.testcase_id
  185. WHERE
  186. a.sub_question_id = b.sub_question_id
  187. AND (
  188. a.score = b.score
  189. OR a.score = b.sub_option_id
  190. )
  191. AND a.uuid = 'ae9db26b-3606-497c-83c5-56341d487a91'
  192. AND b.father_id = 249
  193. AND a. STATUS = b. STATUS = 1
  194. '''
  195. # 答题人人群分类信息
  196. sql_17 = '''
  197. SELECT
  198. a.uuid,
  199. b.sub_option_id
  200. FROM
  201. f_t_daren_score_2 a
  202. LEFT JOIN d_shangju_tiku_02 b ON a.testcase_id = b.testcase_id
  203. WHERE
  204. a.sub_question_id = b.sub_question_id
  205. AND (
  206. a.score = b.score
  207. OR a.score = b.sub_option_id
  208. )
  209. AND a.uuid = %s
  210. AND b.father_id = 236
  211. AND a.STATUS = b.STATUS = 1
  212. '''
  213. """
  214. 数据debug SQL
  215. 1:
  216. SELECT
  217. c.id,
  218. c.sub_question_id,
  219. c.content
  220. FROM
  221. bq_sub_option c
  222. WHERE
  223. c.father_id IN (
  224. SELECT
  225. a.id
  226. FROM
  227. bq_option a
  228. LEFT JOIN bq_question b ON a.question_id = b.id
  229. WHERE
  230. a.serial_number ='FA001'
  231. AND b.serial_number = 'F00245'
  232. AND a. STATUS = 1
  233. AND b. STATUS = 1
  234. )
  235. AND c.STATUS = 1
  236. 2:
  237. select id from bq_testcase where status = 1 and FIND_IN_SET(%s, question_ids)
  238. 3:
  239. SELECT
  240. count(1)
  241. FROM
  242. f_t_daren_score_2 a
  243. LEFT JOIN d_shangju_tiku_02 b ON a.sub_question_id = b.sub_question_id
  244. AND (
  245. a.score = b.score
  246. OR a.score = b.sub_option_id
  247. )
  248. AND a.testcase_id = b.testcase_id
  249. WHERE
  250. b.sub_option_id IN (1964,1965,1966,1967,1968,1969,1970,1971,1972)
  251. """
  252. def __init__(self, path=None):
  253. self.shangju_db = MysqlDB('shangju')
  254. self.marketing_db = MysqlDB('bi_report')
  255. # self.shangju_db.truncate('mvp_standard_score')
  256. self.tag_data = ExcelUtil(file_name=path).init_mvp_data()
  257. self.crowd_info = ExcelUtil(file_name=path, sheet_name='选项-人群分类对应表').init_crowd_info()
  258. self.citys = self.init_city()
  259. self.age = self.init_age()
  260. self.people_sub_option_ids = self.marketing_db.select(self.sql_10)
  261. self.crowd_contain_sub_option_ids = self.get_crowd_contain_sub_option_ids()
  262. self.module_scores = ExcelUtil(file_name='set-behavior-tag.xlsx', sheet_name='算法关系表').init_module_info()
  263. # self.scores_tag = ExcelUtil(file_name='行为与模块分值汇总.xlsx', sheet_name='行为').init_scores()
  264. # self.score_module = ExcelUtil(file_name='行为与模块分值汇总.xlsx', sheet_name='模块').init_scores()
  265. self.scores_tag = None
  266. self.score_module = None
  267. def close(self):
  268. self.shangju_db.close()
  269. self.marketing_db.close()
  270. def init_city(self):
  271. """
  272. 获取答题数据中的城市。
  273. :return:
  274. """
  275. citys = ['上海市', '上海周边']
  276. # citys_info = self.marketing_db.select(self.sql_1)
  277. # citys.extend([x[0] for x in citys_info if x[0] is not None])
  278. return citys
  279. def query_behavioral_info(self, city=None, age=None, crowd=None):
  280. """
  281. 查询行为兴趣信息
  282. :return:
  283. """
  284. # datas = []
  285. # for key in self.tag_data.keys():
  286. # values = self.tag_data[key]
  287. # for value in values:
  288. # question = value[0].split('-')[0]
  289. # option = value[0].split('-')[1]
  290. # corr = value[1]
  291. # data = self.shangju_db.select(self.sql_2, [option, question])
  292. # if len(data) > 0:
  293. # print([question, option, data[0][3], data[0][1], key, corr])
  294. # datas.append([question, option, data[0][3], data[0][1], key, corr])
  295. # self.shangju_db.truncate('mvp_question_classification')
  296. # self.shangju_db.add_some(self.sql_3, datas)
  297. scores_behavioral = self.city_age_crowd(city, age, crowd)
  298. # scores_module = self.module_score(crowd, city, age, scores_behavioral['score'])
  299. # result = {'行为兴趣分值': scores_behavioral['score'], '模块分值': scores_module}
  300. print('update finished!!!')
  301. return scores_behavioral
  302. def people_info(self):
  303. """
  304. 答题人个人信息获取
  305. :return:
  306. """
  307. people_info_city = self.marketing_db.select(self.sql_15)
  308. people_infos = []
  309. for people in people_info_city:
  310. uuid = people[0]
  311. city = people[1]
  312. nld = people[2]
  313. sex = people[3]
  314. sub_option_ids_1 = people[4]
  315. testcaseid = people[5]
  316. if str(city).find('市') != -1:
  317. city = str(city).split('市')[0] + '市'
  318. if str(nld).find(',') != -1:
  319. nld_1 = list(str(nld).split(','))
  320. if len(nld_1) > 0:
  321. nld = nld_1[0]
  322. else:
  323. pass
  324. crowd = []
  325. if testcaseid:
  326. testcastids = list(map(int, str(testcaseid).split(',')))
  327. if len(testcastids) > 0:
  328. gt_75 = [x for x in testcastids if x > 74]
  329. if len(gt_75) > 0:
  330. # 从答题结果中获取城市信息
  331. citys = self.marketing_db.select(self.sql_16, [uuid])
  332. if len(citys) > 0:
  333. city = citys[0][1]
  334. # 根据用户子选项id集合,获取用户的人群分类
  335. if len(gt_75) > 0:
  336. # 特定的测试人群分类从答题结果中获取
  337. sub_option_ids = self.marketing_db.select(self.sql_17, [uuid])
  338. for option in sub_option_ids:
  339. crowd_type = self.crowd_info_1.get(option[1])
  340. if crowd_type:
  341. crowd.append(crowd_type)
  342. else:
  343. crowd.append('A')
  344. else:
  345. if sub_option_ids_1 is not None:
  346. crowd.extend(self.get_people_uuid_by_sub_option_ids(sub_option_ids_1))
  347. if city is None:
  348. city = '上海市'
  349. people_info = PeopleInfo(uuid, city, nld, sex, crowd)
  350. people_infos.append(people_info)
  351. # people_infos.append([uuid, city, nld, sex, crowd])
  352. return people_infos
  353. def people_filter(self, city, nld, crowd):
  354. uuids = []
  355. for people in self.people_info_1:
  356. if people.city == city and people.age == nld and crowd in people.crowd:
  357. uuids.append(people.uuid)
  358. return uuids
  359. def get_people_uuid_by_sub_option_ids(self, sub_ids):
  360. types = []
  361. for key in self.crowd_contain_sub_option_ids.keys():
  362. type_sub_option_ids = self.crowd_contain_sub_option_ids[key]
  363. sub_option_ids = list(map(int, str(sub_ids).split(',')))
  364. # list(set(a).intersection(set(b)))
  365. if len(list(set(sub_option_ids).intersection(set(type_sub_option_ids)))) > 0 and key not in types:
  366. types.append(key)
  367. return types
  368. def update_data(self):
  369. """
  370. 定时更新分值
  371. :return:
  372. """
  373. for city in self.city_list:
  374. for age in self.age_list:
  375. for crowd in self.crowd:
  376. result = self.city_age_crowd(city, age, crowd)
  377. self.insert_score_to_db(result)
  378. print('{}数据关系完成...'.format(time.time()))
  379. def insert_score_to_db(self, scores):
  380. """
  381. 行为、模块分数写入数据库
  382. :return:
  383. """
  384. ids = self.query_data()
  385. behavior_score = scores['behavior_score']
  386. module_score = scores['module_score']
  387. module_insert_sql = self.get_insert_sql('模块分数')
  388. if module_insert_sql:
  389. module_insert_data = []
  390. for module in module_score:
  391. city_2 = module[0]
  392. age_2 = module[1]
  393. crowd_2 = module[2]
  394. module_name_2 = module[3]
  395. module_score_2 = module[4]
  396. for id in ids:
  397. city_1 = id[2]
  398. age_1 = id[1]
  399. crowd_1 = id[3]
  400. id_1 = id[0]
  401. if city_2 == city_1 and self.age_dict[age_2] == age_1 and crowd_2 == crowd_1:
  402. module_insert_data.append([id_1, module_name_2, module_score_2])
  403. # 先清空之前的数据
  404. table_name = self.get_table_name('模块分数')
  405. if table_name:
  406. self.shangju_db.truncate(table_name)
  407. self.shangju_db.add_some(module_insert_sql, module_insert_data)
  408. print('模块分数更新完成...')
  409. for b_score in behavior_score:
  410. for key in b_score.keys():
  411. insert_sql = self.get_insert_sql(key)
  412. if insert_sql:
  413. insert_data = []
  414. score = b_score[key]
  415. for data in score:
  416. city = data[0]
  417. age = data[1]
  418. tag_name = data[2]
  419. crowd = data[3]
  420. tag_score = data[4]
  421. for id in ids:
  422. city_1 = id[2]
  423. age_1 = id[1]
  424. crowd_1 = id[3]
  425. id_1 = id[0]
  426. if city == city_1 and self.age_dict[age] == age_1 and crowd == crowd_1:
  427. insert_data.append([id_1, tag_name, tag_score])
  428. if len(insert_data) > 0:
  429. table_name = self.get_table_name(key)
  430. if table_name:
  431. self.shangju_db.truncate(table_name)
  432. self.shangju_db.add_some(insert_sql, insert_data)
  433. else:
  434. print('未找到对应的表,数据无法插入...')
  435. print('行为分数更新完成...')
  436. def module_score(self, crowd, city, age, scores):
  437. """
  438. 模块分数计算
  439. 城市 年龄 人群分类 模块名称 分数
  440. :return:
  441. """
  442. # import json
  443. # print(json.dumps(scores, ensure_ascii=False))
  444. modules = self.module_scores[crowd]
  445. result = []
  446. for key in modules.keys():
  447. values = modules[key]
  448. module_name = key
  449. score = 0
  450. for value in values:
  451. behavioral_name = value[0]
  452. weight = float(value[2])
  453. standard_score = [x[4] for x in scores if x[2] == behavioral_name]
  454. if len(standard_score) > 0:
  455. score += standard_score[0] * weight
  456. result.append([city, age, crowd, module_name, score])
  457. return result
  458. # def insert_data(self, scores_behavioral, scores_module):
  459. def insert(self):
  460. """
  461. 计算数据写入数据库中,供接口查看
  462. :return:
  463. """
  464. infos = []
  465. for city in self.city_list:
  466. for age in self.age_list:
  467. for c_type in self.crowd:
  468. age_area = self.age_dict.get(age)
  469. if age_area:
  470. infos.append([age_area, city, c_type])
  471. self.shangju_db.add_some(self.sql_11, infos)
  472. def query_data(self):
  473. ids = self.shangju_db.select(self.sql_14)
  474. return ids
  475. def shanghai_85_module_score_insert(self):
  476. """
  477. 上海市,85后模块分数计算
  478. :return:
  479. """
  480. result = []
  481. for crowd in self.crowd:
  482. modules = self.module_scores[crowd]
  483. for key in modules.keys():
  484. values = modules[key]
  485. module_name = key
  486. score = 0
  487. for value in values:
  488. behavioral_name = value[0]
  489. weight = float(value[2])
  490. # standard_score = [x[4] for x in scores if x[2] == behavioral_name]
  491. standard_score = float(value[1])
  492. if standard_score is not None:
  493. score += standard_score * weight
  494. result.append(['上海市', '85后', crowd, module_name, score])
  495. return {'score': result, 'data': self.module_scores}
  496. def tag_module_score_insert(self):
  497. """
  498. 标签模块分数写入数据库
  499. :return:
  500. """
  501. ids = self.query_data()
  502. insert_data = []
  503. insert_data_1 = []
  504. for tag, module in zip(self.scores_tag, self.score_module):
  505. city = tag[0]
  506. age = tag[1]
  507. crowd = tag[2]
  508. tag_name = tag[3]
  509. tag_score = tag[4]
  510. city_2 = module[0]
  511. age_2 = module[1]
  512. crowd_2 = module[2]
  513. module_name_2 = module[3]
  514. module_score_2 = module[4]
  515. for id in ids:
  516. city_1 = id[2]
  517. age_1 = id[1]
  518. crowd_1 = id[3]
  519. id_1 = id[0]
  520. if city == city_1 and self.age_dict[age] == age_1 and crowd == crowd_1:
  521. insert_data.append([id_1, tag_name, tag_score])
  522. if city_2 == city_1 and self.age_dict[age_2] == age_1 and crowd_2 == crowd_1:
  523. insert_data_1.append([id_1, module_name_2, module_score_2])
  524. self.shangju_db.add_some(self.sql_12, insert_data)
  525. self.shangju_db.add_some(self.sql_13, insert_data_1)
  526. def init_age(self):
  527. """
  528. 获取答题数据中的年龄
  529. """
  530. return ['95后', '85后']
  531. # age_info = self.marketing_db.select(self.sql_4)
  532. # # print([x[0] for x in age_info])
  533. # return [x[0] for x in age_info if x[0] is not None]
  534. def city_age_crowd(self, city=None, age=None, crowd=None):
  535. data_start = []
  536. result = []
  537. module_scores = []
  538. self.people_info_1 = self.people_info()
  539. if city is not None and age is not None and crowd is not None:
  540. print('获取指定城市,年龄段,人群类型的数据...')
  541. # people_uuids = self.get_people_uuid_by_type(crowd)
  542. people_uuids = self.people_filter(city, age, crowd)
  543. behavior_data = None
  544. if len(people_uuids) > 0:
  545. print('{}-{}-{}'.format(city, age, crowd))
  546. datas = self.behavior_tag_init(city, age, people_uuids)
  547. data_start.append(datas)
  548. all_data, behavior_data_1 = self.calculation_standard_score(datas, city, age, crowd)
  549. result.append(all_data)
  550. behavior_data = behavior_data_1
  551. if behavior_data:
  552. module_scores.extend(self.module_score(crowd, city, age, behavior_data))
  553. # data_list = []
  554. # for e in data_start:
  555. # for key in e.keys():
  556. # values = e[key]
  557. # for sub_e in values:
  558. # ele = [key]
  559. # ele.extend(sub_e)
  560. # data_list.append(ele)
  561. # pass
  562. return {'behavior_score': result, 'module_score': module_scores}
  563. # return {'score': result, 'data': data_list}
  564. def scores(self):
  565. behavior_score = []
  566. module_scores = []
  567. for city in self.city_list:
  568. for age in self.age_list:
  569. for crowd in self.crowd:
  570. data = self.city_age_crowd(city, age, crowd)
  571. behavior_score.extend(data['behavior_score'])
  572. module_scores.extend(data['module_score'])
  573. return {'behavior_score': behavior_score, 'module_score': module_scores}
  574. def behavior_tag_init(self, city, age, people_uuids):
  575. result = {}
  576. self.group_type_count = self.marketing_db.select(self.sql_5, [people_uuids])
  577. # 表名
  578. for key in self.tag_data.keys():
  579. values = self.tag_data[key]
  580. result_sub = {}
  581. # 标签
  582. for key_tag_name in values.keys():
  583. questions = values[key_tag_name]
  584. elements = []
  585. for value in questions:
  586. question = value[0].split('-')[0]
  587. option = value[0].split('-')[1]
  588. corr = value[1]
  589. fz, fm = self.molecular_value(question, option, city, age, people_uuids)
  590. if fm == 0:
  591. c = 0
  592. else:
  593. c = fz / fm
  594. elements.append([question, option, corr, fz, fm, c])
  595. result_sub[key_tag_name] = elements
  596. result[key] = self.indicator_calculation_d_e(result_sub)
  597. return result
  598. def molecular_value(self, queston, option, city, age, people_uuids):
  599. # 获取当前父选项包含的子选项id和子题id列表
  600. result = self.shangju_db.select(self.sql_6, [option, queston])
  601. sub_option_ids = []
  602. group_types = []
  603. for rt in result:
  604. sub_option_id, sub_question_id, content = rt[0], rt[1], rt[2]
  605. grouptypes = self.shangju_db.select(self.sql_7, [sub_question_id])
  606. for g_t in grouptypes:
  607. if str(g_t[0]) not in group_types:
  608. group_types.append(str(g_t[0]))
  609. sub_option_ids.append(sub_option_id)
  610. # 计算子选项在答题记录中的点击数
  611. sub_options_count = 0
  612. if len(sub_option_ids) > 0:
  613. result_1 = self.marketing_db.select(self.sql_8, [sub_option_ids, people_uuids])
  614. sub_options_count = result_1[0][0]
  615. # 计算父选项包含的子选项对应的子题所在的测试gt包含的点击数。
  616. denominator_value = 0
  617. for info in self.group_type_count:
  618. if str(info[0]) in group_types:
  619. denominator_value += info[1]
  620. return sub_options_count, denominator_value
  621. def indicator_calculation_d_e(self, data):
  622. result = {}
  623. for key in data.keys():
  624. values = data[key]
  625. c_list = []
  626. for x in values:
  627. _x = x[5]
  628. if _x is not None and x != 0:
  629. c_list.append(_x)
  630. fm_list = [x[4] for x in values]
  631. sum_c = sum(fm_list)
  632. if len(c_list) == 0:
  633. min_c = 0
  634. else:
  635. min_c = min(c_list)
  636. elements = []
  637. for value in values:
  638. _value = []
  639. c = value[5]
  640. if sum_c == 0:
  641. d = 0
  642. else:
  643. d = c / sum_c
  644. e = c - min_c
  645. _value.extend(value)
  646. _value.append(d)
  647. _value.append(e)
  648. elements.append(_value)
  649. result[key] = elements
  650. return result
  651. def calculation_standard_score(self, datas, city, age, crowd_type):
  652. scores = {}
  653. for key_tag_type in datas.keys():
  654. print(key_tag_type)
  655. tag_type_data = datas[key_tag_type]
  656. scores_sub = []
  657. for key_tag in tag_type_data.keys():
  658. key_tag_data = tag_type_data[key_tag]
  659. print(key_tag)
  660. print(' 父题序号 父选项序号 相关系系数 分子值 分母值 百分比 人数权重 偏离值')
  661. values = [x[5] for x in key_tag_data]
  662. min_c = min(values)
  663. f = min_c
  664. for value in key_tag_data:
  665. print(' {}'.format(value))
  666. if value[2] is not None and value[7] is not None:
  667. f += float(value[2] * value[7])
  668. print(' 标准分:{}'.format(f))
  669. scores_sub.append([city, age, key_tag, crowd_type, f])
  670. scores[key_tag_type] = scores_sub
  671. # self.shangju_db.add_some(self.sql_9, scores)
  672. return scores, scores['用户画像-行为兴趣']
  673. def people_data(self):
  674. result = self.people_info()
  675. a = 0
  676. b = 0
  677. c = 0
  678. d = 0
  679. e = 0
  680. f = 0
  681. result_1 = []
  682. for rt in result:
  683. crowds = rt.crowd
  684. if rt.uuid in [
  685. 'ae9db26b-3606-497c-83c5-56341d487a91',
  686. '9fb33b6c-bd7a-4114-b225-3ee380943517',
  687. '84636488-1307-47fe-a238-4f9cf279a908',
  688. '4a5b6654-eb99-46ed-8dcf-777648d6baca',
  689. 'ba181da0-c91a-4430-84c6-9612a693f659',
  690. '32eae583-474c-4dca-8b36-d74314f45cee',
  691. 'b07f6ff2-ccd5-44ee-9b7c-b2e1f40d777f',
  692. '149a0e40-5639-4771-8a27-60821e14a1d5',
  693. '4795b731-3e75-4f08-90bc-8ee4a0c366c6',
  694. '4795b731-3e75-4f08-90bc-8ee4a0c366c6',
  695. '47cbd398-1c39-4dc0-8d97-98fe19457516']:
  696. result_1.append([rt.uuid, rt.city, rt.age, rt.crowd])
  697. if 'A' in crowds:
  698. a += 1
  699. if 'B' in crowds:
  700. b += 1
  701. if 'C' in crowds:
  702. c += 1
  703. if 'D' in crowds:
  704. d += 1
  705. if 'E' in crowds:
  706. e += 1
  707. if 'F' in crowds:
  708. f += 1
  709. return result_1
  710. # return {'A': a, 'B': b, 'C': b, 'D': d, 'E': e, 'F': f}
  711. def get_crowd_people(self):
  712. result = {}
  713. for type in self.crowd:
  714. uuids = self.get_people_uuid_by_type(type)
  715. result[type] = len(uuids)
  716. return result
  717. def get_people_uuid_by_type(self, type):
  718. uuids = []
  719. type_sub_option_ids = self.crowd_contain_sub_option_ids[type]
  720. for people in self.people_sub_option_ids:
  721. uuid = people[0]
  722. sub_option_ids = list(map(int, str(people[1]).split(',')))
  723. # list(set(a).intersection(set(b)))
  724. if len(list(set(sub_option_ids).intersection(set(type_sub_option_ids)))) > 0 and uuid not in uuids:
  725. uuids.append(uuid)
  726. return uuids
  727. def get_crowd_contain_sub_option_ids(self):
  728. """
  729. 获取ABCDEF人群包含的子选项id
  730. :return:
  731. """
  732. infos = {}
  733. for key in self.crowd_info.keys():
  734. values = self.crowd_info[key]
  735. sub_option_ids = []
  736. for value in values:
  737. if value is not None:
  738. vals = str(value).split('-')
  739. option, question = vals[1], vals[0]
  740. query_result = self.shangju_db.select(self.sql_6, [option, question])
  741. for qr in query_result:
  742. sub_option_id, sub_question_id, content = qr[0], qr[1], qr[2]
  743. sub_option_ids.append(int(sub_option_id))
  744. infos[key] = sub_option_ids
  745. return infos
  746. if __name__ == '__main__':
  747. mvp = Mvp()
  748. mvp.people_info()