Python Spacy Introduction

Coder Singh
2 min readDec 29, 2022

SpaCy is a popular library for natural language processing (NLP) in Python. It provides tools and libraries for tasks such as tokenization, part-of-speech tagging, dependency parsing, and entity recognition. In this blog, we will look at some of the features of SpaCy and provide some examples of how to use the library.

1.Tokenization: Tokenization is the process of breaking a piece of text into individual tokens, or words. In SpaCy, you can use the Doc.sents and Doc.ents properties to access the sentences and named entities in a document:

import spacy

nlp = spacy.load("en_core_web_sm")

doc = nlp("This is a sentence. This is another sentence.")

for sent in doc.sents:
print(sent)

for ent in doc.ents:
print(ent.text, ent.label_)

2. Part-of-speech tagging: Part-of-speech tagging is the process of identifying the grammatical role of each word in a sentence. In SpaCy, you can use the Doc.pos_ property to access the part-of-speech tags of the words in a document:

doc = nlp("This is a sentence. This is another sentence.")

for token in doc:
print(token.text, token.pos_)

3. Dependency parsing: Dependency parsing is the process of analyzing the grammatical structure of a sentence, identifying the relationships between the words, and labeling them according to their role in the sentence. In SpaCy, you can use the Doc.dep_ property to access the dependency labels of the words in a document:

doc = nlp("This is a sentence. This is another sentence.")

for token in doc:
print(token.text, token.dep_)

4. Entity recognition: Entity recognition is the process of identifying and classifying named entities in a document, such as people, organizations, and locations. In SpaCy, you can use the Doc.ents property to access the named entities in a document:

doc = nlp("Apple is a company based in Cupertino, California.")

for ent in doc.ents:
print(ent.text, ent.label_)

These are just a few examples of the features and capabilities of SpaCy. By using the tools and libraries provided by the library, you can perform a wide range of NLP tasks in Python.

I hope this tutorial has been helpful and gives you a good starting point for working with SpaCy in Python. Happy coding!

--

--