![nlapidevice2 | Embarcadero RAD Studio Delphi C++Builder Blogs nlapi device2](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/nlapi_device2-4045515.jpg?resize=1140%2C694&ssl=1)
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.
![nlapirest6234578 | Embarcadero RAD Studio Delphi C++Builder Blogs nlapirest 6234578](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/nlapirest-6234578.png?resize=750%2C530&ssl=1)
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:
1 |
{document: {type: "PLAIN_TEXT", content: "Embarcadero Delphi is super powerful."}} |
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
object RESTClient1: TRESTClient Accept = 'application/json, text/plain; q=0.9, text/html;q=0.8,' AcceptCharset = 'utf-8, *;q=0.8' BaseURL = 'https://content-language.googleapis.com/v1beta1/documents:analyz' + 'eEntities?alt=json&key=your_api_key_here' ContentType = 'application/json' Params = <> Left = 40 Top = 328 end object RESTRequest1: TRESTRequest AssignedValues = [rvConnectTimeout, rvReadTimeout] Client = RESTClient1 Method = rmPOST Params = < item Kind = pkREQUESTBODY Name = 'body993652B584EA4AB59A378CFB104511F8' Value = '{document: {type: "PLAIN_TEXT", content: "Embarcadero Delphi is ' + 'super powerful."}}' ContentType = ctAPPLICATION_JSON end> Response = RESTResponse1 Left = 128 Top = 328 end object RESTResponse1: TRESTResponse ContentType = 'application/json' RootElement = 'entities' Left = 216 Top = 336 end |
What does the Natural Language API analyzeEntities endpoint return?
Here is a sample of the response JSON you will receive from the API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
{ "entities": [ { "name": "Embarcadero Delphi", "type": "ORGANIZATION", "metadata": { "mid": "/m/01z1nj", "wikipedia_url": "https://en.wikipedia.org/wiki/Delphi_(software)" }, "salience": 1, "mentions": [ { "text": { "content": "Embarcadero Delphi", "beginOffset": -1 }, "type": "PROPER" } ] } ], "language": "en" } |
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.
![nlapi8436454 | Embarcadero RAD Studio Delphi C++Builder Blogs nlapi 8436454](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/nlapi-8436454.png?resize=750%2C406&ssl=1)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
procedure TMainForm.Button1Click(Sender: TObject); begin var JsonObject := TJSONObject.Create; try var document := TJSONObject.Create; document.AddPair(TJSONPair.Create('type', 'PLAIN_TEXT')); document.AddPair(TJSONPair.Create('content', RequestMemo.Lines.Text)); JsonObject.AddPair(TJSONPair.Create('document', document)); RESTRequest1.Params[0].Value := JsonObject.ToJSON; RESTRequest1.Execute; finally JsonObject.Free; end; end; |
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.
![nlapidevice | Embarcadero RAD Studio Delphi C++Builder Blogs nlapi device](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/nlapi_device-6257950.jpg?resize=750%2C500&ssl=1)
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition