# WebAPI Language Detection

> The following examples explain how to use activities such as [HTTP Request](https://docs.uipath.com/activities/other/latest/developer/http-client) or [Deserialize Json](https://docs.uipath.com/activities/other/latest/developer/deserialize-json) for calling a public API, extracting information, and displaying the results. You can find these activities in the **UiPath.WebAPI.Activities** package.

The following examples explain how to use activities such as [HTTP Request](https://docs.uipath.com/activities/other/latest/developer/http-client) or [Deserialize Json](https://docs.uipath.com/activities/other/latest/developer/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 Name | Variable Type | Default Value |
     | --- | --- | --- |
     | `LanguagesDT` | **DataTable** |  |
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](https://dev-assets.cms.uipath.com/assets/images/activities/developer-docs-image-179048-e43a7b4a-b24a2ed6.webp)
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 Name | Variable Type | Default Value |
     | --- | --- | --- |
     | `APIKey` | **String** |  |
     | `Text` | **String** |  |
     | `Result` | **String** |  |
     | `StatusCode` | **Int32** |  |
     | `LanguagesDictionary` | **Dictionary<String, String>** |  |
     | `LanguageCode` | **String** |  |
     | `Reliable` | **Boolean** |  |
     | `Confidence` | **Double** |  |
     | `LanguageName` | **String** |  |
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](https://docs.uipath.com/activities/other/latest/developer/http-client) by adding the **End point** address, the **Request Method**, the form of the **Accept response**, and the **Authentication** method.

     ![docs image](https://dev-assets.cms.uipath.com/assets/images/activities/developer-docs-image-2020-02-20_14-22-49-96ed1525-a7b52064.png)
   * 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 Name | Variable Type | Default Value |
     | --- | --- | --- |
     | `ResultJSON` | **JObject** |  |
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 Name | Argument's Direction | Argument Type | Default Value |
     | --- | --- | --- | --- |
     | `out_LanguagesDictionary` | **Out** | **Dictionary<String, String>** | **LanguagesDictionary** |

     ![docs image](https://dev-assets.cms.uipath.com/assets/images/activities/developer-docs-image-181016-8547e445-109bfa4f.gif)
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](https://dev-assets.cms.uipath.com/assets/images/activities/developer-docs-image-179772-c134b17e-d6387c89.webp)
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 Name | Variable Type | Default Value |
     | --- | --- | --- |
     | `APIKey` | **String** |  |
     | `Texts` | **String[]** |  |
     | `Result` | **String** |  |
     | `StatusCode` | **Int32** |  |
     | `HTTPRequestBodyJSON` | **String** |  |
3. Add an **Assign**activity 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](https://docs.uipath.com/activities/other/latest/developer/http-client) by adding the **End point** address, the **Request Method**, form of the **Accept response**, and the **Authentication** method.

     ![docs image](https://dev-assets.cms.uipath.com/assets/images/activities/developer-docs-image-2020-02-19_17-43-42-8e1e10df-861a0683.png)
   * 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 Name | Variable Type | Default Value |
     | --- | --- | --- |
     | `ResultJSON` | **JObject** |  |
     | `LanguagesDictionary` | **System.Collections.Generic.Dictionary<System.String, System.String>** |  |
     | `LanguageCode` | **String** |  |
     | `LanguageName` | **String** |  |
     | `Reliable` | **Boolean** |  |
     | `Confidence` | **Double** |  |
     | `index` | **Int32** | **0** |
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 Name | Argument's Direction | Argument Type | Default Value |
     | --- | --- | --- | --- |
     | `out_LanguagesDictionary` | **Out** | **Dictionary<String, String>** | **LanguagesDictionary** |

     ![docs image](https://dev-assets.cms.uipath.com/assets/images/activities/developer-docs-image-181016-8547e445-109bfa4f.gif)
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](https://dev-assets.cms.uipath.com/assets/images/activities/developer-docs-image-181080-a1676fb5-f216ec9b.webp)
19. Run the process to detect and log the languages used in the text

[Here](https://docexamples.uipath.com/examples/Activities/WebAPI%20Language%20Detection%20-%20Example.zip) you can download an example.
