无法使用nlp.from_disk()加载NER管道

时间:2020-11-10 14:24:22

标签: python json python-3.x nlp spacy

我试图像这样将预训练的管道加载到我的代码中:

nlp = de_core_news_sm.load()
nlp = nlp.from_disk('./TRAINED/Background/')

但是我遇到了一个错误提示:

ValueError                                Traceback (most recent call last)
<ipython-input-4-1f41fefa6daa> in <module>
      1 nlp = de_core_news_sm.load()
----> 2 nlp = nlp.from_disk('./TRAINED/Background/')
      3 print(nlp)

/opt/anaconda3/lib/python3.8/site-packages/spacy/language.py in from_disk(self, path, exclude, disable)
    972             # Convert to list here in case exclude is (default) tuple
    973             exclude = list(exclude) + ["vocab"]
--> 974         util.from_disk(path, deserializers, exclude)
    975         self._path = path
    976         return self

/opt/anaconda3/lib/python3.8/site-packages/spacy/util.py in from_disk(path, readers, exclude)
    688         # Split to support file names like meta.json
    689         if key.split(".")[0] not in exclude:
--> 690             reader(path / key)
    691     return path
    692 

/opt/anaconda3/lib/python3.8/site-packages/spacy/language.py in deserialize_vocab(path)
    948         def deserialize_vocab(path):
    949             if path.exists():
--> 950                 self.vocab.from_disk(path)
    951             _fix_pretrained_vectors_name(self)
    952 

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

strings.pyx in spacy.strings.StringStore.from_disk()

/opt/anaconda3/lib/python3.8/site-packages/srsly/_json_api.py in read_json(location)
     48         data = sys.stdin.read()
     49         return ujson.loads(data)
---> 50     file_path = force_path(location)
     51     with file_path.open("r", encoding="utf8") as f:
     52         return ujson.load(f)

/opt/anaconda3/lib/python3.8/site-packages/srsly/util.py in force_path(location, require_exists)
     19         location = Path(location)
     20     if require_exists and not location.exists():
---> 21         raise ValueError("Can't read file: {}".format(location))
     22     return location
     23 

ValueError: Can't read file: TRAINED/Background/vocab/strings.json

如果我在macOS上打开Vocab文件夹,则没有string.json文件。只是几个exec文件。我该怎么做才能正确读取模型?

1 个答案:

答案 0 :(得分:2)

您需要具有如下所示的目录结构才能加载spacy模型。在您的情况下,目录中没有string.json文件会引发错误。 enter image description here