process_data.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. """
  2. process the data to satisfy the lightgbm
  3. """
  4. import sys
  5. import os
  6. import json
  7. import asyncio
  8. import argparse
  9. from tqdm import tqdm
  10. import jieba.analyse
  11. from concurrent.futures.thread import ThreadPoolExecutor
  12. sys.path.append(os.getcwd())
  13. from functions import generate_label_date, generate_daily_strings, MysqlClient, MySQLClientSpider
  14. class DataProcessor(object):
  15. """
  16. Insert some information to lightgbm_data
  17. """
  18. def __init__(self, ):
  19. self.client = MysqlClient()
  20. self.client_spider = MySQLClientSpider()
  21. self.label_data = {}
  22. def generate_train_label(self, item, y_ori_data, cate):
  23. """
  24. 生成训练数据,用 np.array矩阵的方式返回,
  25. :return: x_train, 训练数据, y_train, 训练 label
  26. """
  27. video_id = item["video_id"]
  28. dt = item["dt"]
  29. useful_features = [
  30. "uid",
  31. "type",
  32. "channel",
  33. "fans",
  34. "view_count_user_30days",
  35. "share_count_user_30days",
  36. "return_count_user_30days",
  37. "rov_user",
  38. "str_user",
  39. "out_user_id",
  40. "mode",
  41. "out_play_cnt",
  42. "out_like_cnt",
  43. "out_share_cnt",
  44. "out_collection_cnt",
  45. ]
  46. spider_features = [
  47. "channel",
  48. "out_user_id",
  49. "mode",
  50. "out_play_cnt",
  51. "out_like_cnt",
  52. "out_share_cnt"
  53. ]
  54. user_features = [
  55. "uid",
  56. "channel",
  57. "fans",
  58. "view_count_user_30days",
  59. "share_count_user_30days",
  60. "return_count_user_30days",
  61. "rov_user",
  62. "str_user"
  63. ]
  64. match self.ll:
  65. case "all":
  66. item_features = [item[i] for i in useful_features]
  67. case "user":
  68. if item['type'] == "userupload":
  69. item_features = [item[i] for i in user_features]
  70. else:
  71. return None, None
  72. case "spider":
  73. if item['type'] == "spider":
  74. item_features = [item[i] for i in spider_features]
  75. lop, duration = self.cal_lop(video_id)
  76. item_features.append(lop)
  77. item_features.append(duration)
  78. else:
  79. return None, None
  80. keywords_textrank = self.title_processor(video_id)
  81. if keywords_textrank:
  82. for i in range(3):
  83. try:
  84. item_features.append(keywords_textrank[i])
  85. except:
  86. item_features.append(None)
  87. else:
  88. item_features.append(None)
  89. item_features.append(None)
  90. item_features.append(None)
  91. label_dt = generate_label_date(dt)
  92. label_obj = y_ori_data.get(label_dt, {}).get(video_id)
  93. if label_obj:
  94. label = int(label_obj[cate]) if label_obj[cate] else 0
  95. else:
  96. label = 0
  97. return label, item_features
  98. def producer(self):
  99. """
  100. 生成数据
  101. :return:none
  102. """
  103. # 把 label, video_title, daily_dt_str, 存储到 mysql 数据库中去
  104. label_path = "data/train_data/daily-label-20240101-20240325.json"
  105. with open(label_path, encoding="utf-8") as f:
  106. self.label_data = json.loads(f.read())
  107. def read_title(client, video_id):
  108. """
  109. read_title_from mysql
  110. """
  111. sql = f"""SELECT title from wx_video where id = {video_id};"""
  112. # print("title", sql)
  113. try:
  114. title = client.select(sql)[0][0]
  115. return title
  116. except Exception as e:
  117. print(video_id, "\t", e)
  118. return ""
  119. def generate_label(video_id, hourly_dt_str, label_info):
  120. """
  121. generate label daily_dt_str for mysql
  122. :param label_info:
  123. :param video_id:
  124. :param hourly_dt_str:
  125. :return: label, daily_dt_str
  126. """
  127. label_dt = generate_label_date(hourly_dt_str)
  128. label_obj = label_info.get(label_dt, {}).get(video_id)
  129. if label_obj:
  130. label = int(label_obj["total_return"]) if label_obj["total_return"] else 0
  131. else:
  132. label = 0
  133. return label, label_dt
  134. def process_info(item_):
  135. """
  136. Insert data into MySql
  137. :param item_:
  138. """
  139. video_id, hour_dt = item_
  140. label_info = self.label_data
  141. if not label_info:
  142. print(label_info)
  143. title = read_title(client=self.client, video_id=video_id)
  144. label, dt_daily = generate_label(video_id, hour_dt, label_info)
  145. insert_sql = f"""UPDATE lightgbm_data
  146. set video_title = '{title}', label = '{label}', daily_dt_str = '{dt_daily}'
  147. where video_id = '{video_id}'
  148. ;"""
  149. self.client_spider.update(insert_sql)
  150. select_sql = "SELECT video_id, hour_dt_str FROM lightgbm_data where label = 0 and hour_dt_str < '20240327';"
  151. init_data_tuple = self.client_spider.select(select_sql)
  152. init_list = list(init_data_tuple)
  153. for item in init_list:
  154. # print(item)
  155. process_info(item)
  156. # with ThreadPoolExecutor(max_workers=10) as Pool:
  157. # Pool.map(process_info, init_list)
  158. class SpiderProcess(object):
  159. """
  160. Spider Data Process and Process data for lightgbm training
  161. """
  162. def __init__(self):
  163. self.client_spider = MySQLClientSpider()
  164. def spider_lop(self, video_id):
  165. """
  166. Spider lop = like / play
  167. :param video_id:
  168. :return:
  169. """
  170. sql = f"""SELECT like_cnt, play_cnt, duration from crawler_video where video_id = '{video_id}';"""
  171. try:
  172. like_cnt, play_cnt, duration = self.client_spider.select(sql)[0]
  173. lop = (like_cnt + 700) / (play_cnt + 18000)
  174. return lop, duration
  175. except Exception as e:
  176. print(video_id, "\t", e)
  177. return 0, 0
  178. def spider_data_produce(self):
  179. """
  180. 把 spider_duration 存储到数据库中
  181. :return:
  182. """
  183. return
  184. class UserProcess(object):
  185. """
  186. User Data Process
  187. """
  188. def __init__(self):
  189. self.client = MysqlClient()
  190. self.user_features = [
  191. "uid",
  192. "channel",
  193. "user_fans",
  194. "user_view_30",
  195. "user_share_30",
  196. "user_return_30",
  197. "user_rov",
  198. "user_str",
  199. "user_return_videos_30",
  200. "user_return_videos_3",
  201. "user_return_3",
  202. "user_view_3",
  203. "user_share_3",
  204. "address"
  205. ]
  206. def title_processor(self, video_id):
  207. """
  208. 通过 video_id 去获取title, 然后通过 title 再分词,把关键词作为 feature
  209. :param video_id: the video id
  210. :return: tag_list [tag, tag, tag, tag......]
  211. """
  212. sql = f"""SELECT title from wx_video where id = {video_id};"""
  213. try:
  214. title = self.client.select(sql)[0][0]
  215. keywords_textrank = jieba.analyse.textrank(title, topK=3)
  216. return list(keywords_textrank)
  217. except Exception as e:
  218. print(video_id, "\t", e)
  219. return []
  220. def user_data_process(self):
  221. """
  222. 把 user_return_3, user_view_3, user_share_3
  223. user_return_videos_3, user_return_videos_30
  224. address 存储到 mysql 数据库中
  225. :return:
  226. """
  227. user_path = '/data'
  228. if __name__ == "__main__":
  229. # D = DataProcessor()
  230. # D.producer()
  231. # parser = argparse.ArgumentParser() # 新建参数解释器对象
  232. # parser.add_argument("--mode")
  233. # parser.add_argument("--category")
  234. # parser.add_argument("--dtype", default="whole")
  235. # args = parser.parse_args()
  236. # mode = args.mode
  237. # category = args.category
  238. # dtype = args.dtype
  239. D = DataProcessor()
  240. D.producer()
  241. # if mode == "train":
  242. # print("Loading data and process for training.....")
  243. # D = DataProcessor(flag="train", ll=category)
  244. # D.producer("whole")
  245. # elif mode == "predict":
  246. # print("Loading data and process for prediction for each day......")
  247. # D = DataProcessor(flag="predict", ll=category)
  248. # if dtype == "single":
  249. # date_str = str(input("Please enter the date of the prediction"))
  250. # D.producer(date_str)
  251. # elif dtype == "days":
  252. # start_date_str = str(input("Please enter the start date of the prediction"))
  253. # end_date_str = str(input("Please enter the end date of the prediction"))
  254. # dt_list = generate_daily_strings(start_date=start_date_str, end_date=end_date_str)
  255. # for d in dt_list:
  256. # D.producer()