process_data.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. process the data to satisfy the lightgbm
  3. """
  4. import sys
  5. import os
  6. import json
  7. from tqdm import tqdm
  8. import jieba.analyse
  9. sys.path.append(os.getcwd())
  10. from functions import generate_label_date, MysqlClient
  11. class DataProcessor(object):
  12. """
  13. Process the data to satisfy the lightGBM
  14. """
  15. def __init__(self, flag):
  16. self.client = MysqlClient()
  17. self.flag = flag
  18. def generate_train_label(self, item, y_ori_data, cate):
  19. """
  20. 生成训练数据,用 np.array矩阵的方式返回,
  21. :return: x_train, 训练数据, y_train, 训练 label
  22. """
  23. video_id = item["video_id"]
  24. dt = item["dt"]
  25. userful_features = [
  26. "uid",
  27. "type",
  28. "channel",
  29. "fans",
  30. "view_count_user_30days",
  31. "share_count_user_30days",
  32. "return_count_user_30days",
  33. "rov_user",
  34. "str_user",
  35. "out_user_id",
  36. "mode",
  37. "out_play_cnt",
  38. "out_like_cnt",
  39. "out_share_cnt",
  40. "out_collection_cnt",
  41. ]
  42. item_features = [item[i] for i in userful_features]
  43. keywords_textrank, keywords_tf = self.title_processor(video_id)
  44. if keywords_tf and keywords_textrank:
  45. item_features.append(",".join(keywords_textrank))
  46. item_features.append(",".join(keywords_tf))
  47. else:
  48. item_features.append(None)
  49. item_features.append(None)
  50. label_dt = generate_label_date(dt)
  51. label_obj = y_ori_data.get(label_dt, {}).get(video_id)
  52. if label_obj:
  53. label = int(label_obj[cate]) if label_obj[cate] else 0
  54. else:
  55. label = 0
  56. return label, item_features
  57. def title_processor(self, video_id):
  58. """
  59. 通过 video_id 去获取title, 然后通过 title 再分词,把关键词作为 feature
  60. :param video_id: the video id
  61. :return: tag_list [tag, tag, tag, tag......]
  62. """
  63. sql = f"""SELECT title from crawler_video where id = {video_id};"""
  64. try:
  65. title = self.client.select(sql)
  66. keywords_textrank = jieba.analyse.textrank(title, topK=3)
  67. keywords_tfidf = jieba.analyse.extract_tags(title, topK=3)
  68. return list(keywords_textrank), list(keywords_tfidf)
  69. except Exception as e:
  70. print(video_id, "\t", e)
  71. return [], []
  72. def producer(self):
  73. """
  74. 生成数据
  75. :return:none
  76. """
  77. if self.flag == "train":
  78. x_path = "data/hour_train.json"
  79. y_path = "data/daily-label-20240101-20240320.json"
  80. elif self.flag == "predict":
  81. x_path = "prid_data/train_0314_0317.json"
  82. y_path = "data/daily-label-20240315-20240321.json"
  83. else:
  84. return
  85. with open(x_path) as f:
  86. x_data = json.loads(f.read())
  87. with open(y_path) as f:
  88. y_data = json.loads(f.read())
  89. cate_list = ["total_return"]
  90. for c in cate_list:
  91. x_list = []
  92. y_list = []
  93. for video_obj in tqdm(x_data):
  94. our_label, features = self.generate_train_label(video_obj, y_data, c)
  95. x_list.append(features)
  96. y_list.append(our_label)
  97. with open("produce_data/x_data_{}_{}.json".format(c, self.flag), "w") as f1:
  98. f1.write(json.dumps(x_list, ensure_ascii=False))
  99. with open("produce_data/y_data_{}_{}.json".format(c, self.flag), "w") as f2:
  100. f2.write(json.dumps(y_list, ensure_ascii=False))
  101. if __name__ == "__main__":
  102. D = DataProcessor(flag="train")
  103. a, b = D.title_processor("19591529")
  104. print(a, b)