Explorar el Código

优化停用词缓存的创建和加载

ChenYL hace 2 años
padre
commit
cddf1bc181
Se han modificado 1 ficheros con 13 adiciones y 5 borrados
  1. 13 5
      src/utils.py

+ 13 - 5
src/utils.py

@@ -6,8 +6,12 @@ import pickle
 # 停用词存放文件夹
 STOP_WORD_DIR = "./conf/stopwords"
 
-# 停用词模型 缓存
-STOP_WORD_CACHE = "../tmp/stop_word.pkl"
+# 临时文件路径
+TEMP_PATH = "../tmp"
+
+# 停用词模型
+STOP_WORD_CACHE = "stop_word.pkl"
+
 
 def save_obj(path, obj):
     """
@@ -29,10 +33,14 @@ def load_stop_word():
     """
     加载停用词
     """
+    # 判断临时文件路径是否存在,不存在则重新创建
+    if not os.path.exists(TEMP_PATH):
+        os.makedirs(TEMP_PATH)
 
     # 判断是否存在缓存
-    if os.path.exists(STOP_WORD_CACHE):
-        return load_obj(STOP_WORD_CACHE)
+    stop_word_cache_path = os.path.join(TEMP_PATH, STOP_WORD_CACHE)
+    if os.path.exists(stop_word_cache_path) and os.path.isfile(stop_word_cache_path):
+        return load_obj(stop_word_cache_path)
 
     # 停用词容器
     stop_word = set()
@@ -52,6 +60,6 @@ def load_stop_word():
         stop_word_dict[item] = None
 
     # 保存本地作为缓存
-    save_obj(STOP_WORD_CACHE, stop_word_dict)
+    save_obj(stop_word_cache_path, stop_word_dict)
 
     return stop_word_dict