1、安装synonyms库
pip install synonyms
2、测试代码
import synonyms
synlst = synonyms.display('良师益友')
3、报错如下:
Synonyms: v3.18.0, Project home: https://github.com/chatopera/Synonyms/
Project Sponsored by Chatopera
deliver your chatbots with Chatopera Cloud Services --> https://bot.chatopera.com
>> Synonyms load wordseg dict [D:\Python\lib\site-packages\synonyms\data\vocab.txt] ...
Building prefix dict from D:\Python\lib\site-packages\synonyms\data\vocab.txt ...
Dumping model to file cache C:\Users\XIAOXI~1\AppData\Local\Temp\jieba.ue86a6605059b943c699e102c38068450.cache
Loading model cost 2.820 seconds.
Prefix dict has been built successfully.
>> Synonyms on loading stopwords [D:\Python\lib\site-packages\synonyms\data\stopwords.txt] ...
>> Synonyms on loading vectors [D:\Python\lib\site-packages\synonyms\data\words.vector.gz] ...
>> Synonyms downloading data from https://gitee.com/chatopera/cskefu/attach\\_files/610602/download/words.vector.gz to D:\Python\lib\site-packages\synonyms\data\words.vector.gz ...
this only happens if SYNONYMS_WORD2VEC_BIN_URL_ZH_CN is not present and Synonyms initialization for the first time.
It would take minutes that depends on network.
Traceback (most recent call last):
File "D:\pycharm\Claude\test.py", line 1, in <module>
import synonyms
File "D:\Python\lib\site-packages\synonyms\__init__.py", line 14, in <module>
from .synonyms import *
File "D:\Python\lib\site-packages\synonyms\synonyms.py", line 164, in <module>
vectors = load_w2v(model_file=_f_model)
File "D:\Python\lib\site-packages\synonyms\synonyms.py", line 155, in _load_w2v
wget.download(_f_url, out = model_file)
File "D:\Python\lib\site-packages\synonyms\wget.py", line 313, in download
(tmpfile, headers) = ThrowOnErrorOpener().retrieve(url, tmpfile, callback)
File "D:\Python\lib\urllib\request.py", line 1822, in retrieve
fp = self.open(url, data)
File "D:\Python\lib\urllib\request.py", line 1788, in open
return getattr(self, name)(url)
File "D:\Python\lib\urllib\request.py", line 1997, in open_https
return self._open_generic_http(self._https_connection, url, data)
File "D:\Python\lib\urllib\request.py", line 1960, in _open_generic_http
return self.http_error(
File "D:\Python\lib\urllib\request.py", line 1982, in http_error
return self.http_error_default(url, fp, errcode, errmsg, headers)
File "D:\Python\lib\site-packages\synonyms\wget.py", line 284, in http_error_default
raise Exception("%s: %s" % (errcode, errmsg))
Exception: 403: Forbidden
4、缺少文件
这样说明synonyms
正在导入词库,它可能是从指定的网站下载词库,如果网站连不上,就会报错。
我的解决方法是
从下面的网站下载词库word.vector.gz
链接:https://pan.baidu.com/s/1hkqpBVWXZfimzmy2s9y8zA?pwd=ppjo
提取码:ppjo
5、复制下载好的压缩包,拷贝到下面的路径
------\lib\site-packages\synonyms\data\words.vector.gz
找下自己Python的安装路径,把上面的文件复制到这里面就可以了