|
| 1 | +.. This file is automatically generated. Do not edit this file directly. |
| 2 | +
|
| 3 | +Google Cloud Natural Language API Python Samples |
| 4 | +=============================================================================== |
| 5 | + |
| 6 | +This directory contains samples for Google Cloud Natural Language API. The `Google Cloud Natural Language API`_ provides natural language understanding technologies to developers. |
| 7 | + |
| 8 | +This tutorial demostrates how to use the `classify_text` method to classify content category of text files, and use the result to compare texts by their similarity to each other. See the `tutorial page`_ for details about this sample. |
| 9 | + |
| 10 | +.. _tutorial page: https://cloud.google.com/natural-language/docs/classify-text-tutorial |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +.. _Google Cloud Natural Language API: https://cloud.google.com/natural-language/docs/ |
| 16 | + |
| 17 | +Setup |
| 18 | +------------------------------------------------------------------------------- |
| 19 | + |
| 20 | + |
| 21 | +Authentication |
| 22 | +++++++++++++++ |
| 23 | + |
| 24 | +Authentication is typically done through `Application Default Credentials`_, |
| 25 | +which means you do not have to change the code to authenticate as long as |
| 26 | +your environment has credentials. You have a few options for setting up |
| 27 | +authentication: |
| 28 | + |
| 29 | +#. When running locally, use the `Google Cloud SDK`_ |
| 30 | + |
| 31 | + .. code-block:: bash |
| 32 | +
|
| 33 | + gcloud auth application-default login |
| 34 | +
|
| 35 | +
|
| 36 | +#. When running on App Engine or Compute Engine, credentials are already |
| 37 | + set-up. However, you may need to configure your Compute Engine instance |
| 38 | + with `additional scopes`_. |
| 39 | + |
| 40 | +#. You can create a `Service Account key file`_. This file can be used to |
| 41 | + authenticate to Google Cloud Platform services from any environment. To use |
| 42 | + the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to |
| 43 | + the path to the key file, for example: |
| 44 | + |
| 45 | + .. code-block:: bash |
| 46 | +
|
| 47 | + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json |
| 48 | +
|
| 49 | +.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow |
| 50 | +.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using |
| 51 | +.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount |
| 52 | + |
| 53 | +Install Dependencies |
| 54 | +++++++++++++++++++++ |
| 55 | + |
| 56 | +#. Install `pip`_ and `virtualenv`_ if you do not already have them. |
| 57 | + |
| 58 | +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. |
| 59 | + |
| 60 | + .. code-block:: bash |
| 61 | +
|
| 62 | + $ virtualenv env |
| 63 | + $ source env/bin/activate |
| 64 | +
|
| 65 | +#. Install the dependencies needed to run the samples. |
| 66 | + |
| 67 | + .. code-block:: bash |
| 68 | +
|
| 69 | + $ pip install -r requirements.txt |
| 70 | +
|
| 71 | +.. _pip: https://pip.pypa.io/ |
| 72 | +.. _virtualenv: https://virtualenv.pypa.io/ |
| 73 | + |
| 74 | +Samples |
| 75 | +------------------------------------------------------------------------------- |
| 76 | + |
| 77 | +Classify Text Tutorial |
| 78 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +To run this sample: |
| 83 | + |
| 84 | +.. code-block:: bash |
| 85 | +
|
| 86 | + $ python classify_text_tutorial.py |
| 87 | +
|
| 88 | + usage: classify_text_tutorial.py [-h] |
| 89 | + {classify,index,query,query-category} ... |
| 90 | + |
| 91 | + Using the classify_text method to cluster texts. |
| 92 | + |
| 93 | + positional arguments: |
| 94 | + {classify,index,query,query-category} |
| 95 | + classify Classify the input text into categories. |
| 96 | + index Classify each text file in a directory and write the |
| 97 | + results to the index_file. |
| 98 | + query Find the indexed files that are the most similar to |
| 99 | + the query text. |
| 100 | + query-category Find the indexed files that are the most similar to |
| 101 | + the query label. The list of all available labels: |
| 102 | + https://cloud.google.com/natural- |
| 103 | + language/docs/categories |
| 104 | + |
| 105 | + optional arguments: |
| 106 | + -h, --help show this help message and exit |
| 107 | +
|
| 108 | +
|
| 109 | +
|
| 110 | +
|
| 111 | +The client library |
| 112 | +------------------------------------------------------------------------------- |
| 113 | + |
| 114 | +This sample uses the `Google Cloud Client Library for Python`_. |
| 115 | +You can read the documentation for more details on API usage and use GitHub |
| 116 | +to `browse the source`_ and `report issues`_. |
| 117 | + |
| 118 | +.. _Google Cloud Client Library for Python: |
| 119 | + https://googlecloudplatform.github.io/google-cloud-python/ |
| 120 | +.. _browse the source: |
| 121 | + https://github.com/GoogleCloudPlatform/google-cloud-python |
| 122 | +.. _report issues: |
| 123 | + https://github.com/GoogleCloudPlatform/google-cloud-python/issues |
| 124 | + |
| 125 | + |
| 126 | +.. _Google Cloud SDK: https://cloud.google.com/sdk/ |
0 commit comments