Google Cloud offers a Natural Language API which allows a developer to take unstructured text as an input and utilize Google’s machine learning capabilities to derive insight from it. They have a number of different operations that can be performed on a piece of text including syntax analysis, entity analysis, custom entity extraction, sentiment analysis, custom sentiment analysis, content classification, custom content classification, custom models, and spatial structure understanding. The Google Natural Language APIs feature multi-language support, large dataset support, and give you access to Google’s AutoML models.
RAD Studio and Delphi gives you easy access to all of this Natural Language processing capability via Google’s REST API. RAD Studio includes a tool called the REST Debugger where you can configure all of your REST API settings and then export them as components into your Delphi application. This includes wiring up the incoming data automatically to an in memory database table (TFDMemTable). It literally takes only a few minutes to get up and running with Google Cloud’s powerful Natural Language API from within Delphi and RAD Studio. Additionally, the application built and the source code available at the end of this blog post uses Delphi’s cross-platform/multi-platform FireMonkey framework which supports Windows, Linux, macOS, Android, and iOS with a single codebase and single responsive UI. Let’s dive into the Google Cloud Natural Language API and how to build a desktop and mobile application utilizing it’s REST API.
Table of Contents
What can I do with the Google Cloud Natural Language API?
On Google’s website the full REST reference for the Natural Language API is available. Here are the different endpoints available in the API:
- analyzeEntities POST /v1beta2/documents:analyzeEntities
- analyzeEntitySentiment POST /v1beta2/documents:analyzeEntitySentiment
- analyzeSentiment POST /v1beta2/documents:analyzeSentiment
- analyzeSyntax POST /v1beta2/documents:analyzeSyntax
- annotateText POST /v1beta2/documents:annotateText
- classifyText POST /v1beta2/documents:classifyText
How can I set up the Natural Language API credentials?
An API key is needed in order to use the above REST APIs. You will need to visit the following URL which will walk you through creating a project and enabled the Natural Language API on your Google Cloud account.
https://cloud.google.com/natural-language/docs/quickstart-client-libraries
Once you have the Natural Language API enabled on your account you can visit the Credentials page to create an API Key.
https://console.cloud.google.com/apis/credentials
How do I connect to the Google Cloud Natural Language API REST end point with Delphi?
I built a sample application in Delphi using the REST Debugger which utilizes the analyzeEntities end point. There is also a video tutorial for using the RAD Studio REST Debugger available to automatically create the REST components and paste them into your app. The analyzeEntiries endpoint breaks down the content of the text into entities that are contained within Google’s machine learning database. Entities have their own id (called mid), a type classification (like ‘ORGANIZATION’), and contain additional meta data like a Wikipedia URL and the like to provide context to that entity.
Here are the three components in Delphi that make the API call. They are the TRESTClient, TRESTRequest, and TRESTResponse. You will notice that the API URL is set on the BaseURL of TRESTClient. On the TRESTRequest component you will see that the request type is set to rmPOST, the ContentType is set to ctAPPLICATION_JSON, and that it contains one request body for the POST which is set to:
You will also notice that on the TRESTResponse component the RootElement is set to ‘entities’. This means that the ‘entities’ element in the JSON is specifically selected to be pulled into the in memory table (TFDMemTable).
What does the Natural Language API analyzeEntities endpoint return?
Here is a sample of the response JSON you will receive from the API:
Now that we’ve seen the process of how to configure the Natural Language API credentials, the REST endpoints needed, and the components in Delphi to connect to those endpoints let’s take a look at the full sample application.
How do I build a Windows 10 desktop or Android/iOS mobile device application utilizing the Google Cloud Natural Language API?
The sample application features a TMemo as a place to paste in text to be analyzed, a TStringGrid to display the results of the REST API call, and a TWebBrowser component to navigate and display the wikipedia_url property for each entity returned in the list. When the entity is selected in the TStringGrid it will load it’s Wikipedia URL in the TWebBrowser control.
The code for the application is pretty simple and consists of a button click to execute the REST request. The JSON POST content is built on the fly to take in the dynamic text from the TMemo. It also contains some additional code which sets the TWebBrowser control to utilize IE11.
In this blog post we’ve seen how to sign up for the Google Cloud Natural Language API and credentials to use the REST API. We’ve also seen the different endpoints it offers including the Analyze Entities endpoint. We’ve seen how to use the RAD Studio REST Debugger to connect to the endpoint and copy that code into a real application. And finally we’ve seen a real Windows 10 (and Linux and macOS and Android and iOS) application which connects to the Google Cloud Natural Language API and executes an entity analysis on a piece of text.
Check out how these speech services by Google could be vital for the success of your business operations in this guide.