Visualizing parts of speech
As you saw in the Visualizing the dependency parse recipe, parts of speech are included in the dependency parse, so in order to see parts of speech for each word in a sentence, it is enough to do that. In this recipe, we will visualize part of speech counts. We will visualize the counts of past and present tense verbs in the book The Adventures of Sherlock Holmes.
Getting ready
We will use the spacy package for text analysis and the matplotlib package to create the graph. If you don't have matplotlib installed, install it using the following command:
pip install matplotlib
How to do it…
We will create a function that will count the number of verbs by tense and plot each on a bar graph:
- Import the necessary packages:
import spacy import matplotlib.pyplot as plt from Chapter01.dividing_into_sentences import read_text_file
- Load the
spacyengine and define the past and present tag sets:nlp = spacy.load("en_core_web_sm...