spaCy - Top-level Functions

Here, we will be discussing some of the top-level functions used in spaCy. The functions along with the descriptions are listed below −
spacy.load()
To load a model.
spacy.blank()
To create a blank model.
spacy.info()
To provide information about the installation, models and local setup from within spaCy.
spacy.explain()
To give a description.
spacy.prefer_gpu()
To allocate data and perform operations on GPU.
spacy.require_gpu()
To allocate data and perform operations on GPU.
As the name implies, this spacy function will load a model via following −
Its shortcut links.
Its shortcut links.
The name of the installed model package.
The name of the installed model package.
A Unicode paths.
A Unicode paths.
Path-like object.
Path-like object.
spaCy will try to resolve the load argument in the below given order −
If a model is loaded from a shortcut link or package name, spaCy will assume it as a Python package and call the model’s own load() method.
If a model is loaded from a shortcut link or package name, spaCy will assume it as a Python package and call the model’s own load() method.
On the other hand, if a model is loaded from a path, spacy will assume it is a data directory and hence initialize the Language class.
On the other hand, if a model is loaded from a path, spacy will assume it is a data directory and hence initialize the Language class.
Upon using this function, the data will be loaded in via Language.from_disk.
The table below explains its arguments −
In the below examples, spacy.load() function loads a model by using shortcut link, package, unicode path and a pathlib path −
Following is the command for spacy.load() function for loading a model by using the shortcut link −
nlp_model = spacy.load("en")

Following is the command for spacy.load() function for loading a model by using package −
nlp_model = spacy.load("en_core_web_sm")

Following is the command for spacy.load() function for loading a model by using the Unicode path −
nlp_model = spacy.load("/path/to/en")

Following is the command for spacy.load() function for loading a model by using the pathlib path −
nlp_model = spacy.load(Path("/path/to/en"))

Following is the command for spacy.load() function for loading a model with all the arguments −
nlp_model = spacy.load("en_core_web_sm", disable=["parser", "tagger"])

It is the twin of spacy.blank() function, creates a blank model of a given language class.
The table below explains its arguments −
Example
In the below examples, spacy.blank() function is used for creating a blank model of “en” language class.
nlp_model_en = spacy.blank("en")

Like info command, spacy.info() function provides information about the installation, models, and local setup from within spaCy.
If you want to get the model meta data as a dictionary, you can use the meta-attribute on your nlp object with a loaded model. For example, nlp.meta.
The table below explains its arguments −
Example
An example is given below −
spacy.info()
spacy.info("en")
spacy.info("de", markdown=True)
This function will give us a description for the following −
POS tag
POS tag
Dependency label
Dependency label
Entity type
Entity type
The table below explains its arguments −
Example
An example for the use of spacy.explain() function is mentioned below −
import spacy
import en_core_web_sm
nlp= en_core_web_sm.load()
spacy.explain("NORP")
doc = nlp("Hello TutorialsPoint")
for word in doc:
   print(word.text, word.tag_, spacy.explain(word.tag_))
Output
Hello UH interjection
TutorialsPoint NNP noun, proper singular

If you have GPU, this function will allocate data and perform operations on GPU. But the data and operations will not be moved to GPU, if they are already available on CPU. It will return a Boolean output whether the GPU was activated or not.
Example
An example for the use of spacy.prefer_gpu() is stated below −
import spacy
activated = spacy.prefer_gpu()
nlp = spacy.load("en_core_web_sm")
This function is introduced in version 2.0.14 and it will also allocate data and perform operations on GPU. It will raise an error if there is no GPU available. The data and operations will not be moved to GPU, if they are already available on CPU.
It is recommended that this function should be called right after importing spacy and before loading any of the model. It will also return a Boolean type output.
Example
An example for the use of spacy.require_gpu() function is as follows −
import spacy
spacy.require_gpu()
nlp = spacy.load("en_core_web_sm")
 Print
 Add Notes
 Bookmark this page