Use pathlib for the Jieba default dict path

This commit is contained in:
Adam Turner 2024-10-19 20:49:48 +01:00
parent d773a43f59
commit 3dc73cbdf4

View File

@ -2,8 +2,8 @@
from __future__ import annotations
import os
import re
from pathlib import Path
import snowballstemmer
@ -13,8 +13,10 @@ try:
import jieba # type: ignore[import-not-found]
JIEBA = True
JIEBA_DEFAULT_DICT = Path(jieba.__file__).parent / jieba.DEFAULT_DICT_NAME
except ImportError:
JIEBA = False
JIEBA_DEFAULT_DICT = Path()
english_stopwords = set(
"""
@ -234,11 +236,8 @@ class SearchChinese(SearchLanguage):
def init(self, options: dict[str, str]) -> None:
if JIEBA:
default_dict_path = os.path.join(
os.path.dirname(jieba.__file__), jieba.DEFAULT_DICT_NAME
)
dict_path = options.get('dict', default_dict_path)
if dict_path and os.path.isfile(dict_path):
dict_path = options.get('dict', JIEBA_DEFAULT_DICT)
if dict_path and Path(dict_path).is_file():
jieba.load_userdict(dict_path)
self.stemmer = snowballstemmer.stemmer('english')