我已经分别安装了Spacy和en_core_web_sm,并且正在尝试使用完整路径加载en_core_web_sm。
import spacy
import en_core_web_sm
nlp = spacy.load(r'C:\Anaconda3\Lib\site-packages\en_core_web_sm\en_core_web_sm-2.0.0')
doc = nlp("The big grey dog ate all of the chocolate, but fortunately he wasn't sick!")
这会导致以下错误:
sre_constants.error:位置257处的逃逸\ p错误。
完整的堆栈跟踪如下:
Traceback (most recent call last):
File "C:/Users/43976209/PycharmProjects/NLP_Exercises/spacy_trial.py", line 3, in <module>
nlp = spacy.load(r'C:\ProgramData\Anaconda3\Lib\site-packages\en_core_web_sm\en_core_web_sm-2.0.0')
File "C:\ProgramData\Anaconda3\lib\site-packages\spacy\__init__.py", line 30, in load
return util.load_model(name, **overrides)
File "C:\ProgramData\Anaconda3\lib\site-packages\spacy\util.py", line 166, in load_model
return load_model_from_path(Path(name), **overrides)
File "C:\ProgramData\Anaconda3\lib\site-packages\spacy\util.py", line 211, in load_model_from_path
return nlp.from_disk(model_path)
File "C:\ProgramData\Anaconda3\lib\site-packages\spacy\language.py", line 941, in from_disk
util.from_disk(path, deserializers, exclude)
File "C:\ProgramData\Anaconda3\lib\site-packages\spacy\util.py", line 654, in from_disk
reader(path / key)
File "C:\ProgramData\Anaconda3\lib\site-packages\spacy\language.py", line 928, in <lambda>
p, exclude=["vocab"]
File "tokenizer.pyx", line 526, in spacy.tokenizer.Tokenizer.from_disk
File "tokenizer.pyx", line 572, in spacy.tokenizer.Tokenizer.from_bytes
File "C:\ProgramData\Anaconda3\lib\re.py", line 233, in compile
return _compile(pattern, flags)
File "C:\ProgramData\Anaconda3\lib\re.py", line 301, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\ProgramData\Anaconda3\lib\sre_compile.py", line 562, in compile
p = sre_parse.parse(p, flags)
File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 856, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 415, in _parse_sub
itemsappend(_parse(source, state, verbose))
File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 526, in _parse
code1 = _class_escape(source, this)
File "C:\ProgramData\Anaconda3\lib\sre_parse.py", line 336, in _class_escape
raise source.error('bad escape %s' % escape, len(escape))
sre_constants.error: bad escape \p at position 257
我正在运行 Python 3.6.1 :: Anaconda 4.4.0(64位) 使用 Spacy版本2.2.3
这些是我的网络上可用的最新版本,我无法从PyPi下载任何新版本。
请告知。
谢谢, 谢兰德拉
答案 0 :(得分:0)
问题是您正在尝试使用spacy v2.2加载spacy v2.0模型,并且它们不兼容。您可以使用命令spacy validate
检查安装的型号是否与当前的spacy版本兼容。
使用spacy download en_core_web_sm
下载并安装兼容版本的模型。之后,spacy validate
的输出应包含带有绿色选中标记的如下一行:
package en-core-web-sm en_core_web_sm 2.2.5 ✔
您不需要提供site-packages
目录的完整路径,因为spacy会自动搜索已安装软件包中的模型,因此只需使用spacy.load("en_core_web_sm")
。