spaCy - Doc._ _init_ _ Method



This is one of the most useful methods of Doc class. As the name implies, it is used to construct a Doc object.

Arguments

The table below explains its arguments −

NAME TYPE DESCRIPTION
vocab vocab This argument represents a storage container for the lexical types.
words iterable It represents the list of strings which needs to be added to the container.
spaces iterable It is the list of Boolean values which indicates whether each word has a subsequent space or not. If you will specify it, you need to keep its length same as words. The default value will be true.

Example 1

An example of Doc._ _init_ _ method for the construction via nlp object is as follows −

import spacy
nlp_model = spacy.load("en_core_web_sm")
doc = nlp_model("This is Tutorialspoint.com.")
doc

Output

When you run the code, you will see the following output −

This is Tutorialspoint.com.

Example 2

An example of Doc._ _init_ _ method for the construction via DOC class is as follows −

import spacy
from spacy.tokens import Doc
words = ["This is Tutorialspoint.com."]
doc = Doc(nlp_model.vocab, words=words)
doc

Output

When you run the code, you will see the following output −

This is Tutorialspoint.com.
spacy_containers.htm
Advertisements