最新在使用spacy调用日语的模型做一些工作,正常情况直接执行下述代码就可以
python -m spacy download ja_core_news_sm
但是一般会出现下载超时的坑,这是因为除了下载模型,还会下载日语字典之类的文件,如果当前的环境不支持翻墙,则会下载很慢导致报错。
解决问题方法有两种:
1、如果有梯子,则配置全局梯子,这样应该是可以
2、如果是服务器上面梯子不好配置,或者没有梯子的情况,就需要先安装sudachi-core包
直接pip安装sudachi-core仍然会报错的,所以我们需要把源码包下载下来手动安装
使用如下下载命令下载SudachiDict-core
wget "https://pypi.tuna.tsinghua.edu.cn/packages/73/0d/d82b31652781c9ae33ec81871fcc469de5147a7964c94f8caa2bb824d111/SudachiDict-core-20210802.tar.gz" --no-check-certificate
然后下载对应版本的字典(http://sudachi.s3-website-ap-northeast-1.amazonaws.com/sudachidict/sudachi-dictionary-20210802-core.zip)
之后就可以解压SudachiDict-core的代码,根据下面的代码,将字典解压放到相应的目录
ZIP_URL = "http://sudachi.s3-website-ap-northeast-1.amazonaws.com/sudachidict/"\
"sudachi-dictionary-{}-{}.zip".format(DICT_VERSION, DICT_EDITION)
ZIP_NAME = urlparse(ZIP_URL).path.split("/")[-1]
UNZIP_NAME = "sudachi-dictionary-{}".format(DICT_VERSION)
PKG_DIR = "sudachidict_{}".format(DICT_EDITION)
RESOURCE_DIR = os.path.join(PKG_DIR, "resources")
BINARY_NAME = "system_{}.dic".format(DICT_EDITION)
logger = getLogger(__name__)
# Download and place the dictionary file
if not os.path.exists(RESOURCE_DIR):
logger.warning("Downloading the Sudachi dictionary (It may take a while) ...")
_, _msg = urlretrieve(ZIP_URL, ZIP_NAME)
with ZipFile(ZIP_NAME) as z:
z.extractall()
os.rename(UNZIP_NAME, RESOURCE_DIR)
os.rename(os.path.join(RESOURCE_DIR, BINARY_NAME),
os.path.join(RESOURCE_DIR, "system.dic"))
os.remove(ZIP_NAME)
logger.warning("... downloaded and placed the dictionary at `{}`.".format(RESOURCE_DIR))
然后,重新压缩SudachiDict-core为SudachiDict-core-20210802.tar.gz
然后使用pip install SudachiDict-core-20210802.tar.gz 就可以安装完成SudanchiDict-core
为了加速ja的模型包安装,也可以先下载预先下载好模型包,然后直接使用pip install ja_core_news_sm-3.1.0.tar.gz,就可以顺利完成了。