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
# ==============================================================================
# Color Variables & Branding
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'
RED='\e[1;31m'
RESET='\e[0m'
BOLD='\e[1m'
clear
echo -e "${CYAN}${BOLD}"
cat << "EOF"
____ _ _ _ __ ___
/ __ \ | | (_) | / _| / _ \
| | | |_ __| |__ _| |_ ___ | |_ | | | |_ __ ___
| | | | '__| '_ \| | __| / _ \ | _| | | | | '_ \/ __|
| |__| | | | |_) | | |_ | (_) || | | |_| | |_) \__ \
\____/|_| |_.__/|_|\__| \___/ |_| \___/| .__/|___/
| |
|_|
EOF
echo -e "${RESET}"
echo -e "${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP038 PART 1 INITIALIZED <<<${RESET}\n"
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Auto-fetching Project and Zone...${RESET}"
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
if [[ -z "$PROJECT_ID" ]]; then
export PROJECT_ID=$DEVSHELL_PROJECT_ID
fi
export ZONE=$(gcloud compute project-info describe \
--format="value(commonInstanceMetadata.items[google-compute-default-zone])" 2>/dev/null | tail -n 1)
if [[ -z "$ZONE" ]]; then
export ZONE=$(gcloud compute instances list --format="value(zone)" | head -n 1)
fi
gcloud config set compute/zone $ZONE 2>/dev/null
echo -e "✅ Project ID: ${GREEN}$PROJECT_ID${RESET}"
echo -e "✅ Zone: ${GREEN}$ZONE${RESET}\n"
echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 1: Enabling Services and Creating Restricted API Key...${RESET}"
gcloud services enable language.googleapis.com apikeys.googleapis.com || true
gcloud services api-keys create --display-name="NL_API_KEY" --api-target=service=language.googleapis.com || true
export KEY_NAME=$(gcloud services api-keys list --format="value(name)" --filter "displayName=NL_API_KEY" | head -n 1)
export API_KEY=$(gcloud services api-keys get-key-string $KEY_NAME --format="value(keyString)")
echo -e "✅ API Key successfully generated: ${GREEN}$API_KEY${RESET}\n"
echo -e "${GREEN}${BOLD}✅ Part 1 Complete. Check Task 1 progress in the lab window!${RESET}"echo -e "\n${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP038 PART 2 INITIALIZED <<<${RESET}\n"
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 2: Building request.json locally...${RESET}"
cat <<EOF > request.json
{
"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 "${CYAN}${BOLD}[Orbit of Ops] Task 3: Calling the Natural Language API...${RESET}"
curl "https://language.googleapis.com/v1/documents:analyzeEntities?key=${API_KEY}" \
-s -X POST -H "Content-Type: application/json" --data-binary @request.json > result.json
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Securely copying files to 'linux-instance' VM for grading...${RESET}"
# Using --quiet to bypass SSH key generation prompts
gcloud compute scp request.json result.json linux-instance:~/ --zone=$ZONE --quiet || true
echo -e "\n${GREEN}${BOLD}🎉 All blocks executed successfully! The files are on the VM. Please click 'Check my progress' on Tasks 2 & 3!${RESET}"