arrow_back

Classify Text into Categories with the Natural Language API

Join Sign in

Classify Text into Categories with the Natural Language API

1 hour 5 Credits

GSP063

Google Cloud selp-paced labs logo

Overview

The Cloud Natural Language API lets you extract entities from text, perform sentiment and syntactic analysis, and classify text into categories. In this lab, the focus is on text classification. Using a database of 700+ categories, this API feature makes it easy to classify a large dataset of text.

What you'll learn

  • Creating a Natural Language API request and calling the API with curl

  • Using the NL API's text classification feature

  • Using text classification to understand a dataset of news articles

What you'll need

Setup and Requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

  2. Click Continue.

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:

gcloud auth list

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:

gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Confirm that the Cloud Natural Language API is enabled

Click the Navigation menu icon in the top left of the screen.

742dc285f86cdd1f.png

Select APIs & Services > Enabled APIs and Services.

7f3779282bb1a7d6.png

Click Enable APIs and Services.

ff3e9c20f016db94.png

Then, search for language in the search box. Click Cloud Natural Language API:

d9c1c3eb2f87ab22.png

If the API is not enabled, you'll see the Enable button. Click Enable to enable the Cloud Natural Language API:

3da7b549ac00abc8.png

When the API is enabled, Google Cloud displays API information as follows:

212b673b48d92677.png

Create an API Key

Since you're using curl to send a request to the Natural Language API, you need to generate an API key to pass in the request URL.

To create an API key, in your Console, click Navigation menu > APIs & Services > Credentials:

9da406721fbdafc8.png

Then click Create credentials:

168581e4ae32f076.png

In the drop down menu, select API key:

bc4940935c1bef7f.png

Next, copy the key you just generated. Then click Close.

Click Check my progress to verify the objective.

Create an API Key

Now that you have an API key, you will save it as an environment variable to avoid having to insert the value of your API key in each request.

In order to perform next steps please connect to the instance provisioned for you via ssh. Open the Navigation menu and select Compute Engine > VM Instances. You should see the following provisioned linux instance:

provisioned-linux-instance.png

Click on the SSH button. You will be brought to an interactive shell. In the command line, enter in the following, replacing <YOUR_API_KEY> with the key you just copied:

export API_KEY=<YOUR_API_KEY>

Classify a news article

Using the Natural Language API's classifyText method, you can sort text data into categories with a single API call. This method returns a list of content categories that apply to a text document. These categories range in specificity, from broad categories like /Computers & Electronics to highly specific categories such as /Computers & Electronics/Programming/Java (Programming Language). A full list of 700+ possible categories can be found here.

You'll start by classifying a single article, and then see how you can use this method to make sense of a large news corpus. To start, take this headline and description from a New York Times article in the food section:

A Smoky Lobster Salad With a Tapa Twist. This spin on the Spanish pulpo a la gallega skips the octopus, but keeps the sea salt, olive oil, pimentón and boiled potatoes.

Create a request.json file with the code below. You can create the file using one of your preferred command line editors (nano, vim, emacs).

Create a new file named request.json and add the following:

{ "document":{ "type":"PLAIN_TEXT", "content":"A Smoky Lobster Salad With a Tapa Twist. This spin on the Spanish pulpo a la gallega skips the octopus, but keeps the sea salt, olive oil, pimentón and boiled potatoes." } } Create a request to Classify a news article

Now you can send this text to the Natural Language API's classifyText method with the following curl command:

curl "https://language.googleapis.com/v1/documents:classifyText?key=${API_KEY}" \ -s -X POST -H "Content-Type: application/json" --data-binary @request.json

Look at the response:

{ categories: [ { name: '/Food & Drink/Cooking & Recipes', confidence: 0.85 }, { name: '/Food & Drink/Food/Meat & Seafood', confidence: 0.63 } ] }

You created an Speech API request then called the Speech API. Run the following command to save the response in a result.json file:

curl "https://language.googleapis.com/v1/documents:classifyText?key=${API_KEY}" \ -s -X POST -H "Content-Type: application/json" --data-binary @request.json > result.json Check the Entity Analysis response

The API returned 2 categories for this text:

  • /Food & Drink/Cooking & Recipes
  • /Food & Drink/Food/Meat & Seafood

The text doesn't explicitly mention that this is a recipe or even that it includes seafood, but the API is able to categorize it. Classifying a single article is cool, but to really see the power of this feature, classify lots of text data.

Classifying a large text dataset

To see how the classifyText method can help you understand a dataset with lots of text, use this public dataset of BBC news articles. The dataset consists of 2,225 articles in five topic areas (business, entertainment, politics, sports, tech) from 2004 - 2005. A subset of these articles are in a public Cloud Storage bucket. Each of the articles is in a .txt file.

To examine the data and send it to the Natural Language API, you'll write a Python script to read each text file from Cloud Storage, send it to the classifyText endpoint, and store the results in a BigQuery table. BigQuery is Google Cloud's big data warehouse tool - it lets you easily store and analyze large data sets.

To see the type of text you'll be working with, run the following command to view one article (gsutil provides a command line interface for Cloud Storage):

gsutil cat gs://spls/gsp063/bbc_dataset/entertainment/001.txt

Next you'll create a BigQuery table for your data.

Creating a BigQuery table for categorized text data

Before sending the text to the Natural Language API, you need a place to store the text and category for each article.

Navigate to Navigation menu > BigQuery in the Console.

1525c20a6e84b8e4.png

Click Done.

To create a dataset, click on the View actions icon next to your project ID and select Create dataset:

create_dataset.png

Name the dataset news_classification_dataset, then click Create dataset.

To create a table, click on the View actions icon next to the news_classification_dataset and select Create Table.

create_table11.png

Use the following settings for the new table:

  • Create table from: Empty table
  • Name your table: article_data
  • Click Add Field and add the following 3 fields: article_text, category, and confidence.

table_nw.png

Click Create Table.

The table is empty right now. In the next step you'll read the articles from Cloud Storage, send them to the Natural Language API for classification, and store the result in BigQuery.

Click Check my progress to verify the objective.

Create a new Dataset and table for categorized text data

Classifying news data and storing the result in BigQuery

In order to perform next steps please connect to the Cloud Shell.

Before writing a script to send the news data to the Natural Language API, you need to create a service account. This will be used to authenticate to the Natural Language API and BigQuery from a Python script.

Export the name of your Cloud project as an environment variable. Replace <your_project_name> with the Project ID found in the CONNECTION DETAILS section of the lab:

export PROJECT=<your_project_name>

Run the following commands to create a service account:

gcloud iam service-accounts create my-account --display-name my-account gcloud projects add-iam-policy-binding $PROJECT --member=serviceAccount:my-account@$PROJECT.iam.gserviceaccount.com --role=roles/bigquery.admin gcloud iam service-accounts keys create key.json --iam-account=my-account@$PROJECT.iam.gserviceaccount.com export GOOGLE_APPLICATION_CREDENTIALS=key.json

Now you're ready to send the text data to the Natural Language API!

Write a Python script using the Python module for Google Cloud. You can accomplish the same thing from any language, there are many different cloud client libraries.

Create a file called classify-text.py and copy the following into it. You can either create the file using one of your preferred command line editors (nano, vim, emacs). Replace YOUR_PROJECT with your Project ID:

from google.cloud import storage, language, bigquery # Set up our GCS, NL, and BigQuery clients storage_client = storage.Client() nl_client = language.LanguageServiceClient() # TODO: replace YOUR_PROJECT with your project name below bq_client = bigquery.Client(project='YOUR_PROJECT') dataset_ref = bq_client.dataset('news_classification_dataset') dataset = bigquery.Dataset(dataset_ref) table_ref = dataset.table('article_data') table = bq_client.get_table(table_ref) # Send article text to the NL API's classifyText method def classify_text(article): response = nl_client.classify_text( document=language.Document( content=article, type_=language.Document.Type.PLAIN_TEXT ) ) return response rows_for_bq = [] files = storage_client.bucket('qwiklabs-test-bucket-gsp063').list_blobs() print("Got article files from GCS, sending them to the NL API (this will take ~2 minutes)...") # Send files to the NL API and save the result to send to BigQuery for file in files: if file.name.endswith('txt'): article_text = file.download_as_bytes() nl_response = classify_text(article_text) if len(nl_response.categories) > 0: rows_for_bq.append((str(article_text), nl_response.categories[0].name, nl_response.categories[0].confidence)) print("Writing NL API article data to BigQuery...") # Write article text + category data to BQ errors = bq_client.insert_rows(table, rows_for_bq) assert errors == []

Now you're ready to start classifying articles and importing them to BigQuery.

Run the following script:

python3 classify-text.py

The script takes about two minutes to complete, so while it's running read about what's happening.

You're using the Google Cloud Python client library to access Cloud Storage, the Natural Language API, and BigQuery. First, a client is created for each service, then references are created to the BigQuery table. files is a reference to each of the BBC dataset files in the public bucket. The files are looked at, the articles are downloaded as strings, then each one is sent to the Natural Language API in the classify_text function. For all articles where the Natural Language API returns a category, the article and its category data are saved to a rows_for_bq list. When classifying each article is done, the data is inserted into BigQuery using insert_rows().

When the script has finished running, it's time to verify that the article data was saved to BigQuery.

In BigQuery, navigate to the article_data table in the Explorer tab and click Query > In new tab to query the table:

Query_Table.png

Edit the results in the Unsaved query box, adding an asterisk between SELECT and FROM:

SELECT * FROM `YOUR_PROJECT.news_classification_dataset.article_data`

Now click Run.

You will see your data when the query completes. Scroll to the right to see the category column.

The category column has the name of the first category the Natural Language API returned for the article, and _confidence_ is a value between 0 and 1 indicating how confident the API is that it categorized the article correctly. You'll learn how to perform more complex queries on the data in the next step.

Analyzing categorized news data in BigQuery

First, see which categories were most common in the dataset.

In the BigQuery console, click + Compose New Query.

Enter the following query, replacing YOUR_PROJECT with your project name:

SELECT category, COUNT(*) c FROM `YOUR_PROJECT.news_classification_dataset.article_data` GROUP BY category ORDER BY c DESC

Now click Run.

You should see something like this in the query results:

BQ_query_results.png

If you wanted to find the article returned for a more obscure category like /Arts & Entertainment/Music & Audio/Classical Music, you could write the following query:

SELECT * FROM `YOUR_PROJECT.news_classification_dataset.article_data` WHERE category = "/Arts & Entertainment/Music & Audio/Classical Music"

Or, you could get only the articles where the Natural language API returned a confidence score greater than 90%:

SELECT article_text, category FROM `YOUR_PROJECT.news_classification_dataset.article_data` WHERE cast(confidence as float64) > 0.9

To perform more queries on your data, explore the BigQuery documentation. BigQuery also integrates with a number of visualization tools. To create visualizations of your categorized news data, check out the Data Studio quickstart for BigQuery.

Congratulations!

You've learned how to use the Natural Language API text classification method to classify news articles. You started by classifying one article, and then learned how to classify and analyze a large news dataset using the NL API with BigQuery.

What was covered

  • Creating a Natural Language API classifyText request and calling the API with curl
  • Using the Google Cloud Python module to analyze a large news dataset
  • Importing and analyzing data in BigQuery

38616f8aa634e047.png 304e2d6238466ca2.png Language_Speech_Text_Translation_GCAPIs_125.png

Finish your quest

This self-paced lab is part of the Qwiklabs Machine Learning APIs, Data Engineering, and Language, Speech, Text, & Translation with Google Cloud APIs Quests. A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in a Quest and get immediate completion credit if you've taken this lab. See other available Qwiklabs Quests.

Take your next lab

Continue your Quest with Predict Visitor Purchases with a Classification Model in BQML or try one of these:

Next Steps / Learn More

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated August 02, 2022
Lab Last Tested August 02, 2022

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.