|
| 1 | + |
| 2 | +# Google Cloud Natural Language API Sample |
| 3 | + |
| 4 | +This Python sample demonstrates the use of the [Google Cloud Natural Language API][NL-Docs] |
| 5 | +for sentiment, entity, and syntax analysis. |
| 6 | + |
| 7 | +[NL-Docs]: https://cloud.google.com/natural-language/docs/ |
| 8 | + |
| 9 | +## Setup |
| 10 | + |
| 11 | +Please follow the [Set Up Your Project](https://cloud.google.com/natural-language/docs/getting-started#set_up_your_project) |
| 12 | +steps in the Quickstart doc to create a project and enable the |
| 13 | +Cloud Natural Language API. Following those steps, make sure that you |
| 14 | +[Set Up a Service Account](https://cloud.google.com/natural-language/docs/common/auth#set_up_a_service_account), |
| 15 | +and export the following environment variable: |
| 16 | + |
| 17 | +``` |
| 18 | +export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json |
| 19 | +``` |
| 20 | + |
| 21 | +## Run the sample |
| 22 | + |
| 23 | +Install [pip](https://pip.pypa.io/en/stable/installing) if not already installed. |
| 24 | + |
| 25 | +To run the example, install the necessary libraries using pip: |
| 26 | + |
| 27 | +```sh |
| 28 | +$ pip install -r requirements.txt |
| 29 | +``` |
| 30 | + |
| 31 | +Then, run the script: |
| 32 | + |
| 33 | +```sh |
| 34 | +$ python analyze.py <command> <text-string> |
| 35 | +``` |
| 36 | + |
| 37 | +where `<command>` is one of: `entities`, `sentiment`, or `syntax`. |
| 38 | + |
| 39 | +The script will write to STDOUT the json returned from the API for the requested feature. |
| 40 | + |
| 41 | +* Example1: |
| 42 | + |
| 43 | +```sh |
| 44 | +$ python analyze.py entities "Tom Sawyer is a book written by a guy known as Mark Twain." |
| 45 | +``` |
| 46 | + |
| 47 | +You will see something like the following returned: |
| 48 | + |
| 49 | +``` |
| 50 | +{ |
| 51 | + "entities": [ |
| 52 | + { |
| 53 | + "salience": 0.50827783, |
| 54 | + "mentions": [ |
| 55 | + { |
| 56 | + "text": { |
| 57 | + "content": "Tom Sawyer", |
| 58 | + "beginOffset": 0 |
| 59 | + }, |
| 60 | + "type": "PROPER" |
| 61 | + } |
| 62 | + ], |
| 63 | + "type": "PERSON", |
| 64 | + "name": "Tom Sawyer", |
| 65 | + "metadata": { |
| 66 | + "mid": "/m/01b6vv", |
| 67 | + "wikipedia_url": "http://en.wikipedia.org/wiki/The_Adventures_of_Tom_Sawyer" |
| 68 | + } |
| 69 | + }, |
| 70 | + { |
| 71 | + "salience": 0.22226454, |
| 72 | + "mentions": [ |
| 73 | + { |
| 74 | + "text": { |
| 75 | + "content": "book", |
| 76 | + "beginOffset": 16 |
| 77 | + }, |
| 78 | + "type": "COMMON" |
| 79 | + } |
| 80 | + ], |
| 81 | + "type": "WORK_OF_ART", |
| 82 | + "name": "book", |
| 83 | + "metadata": {} |
| 84 | + }, |
| 85 | + { |
| 86 | + "salience": 0.18305534, |
| 87 | + "mentions": [ |
| 88 | + { |
| 89 | + "text": { |
| 90 | + "content": "guy", |
| 91 | + "beginOffset": 34 |
| 92 | + }, |
| 93 | + "type": "COMMON" |
| 94 | + } |
| 95 | + ], |
| 96 | + "type": "PERSON", |
| 97 | + "name": "guy", |
| 98 | + "metadata": {} |
| 99 | + }, |
| 100 | + { |
| 101 | + "salience": 0.086402282, |
| 102 | + "mentions": [ |
| 103 | + { |
| 104 | + "text": { |
| 105 | + "content": "Mark Twain", |
| 106 | + "beginOffset": 47 |
| 107 | + }, |
| 108 | + "type": "PROPER" |
| 109 | + } |
| 110 | + ], |
| 111 | + "type": "PERSON", |
| 112 | + "name": "Mark Twain", |
| 113 | + "metadata": { |
| 114 | + "mid": "/m/014635", |
| 115 | + "wikipedia_url": "http://en.wikipedia.org/wiki/Mark_Twain" |
| 116 | + } |
| 117 | + } |
| 118 | + ], |
| 119 | + "language": "en" |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +* Example2: |
| 124 | + |
| 125 | +```sh |
| 126 | +$ python analyze.py entities "Apple has launched new iPhone." |
| 127 | +``` |
| 128 | + |
| 129 | +You will see something like the following returned: |
| 130 | + |
| 131 | +``` |
| 132 | +{ |
| 133 | + "entities": [ |
| 134 | + { |
| 135 | + "salience": 0.72550339, |
| 136 | + "mentions": [ |
| 137 | + { |
| 138 | + "text": { |
| 139 | + "content": "Apple", |
| 140 | + "beginOffset": 0 |
| 141 | + }, |
| 142 | + "type": "PROPER" |
| 143 | + } |
| 144 | + ], |
| 145 | + "type": "ORGANIZATION", |
| 146 | + "name": "Apple", |
| 147 | + "metadata": { |
| 148 | + "mid": "/m/0k8z", |
| 149 | + "wikipedia_url": "http://en.wikipedia.org/wiki/Apple_Inc." |
| 150 | + } |
| 151 | + }, |
| 152 | + { |
| 153 | + "salience": 0.27449661, |
| 154 | + "mentions": [ |
| 155 | + { |
| 156 | + "text": { |
| 157 | + "content": "iPhone", |
| 158 | + "beginOffset": 23 |
| 159 | + }, |
| 160 | + "type": "PROPER" |
| 161 | + } |
| 162 | + ], |
| 163 | + "type": "CONSUMER_GOOD", |
| 164 | + "name": "iPhone", |
| 165 | + "metadata": { |
| 166 | + "mid": "/m/027lnzs", |
| 167 | + "wikipedia_url": "http://en.wikipedia.org/wiki/IPhone" |
| 168 | + } |
| 169 | + } |
| 170 | + ], |
| 171 | + "language": "en" |
| 172 | +} |
| 173 | +``` |
0 commit comments