Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphi

Easily Perform Powerful Text Analysis With Google Machine Learning

nlapi device2

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.

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.

nlapirest 6234578

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.

nlapi 8436454

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.

Head over and download the full source code for the desktop and mobile Google Cloud Natural Language API REST demo.

nlapi device
See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

FMXExpress.com has over 600 articles with all kinds of tips and tricks for Delphi FireMonkey on Android, IOS, OSX, Windows, and Linux.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES