Skip to main content

WebAPI Language Detection

The following examples explain how to use activities such as HTTP Request or Deserialize Json for calling a public API, extracting information, and displaying the results. You can find these activities in the UiPath.WebAPI.Activities package.

There are three workflows for detecting languages in texts using Language Detection API. The first workflow creates a dictionary of languages, the second detects the language in a single text, and the third detects languages in multiple texts.

Creating the Languages Dictionary

The workflow creates a languages dictionary from a CSV file that contains language codes and names. This workflow is invoked in the two other workflows.

This is how the automation process can be built:

  1. Open Studio and create a new Process.
  2. Download the archive with the project in this example and copy the file languages.csv to your project folder.
  3. Drag a Sequence container in the Workflow Designer.
    • Create the following variable:

      Variable NameVariable TypeDefault Value
      LanguagesDTDataTable
  4. Add a Read CSV activity inside the Sequence container.
    • Add the expression "languages.csv" in the FilePath field.
    • In the Properties panel, add the variable LanguagesDT in the DataTable field.
  5. Add an Assign activity after the Read CSV activity.
    • Add the expression out_LanguagesDictionary in the To field.

    • Add the expression LanguagesDT.AsEnumerable.ToDictionary(of string, string)(function(row) row("Code").ToString, function(row) row("Language").ToString) in the Value field.

      docs image

  6. Run the process to create the dictionary.

Detecting the Language in a Single Text

This is how the automation process can be built:

  1. Open Studio and create a new Process.
  2. Drag a Sequence container in the Workflow Designer.
    • Create the following variables:

      Variable NameVariable TypeDefault Value
      APIKeyString
      TextString
      ResultString
      StatusCodeInt32
      LanguagesDictionaryDictionary<String, String>
      LanguageCodeString
      ReliableBoolean
      ConfidenceDouble
      LanguageNameString
  3. Add an Assign activity inside the Sequence container.
    • Add the variable APIKey in the To field.
    • Add the expression "demo" in the Value field.
  4. Add an Assign activity after the Assign activity.
    • Add the variable Text in the To field.
    • Add the expression "Hello. This is a sample test." in the Value field.
  5. Add an HTTP Request activity after the Assign activity.
    • Configure the wizard by adding the End point address, the Request Method, the form of the Accept response, and the Authentication method.

      docs image

    • In the Properties panel, add the value application/json in the BodyFormat field. NOTE: the workflow shows application/xml.

    • Add the variable Result in the Result field.

    • Add the variable StatusCode in the StatusCode field.

  6. Add an If activity after the HTTP Request activity.
    • Add the expression StatusCode = 200 in the Condition field.
  7. Add a Sequence activity inside the Then field of the If activity.
    • Create the following variables:

      Variable NameVariable TypeDefault Value
      ResultJSONJObject
  8. Add an Invoke Workflow File activity inside the Sequence container.
    • Add the expression "GetLanguagesDictionary.xaml" in the Workflow Path field.

    • Select the Edit Arguments button and add the following argument:

      Argument NameArgument's DirectionArgument TypeDefault Value
      out_LanguagesDictionaryOutDictionary<String, String>LanguagesDictionary

      docs image

  9. Add a Deserialize JSON activity after the Invoke Workflow File activity.
    • Add the variable Result in the Json String field.
  10. Add an Assign activity after the Deserialize JSON activity.
    • Add the variable LanguageCode in the To field.
    • Add the expression ResultJSON("data")("detections")(0)("language").ToString in the Value field.
  11. Add an Assign activity after the previous Assign activity.
    • Add the variable LanguageName in the To field.
    • Add the expression LanguagesDictionary(LanguageCode) in the Value field.
  12. Add another Assign activity after the previous Assign activity.
    • Add the variable Reliable in the To field.
    • Add the expression CBool(ResultJSON("data")("detections")(0)("isReliable").ToString) in the Value field.
  13. Add an Assign activity after the previous Assign activity.
    • Add the variable Confidence in the To field.
    • Add the expression CDbl(ResultJSON("data")("detections")(0)("confidence").ToString) in the Value field.
  14. Add a Log Message after the Assign activity.
    • Add the expression "Detection for the text:" + vbCrLf + " Language is " + LanguageName+ vbCrLf +" Reliable detection: " + Reliable.ToString + vbCrLf + " Confidence level: " + Confidence.ToString in the Message field.
  15. Add a Log Message activity inside the Else field of the If activity.
    • Select the Warn option from the Level drop-down list.
    • Add the expression "HTTP Request was not successful. Code: " + StatusCode.ToString in the Message field.
  16. Add a Log Message activity after the previous Log Message activity.
    • Select the Warn option from the Level drop-down list.

    • Add the expression "HTTP Request was not successful. Result: " + Result in the Message field.

      docs image

  17. Run the process to detect and log the language used in the text.

Detecting the Languages in a Batch of Texts

This is how the automation process can be built:

  1. Open Studio and create a new Process.
  2. Drag a Sequence container in the Workflow Designer.
    • Create the following variables:

      Variable NameVariable TypeDefault Value
      APIKeyString
      TextsString[]
      ResultString
      StatusCodeInt32
      HTTPRequestBodyJSONString
  3. Add an Assignactivity inside the Sequence container.
    • Add the variable APIKey in the To field.
    • Add the value "demo" in the Value field.
  4. Add another Assign activity after the previous one.
    • Add the variable Texts in the To field.
    • Add the value {"Hello world.", "Buenos dias, señor.", "Guten Tag.", "Buna ziua, tuturor."} in the Value field.
  5. Add a new Assign activity after the previous Assign activity.
    • Add the variable HTTPRequestBodyJSON in the To field.
    • Add the value Newtonsoft.Json.JsonConvert.SerializeObject(new with{ .q = Texts }) in the Value field.
  6. Add an HTTP Request activity after the Assign activity.
    • Configure the wizard by adding the End point address, the Request Method, form of the Accept response, and the Authentication method.

      docs image

    • In the Properties panel, add the variable HTTPRequestBodyJSON in the Body field.

    • Add the value application/json in the BodyFormat field.

    • Add the variable Result in the Result field.

    • Add the variable StatusCode in the StatusCode field.

  7. Add an If activity after the HTTP Request activity.
    • Add the expression StatusCode = 200 in the Condition field.
  8. Add a Sequence activity inside the Then field of the If activity.
    • Create the following variables:

      Variable NameVariable TypeDefault Value
      ResultJSONJObject
      LanguagesDictionarySystem.Collections.Generic.Dictionary<System.String, System.String>
      LanguageCodeString
      LanguageNameString
      ReliableBoolean
      ConfidenceDouble
      indexInt320
  9. Add an Invoke Workflow File activity inside the Sequence container.
    • Add the expression "GetLanguagesDictionary.xaml" in the Workflow Path field.

    • Select the Edit Arguments button and add the following argument:

      Argument NameArgument's DirectionArgument TypeDefault Value
      out_LanguagesDictionaryOutDictionary<String, String>LanguagesDictionary

      docs image

  10. Add a Deserialize JSON activity after the Invoke Workflow File activity.
    • Add the variable Result in the Json String field.
  11. Add a For Each activity after the Deserialize JSON activity.
    • Add the expression ResultJSON("data")("detections") in the Values field.
  12. Add an Assign activity inside the Body of the For Each activity.
    • Add the variable LanguageCode in the To field.
    • Add the expression item(0)("language").ToString in the Value field.
  13. Add an Assign activity after the previous Assign activity.
    • Add the variable LanguageName in the To field.
    • Add the expression LanguagesDictionary(LanguageCode) in the Value field.
  14. Add another Assign activity after the previous Assign activity.
    • Add the variable Reliable in the To field.
    • Add the expression CBool(item(0)("isReliable").ToString) in the Value field.
  15. Add an Assign activity after the previous Assign activity.
    • Add the variable Confidence in the To field.
    • Add the expression CDbl(item(0)("confidence").ToString) in the Value field.
  16. Add a Log Message after the Assign activity.
    • Add the expression `"Detection for the text #"+index.ToString +":" + vbCrLf + " Language is " + LanguageName+ vbCrLf +" Reliable detection: "
      • Reliable.ToString + vbCrLf + " Confidence level: " + Confidence.ToString` in the Message field.
  17. Add a Log Message activity inside the Else field of the If activity.
    • Select the Warn option from the Level drop-down list.
    • Add the expression "HTTP Request was not successful. Code: " + StatusCode.ToString in the Message field.
  18. Add a Log Message activity after the previous Log Message activity.
    • Select the Warn option from the Level drop-down list.

    • Add the expression "HTTP Request was not successful. Result: " + Result in the Message field.

      docs image

  19. Run the process to detect and log the languages used in the text

Here you can download an example.