Intro

Dearest gentle reader, have you ever come across comments on the internet in a language you don’t recognize? Or maybe you’re curious to learn what language a piece of text is in? This blog will guide you through the process of detecting languages from any text using Alteryx.

A few weeks ago, we had a full Alteryx Week filled with learning about APIs and macros, covering topics from web scraping to reporting. One of the highlights was using an API to download Game of Thrones data from our coach Shane’s server. His collection of data projects was designed to test and enhance our skills in Alteryx.

At the end of the week, Shane presented us with a unique challenge, and I became the bearer of this task. In this blog, I’ll walk you through the steps to create an app and macro in Alteryx to detect languages from text using an API.

Let’s Begin!

To start with, we need an API that can help us detect languages. For this purpose, we will use the Detect Language API. The first step in any API integration is to refer to the documentation. 

If you can take away one thing from this blog, it would be: to always read the documentation.

This will help us understand how to use the API, what endpoints to call, and the structure of the requests and responses.

This API can fabulously detect 146 languages. Wait till the end to see the results!

Next, we move on to Alteryx. I’ll show you two ways to use the API to detect language: through an App and through Macros. The app will let users input their text and return results, while macros are useful if you have data and need output in a table format.

A. Detect Language App

First, let’s go through the process of creating an app in Alteryx.

  1. Step 1: Input Data

    1. Provide URL, API Key, and Text:
      • Pull a Text Input tool into Alteryx and input these values. You know the URL already; login as a user and get the API key.

    Step 2: Authorization

    1. Add Bearer Token:
      • Use a Formula tool to prepend ‘Bearer ‘ to your API key. Additionally, as per the documentation, rename the text column to ‘q’ so the API knows which column contains the text.

 

2. To authorize, you need to add Bearer in front of your API key so, bring on the formula tool to add that. Also, as per documentation it will look for q to know which column contains text so, I have renamed my column as ‘q’ in this step too.

Step 3: Download Tool Configuration

  1. Set HTTP Method to POST:
    • Drag a Download tool onto the canvas. In the Payload tab, select POST and ensure the ‘q’ field is checked to add values.

If successful, you will get a 200 response from the API, and your download data will look something like this:

You can see the json scrpt in DownloadData with its tags for language, reliability and confidence.

Step 4: Parse the Response

  1. JSON Parse:
    • Use a JSON Parse tool to interpret the JSON response from the API.

Step 5: Reshape Data with Crosstab

  1. Apply Crosstab Tool:
    • Group the data by ‘q’ and aggregate values to get separate columns for language, isReliable, and confidence.

You’ve got the language code (e.g., ‘ja’ for Japanese). If you prefer full language names, you can download a CSV file with the language list or parse it directly from another JSON source.

Please refer to the simple workflow down below as to how I achieved this.

Step 6: Create an Interactive App

  1. Add Interface Tool:
    • Add a Text Box from the Interface tools and connect it to the input at the start of this workflow. Configure the Text Box to change the data value when the user inputs their text. Ensure “On success – Show results to user” is checked in the Interface Designer settings.

Below is the full workflow of this app.

Now, when a user types their language into the text box, this app will provide the result as to which language the text belongs to. This app is so cool that it even detects the ancient forgotten language of Sanskrit. Isn’t that awesome?

 

 

 

 

 

B. Detect Language Macro

Creating a standard macro is pretty simple now. We will just change the input and output of this workflow to be macro input and output.

  1. Create Macro Input/Output:
    • Replace the standard input and output tools with Macro Input and Macro Output tools.
  2. Customize Icon (Optional):
    • In the Interface Designer, you can select a custom icon if you wish.

To test if the macro works, create a new input table with some texts from different languages and add the API key and URL as new columns. Connect it to the macro and output the results.

 

 

 

 

 

 

 

 

 

 

 

 

 

As you can see from the output, this macro successfully detected the languages.

And that’s it for today’s blog. I hope this was helpful to you. Happy language detecting!

#alteryx #detectlanguage #api #macro

Prerana Amatya
Author: Prerana Amatya