Install spaCy 安装spaCy步骤

Install spaCy 快速安装spaCy步骤

  • Installation instructions
  • download a language model
  • Models
  • Run tests
  • Troubleshooting guide

附上spaCy官方网站:https://spacy.io/usage#quickstart

Installation instructions

本文默认你已配置好conda环境,python,Spyder等等。
下面开始安装…

推荐conda环境下,打开anaconda prompt, install spaCy via conda-forge:

// An highlighted block
 conda install -c conda-forge spacy

download a language model

After installation we need to download a language model.

Models

spaCy v2.0 features new neural models for tagging, parsing and entity recognition.

spaCy’s models can be installed as Python packages, just like any other module.
Models can be installed from a download URL or a local directory, manually or via pip. Their data can be located anywhere on your file system.

试过了下载本地,由于windows系统的环境变量很恶心,跳了几次总出错,推荐使用download URL方法下载

anaconda prompt输入:

// An highlighted block
 # With external URL
pip install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.1.0/en_core_web_sm-2.1.0.tar.gz

install成功:

// An highlighted block
 (base) C:\Users\nan>python -m spacy download en_core_web_sm
Collecting en_core_web_sm==2.0.0 from https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.0.0/en_core_web_sm-2.0.0.tar.gz#egg=en_core_web_sm==2.0.0
  Downloading https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-2.0.0/en_core_web_sm-2.0.0.tar.gz (37.4MB)
    100% |████████████████████████████████| 37.4MB 4.2MB/s
Installing collected packages: en-core-web-sm
  Running setup.py install for en-core-web-sm ... done
Successfully installed en-core-web-sm-2.0.0
你没有足够的权限执行此操作。

    Linking successful
    D:\Install_things\Anaconda3\lib\site-packages\en_core_web_sm -->
    D:\Install_things\Anaconda3\lib\site-packages\spacy\data\en_core_web_sm

    You can now load the model via spacy.load('en_core_web_sm')

可用此方法下载其他model,例如,下载en_vectors_web_lg
anaconda prompt输入:

python -m spacy download en_vectors_web_lg

成功结果:

(base) C:\Users\nan>python -m spacy download en_vectors_web_lg
Collecting en_vectors_web_lg==2.0.0 from https://github.com/explosion/spacy-models/releases/download/en_vectors_web_lg-2.0.0/en_vectors_web_lg-2.0.0.tar.gz#egg=en_vectors_web_lg==2.0.0
  Downloading https://github.com/explosion/spacy-models/releases/download/en_vectors_web_lg-2.0.0/en_vectors_web_lg-2.0.0.tar.gz (661.8MB)
    100% |████████████████████████████████| 661.8MB 2.8MB/s
Installing collected packages: en-vectors-web-lg
  Running setup.py install for en-vectors-web-lg ... done
Successfully installed en-vectors-web-lg-2.0.0

    Error: Couldn't link model to 'en_vectors_web_lg'
    Creating a symlink in spacy/data failed. Make sure you have the required
    permissions and try re-running the command as admin, or use a
    virtualenv. You can still import the model as a module and call its
    load() method, or create the symlink manually.

    D:\Install_things\Anaconda3\lib\site-packages\en_vectors_web_lg -->
    D:\Install_things\Anaconda3\lib\site-packages\spacy\data\en_vectors_web_lg


    Creating a shortcut link for 'en' didn't work (maybe you don't have
    admin permissions?), but you can still load the model via its full
    package name: nlp = spacy.load('{name}')
    Download successful but linking failed

检查目前下载的model

python -m spacy validate

正确显示:

Installed models (spaCy v2.0.16)
    D:\Install_things\Anaconda3\lib\site-packages\spacy

    TYPE        NAME                  MODEL                 VERSION
    package     en-vectors-web-lg     en_vectors_web_lg     2.0.0
    package     en-core-web-sm        en_core_web_sm        2.0.0
    package     de-core-news-sm       de_core_news_sm       2.0.0

OK

当然后续也可以使用pip进行安装。

Run tests

简单测试一下是否运行正常。

Spyder:

// An highlighted block
 import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(u"This is a sentence.")
print([(w.text, w.pos_) for w in doc])

结果:

// An highlighted block
[('This', 'DET'), ('is', 'VERB'), ('a', 'DET'), ('sentence', 'NOUN'), ('.', 'PUNCT')]

OK,安装成功,可以运行。

Troubleshooting guide

安装部分遇到了很多的问题,也重新安装了anaconda。
教训惨重,注意:以后更新anaconda 不要用pip,一定要用conda!用conda!!

下面说一下,我遇到的问题:

Spyder出现错误ModuleNotFoundError: No module named ‘spacy.symbols’:

// An highlighted block
File "E:\code\python\spaCy-master\spacy\util.py", line 25, in <module>
    from .symbols import ORTH

ModuleNotFoundError: No module named 'spacy.symbols'

解决办法:

情况说明:1.因为我是用pip 下载到anaconda系统路径下面了。
2.我又额外在GitHub上下载了spacy包,并在包内开始测试。

这样会造成优先路径错误。
删除额外下载的spacy包,就可以了

OK

你可能感兴趣的:(NLP)