Skip to content

Commit

Permalink
add reference
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyima committed Mar 13, 2018
1 parent eb15fcc commit 316965f
Showing 1 changed file with 297 additions and 0 deletions.
297 changes: 297 additions & 0 deletions NLP_GCP_RickandMorty.ipynb
Original file line number Diff line number Diff line change
@@ -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": [
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Setting-Up\" data-toc-modified-id=\"Setting-Up-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Setting Up</a></span><ul class=\"toc-item\"><li><span><a href=\"#Create-a-service-account\" data-toc-modified-id=\"Create-a-service-account-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span><a href=\"https://console.cloud.google.com/apis/credentials/serviceaccountkey?_ga=2.11759798.-323970793.1520889464\" target=\"_blank\">Create a service account</a></a></span></li><li><span><a href=\"#Open-terminal-and-type-the-following-command\" data-toc-modified-id=\"Open-terminal-and-type-the-following-command-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>Open terminal and type the following command</a></span></li><li><span><a href=\"#Verifying-authentication\" data-toc-modified-id=\"Verifying-authentication-1.3\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span><a href=\"https://cloud.google.com/docs/authentication/getting-started#auth-cloud-implicit-python\" target=\"_blank\">Verifying authentication</a></a></span></li></ul></li><li><span><a href=\"#Importing-packages\" data-toc-modified-id=\"Importing-packages-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Importing packages</a></span></li><li><span><a href=\"#Sentiment-Analysis\" data-toc-modified-id=\"Sentiment-Analysis-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>Sentiment Analysis</a></span></li></ul></div>"
]
},
{
"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
}

0 comments on commit 316965f

Please sign in to comment.