Преглед изворни кода

热度召回增加redis存储相关的信息

huanghongbo пре 1 година
родитељ
комит
866c4398b8
1 измењених фајлова са 33 додато и 4 уклоњено
  1. 33 4
      models/recall/hot_recall.py

+ 33 - 4
models/recall/hot_recall.py

@@ -8,7 +8,7 @@
 @Version     : 1.0
 '''
 import pandas as pd
-import redis
+from dao.redis_db import Redis
 import random
 import joblib
 random.seed(12345)
@@ -16,8 +16,23 @@ class HotRecallModel:
     """TODO 1. 将加载数据修改为数据库加载
             2. 将结果保存到redis数据库中"""
     def __init__(self):
-        pass
-    
+        self.redis_db = Redis()
+        hotkeys = self.get_hotkeys()
+        print(hotkeys)
+
+
+    def get_hotkeys(self):
+        info = self.redis_db.redis.zrange("hotkeys", 0, -1, withscores=True)
+        hotkey = tuple()
+        results = []
+        for item, score in info:
+            hotkey += (item, score)
+            results.append(hotkey)
+            print(f"元素: {item}, 分数: {score}")
+
+        return results
+
+
     def load_dataset(self, data_path):
         self._order_data = pd.read_excel(data_path)
         
@@ -49,7 +64,21 @@ class HotRecallModel:
             hot_datas.appends(self._calculate_hot_score(col))
             
         return hot_datas
+
+    def to_redis(self, city, hotkey_name, rec_content_score):
+        # rec_content_score的格式为:(零售户id,分数)
+        rec_item_id = "hot:" + city + ":" + str(hotkey_name)
+        res = dict()
+        for content, score in rec_content_score.items():
+            res[content] = score
+
+        if len(res) > 0:
+            data = dict({rec_item_id: res})
+            for item, value in data.items():
+                self.redis_db.redis.zadd(item, value)
+
+
 if __name__ == "__main__":
     # 序列化
     model = HotRecallModel()
-    joblib.dump(model, "hot_recall.model")
+    # joblib.dump(model, "hot_recall.model")