Entity and Sentiment Analysis with the Natural Language API
Solution for Entity and Sentiment Analysis with the Natural Language API. 1 lab: GSP038. Fast copy-paste commands for Google Cloud.
GSP038 — Entity and Sentiment Analysis with the Natural Language API
Estimated time: 15 minutes
# 🧠 Cloud Natural Language API: Qwik Start > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created to help you understand why each step works. Attempt the challenge yourself first and use this guide as a learning aid rather than as a shortcut to skip the underlying concepts. This guide is intended for educational purposes, hands-on practice, and Google Cloud certification preparation. It is not affiliated with, endorsed by, or officially supported by Google, Google Clou
clear
# Professional ANSI Color Variables
CYAN='\033[1;36m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
RESET='\033[0m'
BOLD='\033[1m'
echo -e "${CYAN}${BOLD}"
echo " ____ _ _ _ __ ___ "
echo " / __ \ | | (_) | / _| / _ \ "
echo " | | | |_ __| |__ _| |_ ___ | |_ | | | |_ __ ___ "
echo " | | | | '__| '_ \| | __| / _ \ | _|| | | | '_ \ / __| "
echo " | |__| | | | |_) | | |_ | (_) || | | |_| | |_) \__ \ "
echo " \____/|_| |_.__/|_|\__| \___/ |_| \___/| .__/|___/ "
echo " | | "
echo " |_| "
echo -e "${RESET}"
echo -e "${YELLOW}▶ Task 1: Enabling APIs and Creating API Key...${RESET}"
gcloud services enable apikeys.googleapis.com
gcloud services api-keys create --display-name="NLP-Key" --api-target=service=language.googleapis.com --quiet
echo -e "${CYAN}▶ Waiting 5 seconds for API key to register in the backend...${RESET}"
sleep 5
export KEY_NAME=$(gcloud services api-keys list --filter="displayName=NLP-Key" --format="value(name)" | head -n 1)
export API_KEY=$(gcloud services api-keys get-key-string $KEY_NAME --format="value(keyString)")
# Saving the key safely so Part 2 can access it
echo $API_KEY > ~/.nlp_api_key
echo -e "\n${GREEN}${BOLD}✓ API Key created successfully!${RESET}"
echo -e "${YELLOW}▶ Please go to the lab page and click 'Check my progress' for Task 1.${RESET}"
echo -e "${BLUE}▶ After you receive the points, run Part 2.${RESET}"clear
CYAN='\033[1;36m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
RESET='\033[0m'
BOLD='\033[1m'
echo -e "${CYAN}${BOLD}=== Natural Language API Analysis ===${RESET}"
# Retrieve API key from Part 1
if [ -z "$API_KEY" ]; then export API_KEY=$(cat ~/.nlp_api_key); fi
export PROJECT_ID=$(gcloud config get-value project)
echo -e "${BLUE}▶ Task 2: Building request.json payload...${RESET}"
cat > request.json <<EOF
{
"document":{
"type":"PLAIN_TEXT",
"content":"Joanne Rowling, who writes under the pen names J. K. Rowling and Robert Galbraith, is a British novelist and screenwriter who wrote the Harry Potter fantasy series."
},
"encodingType":"UTF8"
}
EOF
echo -e "${YELLOW}▶ Task 3: Querying the Natural Language API...${RESET}"
# Safety loop: Waits for 200 OK response in case the key needs a few more seconds to activate
while true; do
HTTP_STATUS=$(curl -s -o result.json -w "%{http_code}" -X POST -H "Content-Type: application/json" --data-binary @request.json "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}")
if [ "$HTTP_STATUS" == "200" ]; then
echo -e "\n${GREEN}✓ API Key is active! Analysis saved to result.json.${RESET}"
break
else
echo -ne "${CYAN} Key not ready yet (HTTP $HTTP_STATUS). Retrying in 10 seconds...\033[0K\r${RESET}"
sleep 10
fi
done
echo -e "${BLUE}▶ Task 3 (Continued): Uploading results to Cloud Storage...${RESET}"
# Create the required storage bucket before uploading
gcloud storage buckets create gs://$PROJECT_ID-nlp/ --location=us 2>/dev/null || true
gcloud storage cp result.json gs://$PROJECT_ID-nlp/
echo -e "\n${GREEN}${BOLD}✓ All tasks completed! You may now click 'Check my progress' on the lab page to claim your 100%.${RESET}"