- 概述
- 加密
- 数据库
- Java
- Python
- 网页 API
开发者活动
以下示例说明了如何使用“HTTP 请求”或“反序列化 JSON”等活动来调用公共 API、提取信息以及显示结果。您可以在“UiPath.WebAPI.Activities ”包中找到这些活动。
使用语言检测 API 检测文本中的语言可通过三个工作流进行。 第一个工作流创建语言字典,第二个工作流检测单个文本中的语言,第三个工作流检测多个文本中的语言。
创建语言字典
工作流从包含语言代码和名称的 CSV 文件创建语言字典。 此工作流在其他两个工作流中调用。
此自动化流程的构建方法如下:
- 打开 Studio ,创建一个新“流程”。
- Download the archive with the project in this example and copy the file languages.csv to your project folder.
- 将“序列”容器拖动至“工作流设计器”。
-
创建以下变量:
变量名称 变量类型 默认值 LanguagesDT数据表
-
- Add a Read CSV activity inside the Sequence container.
- Add the expression
"languages.csv"in the FilePath field. - 在“属性”面板中,向“数据表”字段添加“
LanguagesDT”变量。
- Add the expression
- 将“分配”活动添加到“读取 CSV”活动之后。
-
Add the expression
out_LanguagesDictionaryin the To field. -
在“值”字段添加表达式
LanguagesDT.AsEnumerable.ToDictionary(of string, string)(function(row) row("Code").ToString, function(row) row("Language").ToString)。
-
- 运行该流程以创建字典。
检测单个文本中的语言
此自动化流程的构建方法如下:
- 打开 Studio ,创建一个新“流程”。
- 将“序列”容器拖动至“工作流设计器”。
-
创建以下变量:
变量名称 变量类型 默认值 APIKey字符串 Text字符串 Result字符串 StatusCodeInt32 LanguagesDictionaryDictionary<String, String> LanguageCode字符串 Reliable布尔值 ConfidenceDouble LanguageName字符串
-
- 在“序列”容器中添加“分配”活动。
- 在“收件人”字段中添加“
APIKey”变量。 - 在“值”字段添加表达式
"demo"。
- 在“收件人”字段中添加“
- 将“分配”活动添加到“分配”活动之后。
- 在“收件人”字段中添加“
Text”变量。 - 在“值”字段添加表达式
"Hello. This is a sample test."。
- 在“收件人”字段中添加“
- 将“HTTP 请求”活动添加到“分配”活动之后。
-
通过添加 端点 地址、 请求方法 、 接受响应表单 和 身份验证 方法来配置 向导 。
-
In the Properties panel, add the value
application/jsonin the BodyFormat field. NOTE: the workflow shows application/xml. -
在“结果”字段中添加
Result变量。 -
Add the variable
StatusCodein the StatusCode field.
-
- 在“HTTP 请求”活动之后添加“ IF 条件”活动。
- 在“条件”字段中添加表达式“
StatusCode = 200”。
- 在“条件”字段中添加表达式“
- Add a Sequence activity inside the Then field of the If activity.
-
创建以下变量:
变量名称 变量类型 默认值 ResultJSONJObject
-
- 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
-
- 将“反序列化 JSON”活动添加到“调用工作流文件”活动之后。
- Add the variable
Resultin the Json String field.
- Add the variable
- 将“分配”活动添加到“反序列化 JSON”活动之后。
- 在“收件人”字段中添加“
LanguageCode”变量。 - 在“值”字段添加表达式
ResultJSON("data")("detections")(0)("language").ToString。
- 在“收件人”字段中添加“
- 将“分配”活动添加到上一个“分配”活动之后。
- 在“收件人”字段中添加“
LanguageName”变量。 - 在“值”字段添加表达式
LanguagesDictionary(LanguageCode)。
- 在“收件人”字段中添加“
- 将另一个“分配”活动添加到上一个“分配”活动之后。
- 在“收件人”字段中添加“
Reliable”变量。 - 在“值”字段添加表达式
CBool(ResultJSON("data")("detections")(0)("isReliable").ToString)。
- 在“收件人”字段中添加“
- 将“分配”活动添加到上一个“分配”活动之后。
- 在“收件人”字段中添加“
Confidence”变量。 - 在“值”字段添加表达式
CDbl(ResultJSON("data")("detections")(0)("confidence").ToString)。
- 在“收件人”字段中添加“
- 在“分配”活动后添加“日志消息” 。
- 在“行”字段中添加表达式“
"Detection for the text:" + vbCrLf + " Language is " + LanguageName+ vbCrLf +" Reliable detection: " + Reliable.ToString + vbCrLf + " Confidence level: " + Confidence.ToString”。
- 在“行”字段中添加表达式“
- 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”。
- 将“日志消息”活动拖动至上一个“日志消息”活动之后。
-
Select the Warn option from the Level drop-down list.
-
在“行”字段中添加表达式“
"HTTP Request was not successful. Result: " + Result”。
-
- 运行流程以检测并记录文本中使用的语言。
检测一批文本中的语言
此自动化流程的构建方法如下:
- 打开 Studio ,创建一个新“流程”。
- 将“序列”容器拖动至“工作流设计器”。
-
创建以下变量:
变量名称 变量类型 默认值 APIKey字符串 Texts字符串[] Result字符串 StatusCodeInt32 HTTPRequestBodyJSON字符串
-
- 将分配活动添加至“序列”容器。
- 在“收件人”字段中添加“
APIKey”变量。 - Add the value
"demo"in the Value field.
- 在“收件人”字段中添加“
- 将另一个“分配”活动添加到上一个“分配”活动之后。
- 在“收件人”字段中添加“
Texts”变量。 - Add the value
{"Hello world.", "Buenos dias, señor.", "Guten Tag.", "Buna ziua, tuturor."}in the Value field.
- 在“收件人”字段中添加“
- 将新的“分配”活动添加到上一个“分配”活动之后。
- 在“收件人”字段中添加“
HTTPRequestBodyJSON”变量。 - Add the value
Newtonsoft.Json.JsonConvert.SerializeObject(new with{ .q = Texts })in the Value field.
- 在“收件人”字段中添加“
- 将“HTTP 请求”活动添加到“分配”活动之后。
-
通过添加 端点 地址、 请求方法 、 接受响应 形式和 身份验证 方法来配置 向导 。
-
In the Properties panel, add the variable
HTTPRequestBodyJSONin the Body field. -
Add the value
application/jsonin the BodyFormat field. -
在“结果”字段中添加
Result变量。 -
Add the variable
StatusCodein the StatusCode field.
-
- 在“HTTP 请求”活动之后添加“ IF 条件”活动。
- 在“条件”字段中添加表达式“
StatusCode = 200”。
- 在“条件”字段中添加表达式“
- 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 indexInt32 0
-
- 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
-
- 将“反序列化 JSON”活动添加到“调用工作流文件”活动之后。
- Add the variable
Resultin the Json String field.
- Add the variable
- 将“遍历循环”活动添加到“反序列化 JSON”活动之后。
- Add the expression
ResultJSON("data")("detections")in the Values field.
- Add the expression
- Add an Assign activity inside the Body of the For Each activity.
- 在“收件人”字段中添加“
LanguageCode”变量。 - 在“值”字段添加表达式
item(0)("language").ToString。
- 在“收件人”字段中添加“
- 将“分配”活动添加到上一个“分配”活动之后。
- 在“收件人”字段中添加“
LanguageName”变量。 - 在“值”字段添加表达式
LanguagesDictionary(LanguageCode)。
- 在“收件人”字段中添加“
- 将另一个“分配”活动添加到上一个“分配”活动之后。
- 在“收件人”字段中添加“
Reliable”变量。 - 在“值”字段添加表达式
CBool(item(0)("isReliable").ToString)。
- 在“收件人”字段中添加“
- 将“分配”活动添加到上一个“分配”活动之后。
- 在“收件人”字段中添加“
Confidence”变量。 - 在“值”字段添加表达式
CDbl(item(0)("confidence").ToString)。
- 在“收件人”字段中添加“
- 在“分配”活动后添加“日志消息” 。
- 添加表达式 “检测文本 #”+index.ToString +“:” + vbCRLf +“语言为”+语言名称+ vbCRLf +“可靠的检测:”
- Reliable.ToString + vbCRLf +“置信度级别:” + “消息”字段中的 confidence.ToString`。
- 添加表达式 “检测文本 #”+index.ToString +“:” + vbCRLf +“语言为”+语言名称+ vbCRLf +“可靠的检测:”
- 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”。
- 将“日志消息”活动拖动至上一个“日志消息”活动之后。
-
Select the Warn option from the Level drop-down list.
-
在“行”字段中添加表达式“
"HTTP Request was not successful. Result: " + Result”。
-
- 运行流程以检测并记录文本中使用的语言
您可在此处下载示例。