{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Amazon Comprehend Sentiment Analysis \n", "\n", "Amazon Comprehend can be used to perform sentiment analysis. You can accurately analyze customer interactions, including social media posts, reviews, customer interaction transcripts to improve your products and services.\n", "\n", "You can use Amazon Comprehend to determine the sentiment of a document. You can determine if the sentiment is positive, negative, neutral, or mixed. For example, you can use sentiment analysis to determine the sentiments of comments on a blog posting to determine if your readers liked the post.\n", "\n", "Determine sentiment operations can be performed using any of the primary languages supported by Amazon Comprehend. All documents must be in the same language.\n", "\n", "You can use any of the following operations to detect the sentiment of a document or a set of documents.\n", "\n", " DetectSentiment\n", "\n", " BatchDetectSentiment\n", "\n", " StartSentimentDetectionJob\n", "\n", "The operations return the most likely sentiment for the text as well as the scores for each of the sentiments. The score represents the likelihood that the sentiment was correctly detected. For example, in the example below it is 95 percent likely that the text has a Positive sentiment. There is a less than 1 percent likelihood that the text has a Negative sentiment. You can use the SentimentScore to determine if the accuracy of the detection meets the needs of your application.\n", "\n", "The DetectSentiment operation returns an object that contains the detected sentiment and a SentimentScore object. The BatchDetectSentiment operation returns a list of sentiments and SentimentScore objects, one for each document in the batch. The StartSentimentDetectionJob operation starts an asynchronous job that produces a file containing a list of sentiments and SentimentScore objects, one for each document in the job. \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This lab includes step-by-step instructions for performing sentiment analysis using Amazon Comprehend." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup\n", "\n", "Let's start by specifying:\n", "\n", "* AWS region.\n", "* The IAM role arn used to give access to Comprehend API and S3 bucket.\n", "* The S3 bucket that you want to use for training and model data.\n" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "\n", "import os\n", "import boto3\n", "import re\n", "import json\n", "import sagemaker\n", "from sagemaker import get_execution_role\n", "\n", "region = boto3.Session().region_name\n", "\n", "role = get_execution_role()\n", "\n", "bucket = sagemaker.Session().default_bucket()" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s3://sagemaker-us-east-1-340280328827/sagemaker/sentiment-analysis\n" ] } ], "source": [ "prefix = \"sagemaker/sentiment-analysis\"\n", "bucketuri=\"s3://\"+bucket+\"/\"+prefix\n", "print(bucketuri)\n", "# customize to your bucket where you have stored the data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data\n", "Let's start by uploading the dataset the sample data s3 bucket.The sample dataset contains Amazon reviews taken from the larger dataset \"Amazon reviews - Full\", which was published with the article \"Character-level Convolutional Networks for Text Classification\" (Xiang Zhang et al., 2015). \n", "\n", "Now lets read this into a Pandas data frame and take a look.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# Download the data set\n", "\n", "!wget https://docs.aws.amazon.com/comprehend/latest/dg/samples/tutorial-reviews-data.zip\n", "!apt-get install unzip -y\n", "!unzip -o tutorial-reviews-data.zip\n", "\n" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Review | \n", "
---|---|
0 | \n", "Written in old English. It was very hard to re... | \n", "
1 | \n", "Thought I was getting a book received book on ... | \n", "
2 | \n", "this book was recommend 2 me from my neighbor.... | \n", "
3 | \n", "I believe that this is a fantastic book for th... | \n", "
4 | \n", "I always liked this book. The kindle version i... | \n", "
... | \n", "... | \n", "
31 | \n", "a classic book where there is really nothing b... | \n", "
32 | \n", "I had to read this book, which I had heard qui... | \n", "
33 | \n", "This book was purchased for my daughter for a ... | \n", "
34 | \n", "A classic? Hardly. In it's time this book may ... | \n", "
35 | \n", "The whole time reading The Scarlet Letter I th... | \n", "
36 rows × 1 columns
\n", "