Analyze Sentiment with Natural Language API

Solution for Analyze Sentiment with Natural Language API. 1 lab: ARC130. Fast copy-paste commands for Google Cloud.

ARC130 — Analyze Sentiment with Natural Language API: Challenge Lab

Estimated time: 30 minutes

# Natural Language API Challenge Lab This Challenge Lab consists of **two parts**: - **Part 1:** Manual configuration using **Google Docs** (Tasks 1 & 2) - **Part 2:** Cloud Shell automation (Tasks 3 & 4) Follow the execution sequence below exactly as shown. ## 🚀 Part 1 — Manual UI Setup (Tasks 1 & 2) ### 🔹 Step 1 — Generate an API Key 1. Open the **Google Cloud Console**. 2. Navigate to: - **APIs & Services → Credentials** 3. Click: - **Create Credentials → API Key** 4. Copy the g

GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'
RESET='\e[0m'
BOLD='\e[1m'

clear
echo -e "${CYAN}${BOLD}"
echo "   ____       _     _ _            __   ___             "
echo "  / __ \     | |   (_) |          / _| / _ \            "
echo " | |  | |_ __| |__  _| |_   ___  | |_ | | | |_ __  ___  "
echo " | |  | | '__| '_ \| | __| / _ \ |  _|| | | | '_ \/ __| "
echo " | |__| | |  | |_) | | |_ | (_) || |  | |_| | |_) \__ \ "
echo "  \____/|_|  |_.__/|_|\__| \___/ |_|   \___/| .__/|___/ "
echo "                                            | |         "
echo "                                            |_|         "
echo -e "${RESET}"
echo -e "${MAGENTA}${BOLD}>>> ORBIT OF OPS: ARC130 TERMINAL AUTOMATION INITIALIZED <<<${RESET}\n"

export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
echo -e "${YELLOW}${BOLD}[*] Auto-fetching VM Zone...${RESET}"
export ZONE=$(gcloud compute instances list --filter="name=lab-vm" --format="value(zone)" | head -n 1)
export VM_NAME="lab-vm"

echo -e "${YELLOW}${BOLD}[*] Project ID: ${PROJECT_ID}${RESET}"
echo -e "${YELLOW}${BOLD}[*] Target VM : ${VM_NAME} (${ZONE})${RESET}\n"

echo -e "${CYAN}${BOLD}[*] Compiling Remote Execution Payload...${RESET}"

cat > remote_payload.sh <<'EOF_SCRIPT'
#!/bin/bash
echo "--> [Task 3] Creating analyze-request.json..."
cat > analyze-request.json <<EOF
{
  "document":{
    "type":"PLAIN_TEXT",
    "content": "Google, headquartered in Mountain View, unveiled the new Android phone at the Consumer Electronic Show.  Sundar Pichai said in his keynote that users love their new Android phones."
  },
  "encodingType": "UTF8"
}
EOF

echo "--> [Task 3] Analyzing English text syntax..."
curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://language.googleapis.com/v1/documents:analyzeSyntax" \
-d @analyze-request.json > analyze-response.txt

echo "--> [Task 4] Creating multi-nl-request.json..."
cat > multi-nl-request.json <<EOF
{
  "document":{
    "type":"PLAIN_TEXT",
    "content":"Le bureau japonais de Google est situé à Roppongi Hills, Tokyo."
  }
}
EOF

echo "--> [Task 4] Analyzing multilingual text entities..."
curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://language.googleapis.com/v1/documents:analyzeEntities" \
-d @multi-nl-request.json > multi-response.txt
EOF_SCRIPT

echo -e "${YELLOW}${BOLD}[*] Deploying and Executing Payload on ${VM_NAME}...${RESET}"
gcloud compute scp remote_payload.sh $VM_NAME:~ --zone=$ZONE --quiet
gcloud compute ssh $VM_NAME --zone=$ZONE --quiet --command="bash ~/remote_payload.sh"

rm remote_payload.sh

echo -e "\n${GREEN}${BOLD}>>> MISSION COMPLETE! Check all progress bars in Qwiklabs. <<<${RESET}"