Exercise: spaCy and Transformers

Let's test what you've learned in this chapter through a couple coding exercises.

Exercise 1: Using transformers to perform sentiment analysis

Write a Python program that uses spaCy and transformers to perform sentiment analysis on a given sentence.

Assume the sentence is: “I really enjoy using this product. It's amazing!”

Exercise 2: Using transformers to perform question answering

Write a Python program that uses spaCy and transformers to answer a question from a given paragraph. Assume that you are provided with the following paragraph:

“New York City, often simply called New York, is the most populous city in the United States. With an estimated 2020 population of 8,253,213 distributed over about 302.6 square miles (784 km2), New York City is also the most densely populated major city in the United States. Located at the southern tip of the State of New York, the city is the center of the New York metropolitan area, the largest metropolitan area in the world by urban landmass. With almost 20 million people in its metropolitan statistical area and approximately 23 million in its combined statistical area, it is one of the world's most populous megacities. New York City has been described as the cultural, financial, and media capital of the world, significantly influencing commerce, entertainment, research, technology, education, politics, tourism, art, fashion, and sports."

Ask the following questions:

  • "What is the population of New York City?”

  • “What is the area of New York City?”

Both these problems can be attempted in the Jupyter notebook below:

Get hands-on with 1200+ tech skills courses.