无法加载spacy英文模型 - 'WindowsPath'对象没有属性'read'

时间:2017-10-16 07:39:59

标签: python nlp spacy

我使用pip安装了spacy,然后使用下载了英文模型 $ python -m spacy download en 下载后给了我留言

You can now load the model via spacy.load('en') 使用IPython,

import spacy nlp=spacy.load('en')

AttributeError                            Traceback (most recent call last)
<ipython-input-5-a32b6d2b36d8> in <module>()
----> 1 nlp=spacy.load('en')

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\__init__.pyc in load(n
ame, **overrides)
     13     from .deprecated import resolve_load_name
     14     name = resolve_load_name(name, **overrides)
---> 15     return util.load_model(name, **overrides)
     16
     17

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\util.pyc in load_model
(name, **overrides)
    102     if isinstance(name, basestring_):
    103         if name in set([d.name for d in data_path.iterdir()]): # in data
 dir / shortcut
--> 104             return load_model_from_link(name, **overrides)
    105         if is_package(name): # installed as package
    106             return load_model_from_package(name, **overrides)

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\util.pyc in load_model
_from_link(name, **overrides)
    121             "Cant' load '%s'. If you're using a shortcut link, make sure
 it "
    122             "points to a valid model package (not just a data directory)
." % name)
--> 123     return cls.load(**overrides)
    124
    125

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\data\en\__init__.pyc i
n load(**overrides)
     10
     11 def load(**overrides):
---> 12     return load_model_from_init_py(__file__, **overrides)

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\util.pyc in load_model
_from_init_py(init_file, **overrides)
    165     if not model_path.exists():
    166         raise ValueError("Can't find model directory: %s" % path2str(dat
a_path))
--> 167     return load_model_from_path(data_path, meta, **overrides)
    168
    169

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\util.pyc in load_model
_from_path(model_path, meta, **overrides)
    148             component = nlp.create_pipe(name, config=config)
    149             nlp.add_pipe(component, name=name)
--> 150     return nlp.from_disk(model_path)
    151
    152

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\language.pyc in from_d
isk(self, path, disable)
    571         if not (path / 'vocab').exists():
    572             exclude['vocab'] = True
--> 573         util.from_disk(path, deserializers, exclude)
    574         return self
    575

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\util.pyc in from_disk(
path, readers, exclude)
    495     for key, reader in readers.items():
    496         if key not in exclude:
--> 497             reader(path / key)
    498     return path
    499

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\language.pyc in <lambd
a>(p)
    558         path = util.ensure_path(path)
    559         deserializers = OrderedDict((
--> 560             ('vocab', lambda p: self.vocab.from_disk(p)),
    561             ('tokenizer', lambda p: self.tokenizer.from_disk(p, vocab=Fa
lse)),
    562             ('meta.json', lambda p: p.open('w').write(json_dumps(self.me
ta)))

vocab.pyx in spacy.vocab.Vocab.from_disk()

vectors.pyx in spacy.vectors.Vectors.from_disk()

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\spacy\util.pyc in from_disk(
path, readers, exclude)
    495     for key, reader in readers.items():
    496         if key not in exclude:
--> 497             reader(path / key)
    498     return path
    499

vectors.pyx in spacy.vectors.Vectors.from_disk.load_keys()

C:\Users\PARVATHY SARAT\Anaconda2\lib\site-packages\numpy\lib\npyio.pyc in load(
file, mmap_mode, allow_pickle, fix_imports, encoding)
    389         _ZIP_PREFIX = asbytes('PK\x03\x04')
    390         N = len(format.MAGIC_PREFIX)
--> 391         magic = fid.read(N)
    392         fid.seek(-N, 1)  # back-up
    393         if magic.startswith(_ZIP_PREFIX):

AttributeError: 'WindowsPath' object has no attribute 'read'

我将英文模型文件(en_core_web_sm)下载到工作目录,我错过了什么?我需要设置路径变量吗?非常感谢任何帮助,谢谢!

3 个答案:

答案 0 :(得分:2)

如果有其他人收到此错误:我在Github上与spaCy的开发人员打开了issue。我当时建议使用Python 3.6而不是2.7,因为没有替代解决方法。下一个spaCy版本应该涵盖这个错误修复(我被告知)。

答案 1 :(得分:1)

是的,在anaconda环境中存在涉及语言下载的故障。这是待处理的拉取请求 https://github.com/explosion/spaCy/pull/1792

答案 2 :(得分:0)

感谢Ines的快速解答。这可能是问题所在。我一直在寻找另一个方向。 msgpack-python 2小时前刚刚发布了0.5.5,因此我怀疑可能是问题所在。确实,当我安装msgpack-python 0.5.4时,加载模型效果很好。

pip install msgpack-python == 0.5.4

您可以考虑将msgpack-python版本锁定为0.5.4吗?当我检查需求时,您没有为msgpack-python指定版本。