From 316965f28a4d9104700e008414ac5b35686160a0 Mon Sep 17 00:00:00 2001 From: Jieyi Ma Date: Mon, 12 Mar 2018 20:23:36 -0700 Subject: [PATCH] add reference --- NLP_GCP_RickandMorty.ipynb | 297 +++++++++++++++++++++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 NLP_GCP_RickandMorty.ipynb diff --git a/NLP_GCP_RickandMorty.ipynb b/NLP_GCP_RickandMorty.ipynb new file mode 100644 index 0000000..332e005 --- /dev/null +++ b/NLP_GCP_RickandMorty.ipynb @@ -0,0 +1,297 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](file:///Users/Jessica/Desktop/Screenshot%202018-03-11%2023.37.25.png)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " # Natural Language Processing with Google Cloud Platform\n", + " -- In Application with Rick & Morty " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "toc": true + }, + "source": [ + "

Table of Contents

\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://images7.alphacoders.com/633/thumb-1920-633262.png)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setting Up" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using a service account to [get authetication](https://cloud.google.com/docs/authentication/getting-started). \n", + "\n", + "### [Create a service account](https://console.cloud.google.com/apis/credentials/serviceaccountkey?_ga=2.11759798.-323970793.1520889464)\n", + "- select **new service account**\n", + "- From the **Role** dropdown, select **Project > Owner**.\n", + "- **Create** botton, download the JSON file that contains the key to local computer\n", + "\n", + "### Open terminal and type the following command\n", + "- **export GOOGLE_APPLICATION_CREDENTIALS=\"[PATH]\"**\n", + "- **jupyter notebook**\n", + " - ![setup](https://user-images.githubusercontent.com/31974451/37270410-f0b50b22-258b-11e8-8796-9256498b667c.png)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### [Verifying authentication](https://cloud.google.com/docs/authentication/getting-started#auth-cloud-implicit-python)\n", + "\n", + "I use python in jupyter notebook: " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def implicit():\n", + " from google.cloud import storage\n", + "\n", + " # If you don't specify credentials when constructing the client, the\n", + " # client library will look for credentials in the environment.\n", + " storage_client = storage.Client()\n", + "\n", + " # Make an authenticated API request\n", + " buckets = list(storage_client.list_buckets())\n", + " print(buckets)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "#!pip3 install google.cloud" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Importing packages" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "# Imports the Google Cloud client library\n", + "import google.cloud\n", + "from google.cloud import language\n", + "from google.cloud.language import enums\n", + "from google.cloud.language import types\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Sentiment Analysis" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "text = \"Rick is an eccentric and alcoholic mad scientist, who eschews many ordinary conventions such as school, marriage, love, and family.\"\n", + "client = language.LanguageServiceClient()\n", + "document = types.Document(\n", + " content=text,\n", + " type=enums.Document.Type.PLAIN_TEXT)\n", + "\n", + "sentiment = client.analyze_sentiment(document).document_sentiment" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Score: 0.8999999761581421\n", + "Magnitude: 0.8999999761581421\n" + ] + } + ], + "source": [ + "print('Score: {}'.format(sentiment.score))\n", + "print('Magnitude: {}'.format(sentiment.magnitude))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[name: \"Rick\"\n", + "type: PERSON\n", + "salience: 0.758594810962677\n", + "mentions {\n", + " text {\n", + " content: \"Rick\"\n", + " begin_offset: -1\n", + " }\n", + " type: PROPER\n", + "}\n", + "mentions {\n", + " text {\n", + " content: \"mad scientist\"\n", + " begin_offset: -1\n", + " }\n", + " type: COMMON\n", + "}\n", + ", name: \"conventions\"\n", + "type: OTHER\n", + "salience: 0.08138427138328552\n", + "mentions {\n", + " text {\n", + " content: \"conventions\"\n", + " begin_offset: -1\n", + " }\n", + " type: COMMON\n", + "}\n", + ", name: \"love\"\n", + "type: OTHER\n", + "salience: 0.061606135219335556\n", + "mentions {\n", + " text {\n", + " content: \"love\"\n", + " begin_offset: -1\n", + " }\n", + " type: COMMON\n", + "}\n", + ", name: \"family\"\n", + "type: PERSON\n", + "salience: 0.04316957667469978\n", + "mentions {\n", + " text {\n", + " content: \"family\"\n", + " begin_offset: -1\n", + " }\n", + " type: COMMON\n", + "}\n", + ", name: \"school\"\n", + "type: ORGANIZATION\n", + "salience: 0.027622591704130173\n", + "mentions {\n", + " text {\n", + " content: \"school\"\n", + " begin_offset: -1\n", + " }\n", + " type: COMMON\n", + "}\n", + ", name: \"marriage\"\n", + "type: OTHER\n", + "salience: 0.027622591704130173\n", + "mentions {\n", + " text {\n", + " content: \"marriage\"\n", + " begin_offset: -1\n", + " }\n", + " type: COMMON\n", + "}\n", + "]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "entities = client.analyze_entities(document).entities\n", + "entities" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.4" + }, + "toc": { + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": true, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": true, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}