UiPath Documentation
activities
latest
false
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。

开发者活动

上次更新日期 2026年5月15日

WebAPI 语言检测

以下示例说明了如何使用“HTTP 请求”“反序列化 JSON”等活动来调用公共 API、提取信息以及显示结果。您可以在“UiPath.WebAPI.Activities ”包中找到这些活动。

使用语言检测 API 检测文本中的语言可通过三个工作流进行。 第一个工作流创建语言字典,第二个工作流检测单个文本中的语言,第三个工作流检测多个文本中的语言。

创建语言字典

工作流从包含语言代码和名称的 CSV 文件创建语言字典。 此工作流在其他两个工作流中调用。

此自动化流程的构建方法如下:

  1. 打开 Studio ,创建一个新“流程”
  2. Download the archive with the project in this example and copy the file languages.csv to your project folder.
  3. 将“序列”容器拖动至“工作流设计器”
    • 创建以下变量:

      变量名称变量类型默认值
      LanguagesDT数据表
  4. Add a Read CSV activity inside the Sequence container.
    • Add the expression "languages.csv" in the FilePath field.
    • 在“属性”面板中,向“数据表”字段添加“LanguagesDT”变量。
  5. “分配”活动添加到“读取 CSV”活动之后。
    • Add the expression out_LanguagesDictionary in the To field.

    • 在“值”字段添加表达式 LanguagesDT.AsEnumerable.ToDictionary(of string, string)(function(row) row("Code").ToString, function(row) row("Language").ToString)

  6. 运行该流程以创建字典。
检测单个文本中的语言

此自动化流程的构建方法如下:

  1. 打开 Studio ,创建一个新“流程”
  2. 将“序列”容器拖动至“工作流设计器”
    • 创建以下变量:

      变量名称变量类型默认值
      APIKey字符串
      Text字符串
      Result字符串
      StatusCodeInt32
      LanguagesDictionaryDictionary<String, String>
      LanguageCode字符串
      Reliable布尔值
      ConfidenceDouble
      LanguageName字符串
  3. 在“序列”容器中添加“分配”活动。
    • 在“收件人”字段中添加“APIKey”变量。
    • 在“值”字段添加表达式 "demo"
  4. “分配”活动添加到“分配”活动之后。
    • 在“收件人”字段中添加“Text”变量。
    • 在“值”字段添加表达式 "Hello. This is a sample test."
  5. “HTTP 请求”活动添加到“分配”活动之后。
    • 通过添加 端点 地址、 请求方法 接受响应表单 和 身份验证 方法来配置 向导

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

    • 在“结果”字段中添加 Result 变量。

    • Add the variable StatusCode in the StatusCode field.

  6. “HTTP 请求”活动之后添加“ IF 条件”活动。
    • 在“条件”字段中添加表达式“StatusCode = 200”。
  7. Add a Sequence activity inside the Then field of the If activity.
    • 创建以下变量:

      变量名称变量类型默认值
      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:

      参数名称参数方向参数类型默认值
      out_LanguagesDictionary输出Dictionary<String, String>LanguagesDictionary

  9. “反序列化 JSON”活动添加到“调用工作流文件”活动之后。
    • Add the variable Result in the Json String field.
  10. “分配”活动添加到“反序列化 JSON”活动之后。
    • 在“收件人”字段中添加“LanguageCode”变量。
    • 在“值”字段添加表达式 ResultJSON("data")("detections")(0)("language").ToString
  11. “分配”活动添加到上一个“分配”活动之后。
    • 在“收件人”字段中添加“LanguageName”变量。
    • 在“值”字段添加表达式 LanguagesDictionary(LanguageCode)
  12. 将另一个“分配”活动添加到上一个“分配”活动之后。
    • 在“收件人”字段中添加“Reliable”变量。
    • 在“值”字段添加表达式 CBool(ResultJSON("data")("detections")(0)("isReliable").ToString)
  13. “分配”活动添加到上一个“分配”活动之后。
    • 在“收件人”字段中添加“Confidence”变量。
    • 在“值”字段添加表达式 CDbl(ResultJSON("data")("detections")(0)("confidence").ToString)
  14. “分配”活动后添加“日志消息”
    • 在“行”字段中添加表达式“"Detection for the text:" + vbCrLf + " Language is " + LanguageName+ vbCrLf +" Reliable detection: " + Reliable.ToString + vbCrLf + " Confidence level: " + Confidence.ToString”。
  15. Add a Log Message activity inside the Else field of the If activity.
    • Select the Warn option from the Level drop-down list.
    • 在“行”字段中添加表达式“"HTTP Request was not successful. Code: " + StatusCode.ToString”。
  16. “日志消息”活动拖动至上一个“日志消息”活动之后。
    • Select the Warn option from the Level drop-down list.

    • 在“行”字段中添加表达式“"HTTP Request was not successful. Result: " + Result”。

  17. 运行流程以检测并记录文本中使用的语言。
检测一批文本中的语言

此自动化流程的构建方法如下:

  1. 打开 Studio ,创建一个新“流程”
  2. 将“序列”容器拖动至“工作流设计器”
    • 创建以下变量:

      变量名称变量类型默认值
      APIKey字符串
      Texts字符串[]
      Result字符串
      StatusCodeInt32
      HTTPRequestBodyJSON字符串
  3. 分配活动添加至“序列”容器。
    • 在“收件人”字段中添加“APIKey”变量。
    • Add the value "demo" in the Value field.
  4. 将另一个“分配”活动添加到上一个“分配”活动之后。
    • 在“收件人”字段中添加“Texts”变量。
    • Add the value {"Hello world.", "Buenos dias, señor.", "Guten Tag.", "Buna ziua, tuturor."} in the Value field.
  5. 将新的“分配”活动添加到上一个“分配”活动之后。
    • 在“收件人”字段中添加“HTTPRequestBodyJSON”变量。
    • Add the value Newtonsoft.Json.JsonConvert.SerializeObject(new with{ .q = Texts }) in the Value field.
  6. “HTTP 请求”活动添加到“分配”活动之后。
    • 通过添加 端点 地址、 请求方法 接受响应 形式和 身份验证 方法来配置 向导

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

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

    • 在“结果”字段中添加 Result 变量。

    • Add the variable StatusCode in the StatusCode field.

  7. “HTTP 请求”活动之后添加“ IF 条件”活动。
    • 在“条件”字段中添加表达式“StatusCode = 200”。
  8. Add a Sequence activity inside the Then field of the If activity.
    • 创建以下变量:

      变量名称变量类型默认值
      ResultJSONJObject
      LanguagesDictionarySystem.Collections.Generic.Dictionary<System.String, System.String>
      LanguageCode字符串
      LanguageName字符串
      Reliable布尔值
      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:

      参数名称参数方向参数类型默认值
      out_LanguagesDictionary输出Dictionary<String, String>LanguagesDictionary

  10. “反序列化 JSON”活动添加到“调用工作流文件”活动之后。
    • Add the variable Result in the Json String field.
  11. “遍历循环”活动添加到“反序列化 JSON”活动之后。
    • Add the expression ResultJSON("data")("detections") in the Values field.
  12. Add an Assign activity inside the Body of the For Each activity.
    • 在“收件人”字段中添加“LanguageCode”变量。
    • 在“值”字段添加表达式 item(0)("language").ToString
  13. “分配”活动添加到上一个“分配”活动之后。
    • 在“收件人”字段中添加“LanguageName”变量。
    • 在“值”字段添加表达式 LanguagesDictionary(LanguageCode)
  14. 将另一个“分配”活动添加到上一个“分配”活动之后。
    • 在“收件人”字段中添加“Reliable”变量。
    • 在“值”字段添加表达式 CBool(item(0)("isReliable").ToString)
  15. “分配”活动添加到上一个“分配”活动之后。
    • 在“收件人”字段中添加“Confidence”变量。
    • 在“值”字段添加表达式 CDbl(item(0)("confidence").ToString)
  16. “分配”活动后添加“日志消息”
    • 添加表达式 “检测文本 #”+index.ToString +“:” + vbCRLf +“语言为”+语言名称+ vbCRLf +“可靠的检测:”
      • Reliable.ToString + vbCRLf +“置信度级别:” + “消息”字段中的 confidence.ToString`。
  17. Add a Log Message activity inside the Else field of the If activity.
    • Select the Warn option from the Level drop-down list.
    • 在“行”字段中添加表达式“"HTTP Request was not successful. Code: " + StatusCode.ToString”。
  18. “日志消息”活动拖动至上一个“日志消息”活动之后。
    • Select the Warn option from the Level drop-down list.

    • 在“行”字段中添加表达式“"HTTP Request was not successful. Result: " + Result”。

  19. 运行流程以检测并记录文本中使用的语言

您可在此处下载示例。

    此页面有帮助吗?

    连接

    需要帮助? 支持

    想要了解详细内容? UiPath Academy

    有问题? UiPath 论坛

    保持更新