Getting Started with Text Parsing and Analysis for Go

[MUSIC PLAYING] SPEAKER: To get started with
the Cloud Natural Language API for Go, you’ll need
a GCP project, which you can set up in the console. In your project, enable
the Google Natural Language API for that project. Then create a service account
and download the private key as JSON. You need these credentials
to access Google Cloud APIs, so keep them secure and out
of your code and public repos. To access the credentials
from your project, go to the terminal and set
an environment variable to the path of your
service account. Now install the client library. If you use Cloud
Shell in the console, you can skip this step since
the required dependencies are already included. Make a new file or
open an existing file. I’m using VS Code, but you can
use whatever IDE you prefer. Import the Google Cloud
client library as well as some other packages you need. Then instantiate
a language client. If there is an error in
instantiating it, handle it. For the purposes
of this example, I’m just going to log it. Provide some text to analyze. I’m going to go with the
classic example here. Call AnalyzeSentiment passing
the context, a document object, and the type of encoding. The document comprises
a source of content, which, in this case, is
the text “Hello, world!” and a type of content,
which is plain text. Include some error handling. Again, for now, I’m just
going to log the error. If there is no error, the
AnalyzeSentiment function returns an analyze
sentiment response. Analyze sentiment response has
three properties– document sentiment, the overall
sentiment of the input document, language, which is the language
of the text, and sentences, which is the sentiment for all
the sentences in the document. In this example, I’m going to
focus on document sentiment. Log if the sentiment score
is positive or negative. Score is a sentiment
score between negative one for negative sentiment
and positive one for positive sentiment. OK, go ahead and run the code. And there you go. You just sent your first request
to the Natural Language API. But there is more that the Cloud
Natural Language API can do, like analyzing syntax
and annotating text. To find out more about
the client libraries, consult natural language basics,
or work through the sentiment analysis tutorial, check
out the Cloud documentation. [MUSIC PLAYING]


Add a Comment

Your email address will not be published. Required fields are marked *