How to use function spaCy in Python? - with practical example
SpaCy is a powerful natural language processing library in Python that can be used for tasks such as text tokenization, part-of-speech tagging, named entity recognition, and more. In this video, I will show you how to use the spaCy function in Python with two different examples.
Example 1: Tokenization
Step 1: First, we need to install spaCy using pip install spacy.
Step 2: Next, we need to download a language model. For example, let's download the English language model with python -m spacy download en.
Step 3: Now, we can load the language model and create a spaCy object to tokenize text.
In this example, we tokenize the text "Hello, my name is John." using spaCy. We load the English language model, create a spaCy object, and then iterate through the tokens in the text to print them out. Example 2: Named Entity Recognition Step 1: We need to load a language model that supports named entity recognition. Let's use the English language model again. Step 2: We can then process a text that contains named entities to extract and classify them.
In this example, we use spaCy to perform named entity recognition on the text "Apple is a technology company headquartered in Cupertino, California.". We load the English language model, process the text, and then iterate through the entities in the text to print out the entity text and its label.
import spacy nlp = spacy.load("en") text = "Hello, my name is John." doc = nlp(text) for token in doc: print(token.text)
In this example, we tokenize the text "Hello, my name is John." using spaCy. We load the English language model, create a spaCy object, and then iterate through the tokens in the text to print them out. Example 2: Named Entity Recognition Step 1: We need to load a language model that supports named entity recognition. Let's use the English language model again. Step 2: We can then process a text that contains named entities to extract and classify them.
import spacy nlp = spacy.load("en") text = "Apple is a technology company headquartered in Cupertino, California." doc = nlp(text) for ent in doc.ents: print(ent.text, ent.label_)
In this example, we use spaCy to perform named entity recognition on the text "Apple is a technology company headquartered in Cupertino, California.". We load the English language model, process the text, and then iterate through the entities in the text to print out the entity text and its label.
Comments
Post a Comment