Spacy: OSError: [E050] Can't find model on Google Colab | Python

Spacy: OSError: [E050] Can't find model on Google Colab | Python

我正在尝试使用西班牙语核心模型 'lemmatize' 西班牙语文本 es_core_news_sm。但是,我收到 OSError.

下面的代码是在Google Colabs[=28 上使用SpaCy lemmatization 的示例=]:

import spacy
spacy.prefer_gpu()

nlp = spacy.load('es_core_news_sm')
text = 'yo canto, tú cantas, ella canta, nosotros cantamos, cantáis, cantan…'
doc = nlp(text)
lemmas = [tok.lemma_.lower() for tok in doc]

我也尝试过导入核心,但没有以这种方式工作,得到了类似的回溯。

import es_core_news_sm
nlp = es_core_news_sm.load()

回溯:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-93-fd65d69a4f87> in <module>()
      2 spacy.prefer_gpu()
      3 
----> 4 nlp = spacy.load('es_core_web_sm')
      5 text = 'yo canto, tú cantas, ella canta, nosotros cantamos, cantáis, cantan…'
      6 doc = nlp(text)

1 frames
/usr/local/lib/python3.6/dist-packages/spacy/util.py in load_model(name, **overrides)
    137     elif hasattr(name, "exists"):  # Path or Path-like to model data
    138         return load_model_from_path(name, **overrides)
--> 139     raise IOError(Errors.E050.format(name=name))
    140 
    141 

OSError: [E050] Can't find model 'es_core_web_sm'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.

您首先需要下载数据:

!spacy download es_core_news_sm

然后重新启动运行时间,之后您的代码将运行正确:

import spacy
spacy.prefer_gpu()

nlp = spacy.load('es_core_news_sm')
text = 'yo canto, tú cantas, ella canta, nosotros cantamos, cantáis, cantan…'
doc = nlp(text)
lemmas = [tok.lemma_.lower() for tok in doc]
print(len(lemmas))
16