Spacy is a open source python tool widely used for Advanced Natural Language Processing. It comes with a lot of pretrained deep learning models for tagging, text classification, named entity recogntition and many other tasks. We can install spacy using pip, conda or can build from source. We can also use CPU or GPU for training and inference of pipelines.

Installation

Using PIP

To install spacy using pip, we need to open command promot and run pip install command.

pip install spacy

And for GPU, it requires a little modification, you need to setup cuda and cudnn for that.

pip install -U spacy[cuda102]

Using Conda

To install using conda, we can use conda commands, you must have conda installed on your system.

conda install -c conda-forge spacy

Once we have installed spacy, now we can download pre-trained model. Pretrained model download may require administration access. For example, we can download a simple model using this command.

python -m spacy download en_core_web_sm

There are a lot of different pretrained models available from spacy for different languages and with different parameters. We can use them by downloading from their repository. Get to this url to view list of all available models from spacy.

https://github.com/explosion/spacy-models/releases

After model download we, can test this model

Using Model

To test downloaded model, we can load model in python script and perform a simple NLP query for a sentense.

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Great! Ali and his friend are the only brilliant boys with highest score in class")

Now we can iterate to view all tokens and their properties.

for token in doc:
    print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_, token.shape_, token.is_alpha, token.is_stop)
TEXT LEMMA POS TAG DEP SHAPE IS_ALPHA IS_STOP
Great great ADJ JJ ROOT Xxxxx True False
! ! PUNCT . punct ! False False
Ali Ali PROPN NNP nsubj Xxx True False
and and CCONJ CC cc xxx True False
his -PRON- DET PRP$ poss xxx True True
friend friend NOUN NN conj xxxxx True False
are be AUX VBP ROOT xxx True True
the the DET DT det xxx True True
only only ADJ JJ amod xxxx True True
brilliant brilliant ADJ JJ amod xxxx True False
boys boy NOUN NNS attr xxxx True False
with with ADP IN prep xxxx True True
highest high ADJ JJS amod xxxx True False
score score NOUN NN pobj xxxx True False
in in ADP IN prep xx True True
class class NOUN NN pobj xxxxx True False