[PYTHON] From the introduction of GoogleCloudPlatform Natural Language API to how to use it

From the introduction of GoogleCloudPlatform Natural Language API to how to use it

Download SDK

https://cloud.google.com/sdk/docs/quickstart-mac-os-x?hl=ja

https://cloud.google.com/natural-language/docs/common/auth?hl=ja#set_up_a_service_account

export GOOGLE_APPLICATION_CREDENTIALS="/Users/users/hoge/key.json"

Preparing the Google Cloud Natural Language API

https://cloud.google.com/natural-language/docs/getting-started?hl=ja

python binding

https://github.com/GoogleCloudPlatform/google-cloud-python python3.6 is also supported


pip install --upgrade google-cloud
gcloud auth application-default login

https://googlecloudplatform.github.io/google-cloud-python/stable/language-responses.html#google.cloud.language.entity.Entity

https://googlecloudplatform.github.io/google-cloud-python/stable/language-usage.html Create a convenient class as follows, referring to:

from google.cloud import language
class GCNaturalLanguage(object):
    def __init__(self, upper=10000):
        # Instantiates a client
        self.client = language.Client()
        self.upper = upper

    def get_entity(self, text):
        length = len(text)

        if length > self.upper:
            print("{} .. too long".format(length))
            return {}

        document = self.client.document_from_text(text, language='ja')

        # Detects the sentiment of the text
        res = document.analyze_entities()
        print("{} characters => done!".format(len(text)))
        dic = {}
        for entity in res.entities:

            for m in entity.mentions:
                dic.update({m.text.begin_offset: m.text.content})

        return dic

Example)

import GCNaturalLanguage
gcn = GCNaturalLanguage()
dic = gcn.get_entity("I tried setting cross domain with access analysis")
print(dic)
# 21 characters => done!
# {0: 'access analysis', 7: 'Cross domain'}

By the way, in MeCab,

#Mecab, which is popular as an extended dictionary-ipadic-I use neologd
$ mecab -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd
I tried setting cross domain with access analysis
Access analysis noun,Proper noun,General,*,*,*,access analysis,Access Kaiseki,Access Kaiseki
Particles,Case particles,General,*,*,*,so,De,De
Cross noun,Change connection,*,*,*,*,cross,cross,cross
Domain noun,General,*,*,*,*,domain,domain,domain
Particles,Case particles,General,*,*,*,To,Wo,Wo
Setting noun,Change connection,*,*,*,*,Setting,Setting,Setting
Verb,Independence,*,*,Sahen Suru,Continuous form,To do,Shi,Shi
Particles,Connection particle,*,*,*,*,hand,Te,Te
Only verb,Non-independent,*,*,One step,Continuous form,View,Mi,Mi
Auxiliary verb,*,*,*,Special,Uninflected word,Ta,Ta,Ta
EOS

have become. You can see that "access analysis" is said well for both the former and the latter. (By the way, if you do morphological analysis directly as mecab without taking mecab-ipadic-neologd as an argument, "access" and "analysis" will be separated.)

However, for example, if you want to extract the terminology "cross-domain", you cannot extract it directly with MeCab, so using the external tool GCP Natural Language API will achieve your goal. As a way of using it in the future, it is good to register the one extracted by GCP as a new word in the user dictionary and then use it like using MeCab again [^ add].

[^ add]: I wrote the details at http://qiita.com/knknkn1162/items/8c12f42dd167aae01c02#_reference-aa421a94c959d84ff7fb.

Recommended Posts

From the introduction of GoogleCloudPlatform Natural Language API to how to use it
How to use Python Kivy (reference) -I translated Kivy Language of API reference-
How to use the Google Cloud Translation API
How to use the NHK program guide API
From the introduction of pyethapp to the execution of contract
How to use Python Kivy ① ~ Basics of Kv Language ~
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to post a ticket from the Shogun API
How to use the generator
How to use the decorator
[Introduction] How to use open3d
Summary from the beginning to Chapter 1 of the introduction to design patterns learned in the Java language
How to limit the API to be published in the C language shared library of Linux
How to calculate the amount of calculation learned from ABC134-D
Introduction of DataLiner ver.1.3 and how to use Union Append
How to use the zip function
How to use the optparse module
How to use SWIG from waf
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
How to use computer language slowly 2
How to use OpenPose's Python API
Use the Flickr API from Python
How to use bing search api
How to use computer language slowly
Summary of how to use csvkit
How to use the ConfigParser module
[Python] How to use Typetalk API
How to get followers and followers from python using the Mastodon API
[Introduction to Python] How to use the in operator in a for statement?
How to use Jupyter on the front end of supercomputer ITO
How to use machine learning for work? 01_ Understand the purpose of machine learning
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Summarize how to preprocess text (natural language processing) with tf.data.Dataset api
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
The Like (LGTM) order has disappeared from My Page, so use the Qiita API to get it.
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to use the Spark ML pipeline
How to check the version of Django
[TF] How to use Tensorboard from Keras
[Linux] How to use the echo command
How to calculate Use% of df command
How to use the Linux grep command
[Python2.7] Summary of how to use unittest
How to operate Linux from the console
Jupyter Notebook Basics of how to use
Basics of PyTorch (1) -How to use Tensor-
How to access the Datastore from the outside
How to use the IPython debugger (ipdb)
Summary of how to use Python list
How to use GCP's Cloud Vision API
[Python2.7] Summary of how to use subprocess
Introduction to Scapy ① (From installation to execution of Scapy)
[Question] How to use plot_surface of python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Introduction of cyber security framework "MITRE CALDERA": How to use and training
[C language] How to use the crypt function on Linux [Password hashing]
How to calculate the volatility of a brand
How to use the C library in Python