NLP with Spacy (Introduction)
Today I learned about NLP with Spacy. You might be wondering why I use Spacy instead of NLTK. NLTK gives you more flexibility regarding which algorithm you want to use and its functionalities.
With Spacy, you can’t choose which algorithm to work with since it only picks the most efficient algorithm. That means you don’t have the option to work with the algorithm of your preference. But for many common NLP tasks, Spacy works faster and more efficiently compared to NLTK.
In the table above you can see the capabilities of some libraries. But in this learning process, I only use the library that compatible with Python.
Before we start, please note I am using Google Colab to do my NLP experiment.
First, you have to install Spacy and download the language library by typing the command below. It might take a while to finish the installation process.
!pip install -U spacy
!python -m spacy download en_core_web_sm
After you finish the installation, you can load Spacy by using this code to import Spacy and the language library, so you can use the library into your code.
import spacy
nlp = spacy.load('en_core_web_sm')
Here’s an example to use Spacy
doc = nlp(u'My name is Adele. I have $9999 in my bank account. I use Union Bank. I have U.S.A bank account Eventhough I live in the U.K.')
First of all, you can create any kind of sentence you want using the English language or you just can copy the sentence above. After that let’s try to check the part of speech and the syntactic dependency relation from each of them by typing the following code
for token in doc:
print(token,token.pos_,token.dep_)
After you run that code, you can see the details of the part of speech and syntactic dependency relation from each word you type. I use pos_ and dep_ since it gives you more detail. Because if you only use pos and dep, the result will only show numbers and you have to look at the reference to understand the meaning behind the numbers.
you can explore more about Spacy by reading the documentation here https://spacy.io/usage/linguistic-features
Happy exploring!