-
Notifications
You must be signed in to change notification settings - Fork 11
/
TextCheckApiDemoV5.cs
233 lines (225 loc) · 14.4 KB
/
TextCheckApiDemoV5.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;
namespace Com.Netease.Is.Antispam.Demo
{
class TextCheckApiDemoV5
{
public static void textCheck()
{
/** 产品密钥ID,产品标识 */
String secretId = "your_secret_id";
/** 产品私有密钥,服务端生成签名信息使用,请严格保管,避免泄露 */
String secretKey = "your_secret_key";
/** 业务ID,易盾根据产品业务特点分配 */
String businessId = "your_business_id";
/** 易盾反垃圾云服务文本在线检测接口地址 */
String apiUrl = "http://as.dun.163.com/v5/text/check";
Dictionary<String, String> parameters = new Dictionary<String, String>();
long curr = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
String time = curr.ToString();
// 1.设置公共参数
parameters.Add("secretId", secretId);
parameters.Add("businessId", businessId);
parameters.Add("version", "v5");
parameters.Add("timestamp", time);
parameters.Add("nonce", new Random().Next().ToString());
// 2.设置私有参数
parameters.Add("dataId", "ebfcad1c-dba1-490c-b4de-e784c2691768");
parameters.Add("content", "易盾测试内容");
// parameters.Add("dataType", "1");
// parameters.Add("ip", "123.115.77.137");
// parameters.Add("account", "csharp@163.com");
// parameters.Add("deviceType", "4");
// parameters.Add("deviceId", "92B1E5AA-4C3D-4565-A8C2-86E297055088");
// parameters.Add("callback", "ebfcad1c-dba1-490c-b4de-e784c2691768");
// parameters.Add("publishTime", time);
// 3.生成签名信息
String signature = Utils.genSignature(secretKey, parameters);
parameters.Add("signature", signature);
// 4.发送HTTP请求
HttpClient client = Utils.makeHttpClient();
String result = Utils.doPost(client, apiUrl, parameters, 1000);
Console.WriteLine(result);
if(result != null)
{
JObject ret = JObject.Parse(result);
int code = ret.GetValue("code").ToObject<Int32>();
String msg = ret.GetValue("msg").ToObject<String>();
if (code == 200)
{
JObject resultObject = (JObject)ret["result"];
if(null != resultObject["antispam"]){
JObject antispam = resultObject["antispam"].ToObject<JObject>();
if(null != antispam){
String taskId = antispam["taskId"].ToObject<String>();
String dataId = antispam["dataId"].ToObject<String>();
int suggestion = antispam["suggestion"].ToObject<int>();
int resultType = antispam["resultType"].ToObject<int>();
int censorType = antispam["censorType"].ToObject<int>();
Boolean isRelatedHit = antispam["isRelatedHit"].ToObject<Boolean>();
JArray labels = (JArray)antispam.SelectToken("labels");
Console.WriteLine(String.Format("内容安全结果,taskId: {0},dataId: {1},suggestion: {2}", taskId, dataId, suggestion));
foreach (var labelElement in labels)
{
JObject labelItem = (JObject)labelElement;
int label = labelItem.GetValue("label").ToObject<Int32>();
int level = labelItem.GetValue("level").ToObject<Int32>();
JArray subLabels = (JArray)labelItem.SelectToken("subLabels");
if(null != subLabels){
foreach (var subLabelElement in subLabels){
JObject subLabelItem = (JObject)subLabelElement;
String subLabel = subLabelItem.GetValue("subLabel").ToObject<String>();
Console.WriteLine(String.Format("内容安全分类,label: {0},subLabel: {1}", label, subLabel));
if(null != subLabelItem["details"]){
JObject details = subLabelItem["details"].ToObject<JObject>();
// 自定义敏感词信息
if(null != details["keywords"]){
JArray keywords = (JArray)antispam.SelectToken("keywords");
if(null != keywords){
foreach (var keywordElement in keywords){
JObject keywordItem = (JObject)keywordElement;
String word = keywordItem["word"].ToObject<String>();
}
}
}
// 自定义名单库信息
if(null != details["libInfos"]){
JArray libInfos = (JArray)antispam.SelectToken("libInfos");
if(null != libInfos){
foreach (var libInfoElement in libInfos){
JObject libInfoItem = (JObject)libInfoElement;
int type = libInfoItem["type"].ToObject<int>();
String entity = libInfoItem["entity"].ToObject<String>();
}
}
}
// 线索信息
if(null != details["hitInfos"]){
JArray hitInfos = (JArray)antispam.SelectToken("hitInfos");
if(null != hitInfos){
foreach (var hitInfoElement in hitInfos){
JObject hitInfoItem = (JObject)hitInfoElement;
String value = hitInfoItem["value"].ToObject<String>();
JArray positions = (JArray)hitInfoItem.SelectToken("positions");
if(null != positions){
foreach (var positionElement in positions){
JObject positionItem = (JObject)positionElement;
String fieldName = positionItem["fieldName"].ToObject<String>();
int startPos = positionItem["startPos"].ToObject<int>();
int endPos = positionItem["endPos"].ToObject<int>();
}
}
}
}
}
// 反作弊信息
if(null != details["anticheat"]){
JObject anticheat = details["anticheat"].ToObject<JObject>();
if(null != anticheat){
int type = anticheat["type"].ToObject<int>();
}
}
}
}
}
}
}
}
// 情感分析结果
if(null != resultObject["emotionAnalysis"]){
JObject emotionAnalysis = resultObject["emotionAnalysis"].ToObject<JObject>();
if(null != emotionAnalysis){
String taskId = emotionAnalysis["taskId"].ToObject<String>();
String dataId = emotionAnalysis["dataId"].ToObject<String>();
if (null != emotionAnalysis["details"]) {
JArray details = (JArray)emotionAnalysis.SelectToken("details");
Console.WriteLine(String.Format("情感分析结果,taskId: {0},dataId: {1},details: {2}", taskId, dataId, details));
if (details != null) {
foreach (var detailElement in details){
JObject detailItem = (JObject)detailElement;
double positiveProb = detailItem["positiveProb"].ToObject<double>();
double negativeProb = detailItem["negativeProb"].ToObject<double>();
String sentiment = detailItem["sentiment"].ToObject<String>();
}
}
}
}
}
// 反作弊结果
if(null != resultObject["anticheat"]){
JObject anticheat = resultObject["anticheat"].ToObject<JObject>();
if(null != anticheat){
String taskId = anticheat["taskId"].ToObject<String>();
String dataId = anticheat["dataId"].ToObject<String>();
if (null != anticheat["details"]) {
JArray details = (JArray)anticheat.SelectToken("details");
Console.WriteLine(String.Format("反作弊结果,taskId: {0},dataId: {1},details: {2}", taskId, dataId, details));
if (details != null) {
foreach (var detailElement in details){
JObject detailItem = (JObject)detailElement;
int suggestion = detailItem["suggestion"].ToObject<int>();
JArray hitInfos = (JArray)detailItem.SelectToken("hitInfos");
if(null != hitInfos){
foreach (var hitInfoElement in hitInfos){
JObject hitInfoItem = (JObject)hitInfoElement;
int hitType = hitInfoItem["hitType"].ToObject<int>();
String hitMsg = hitInfoItem["hitMsg"].ToObject<String>();
}
}
}
}
}
}
}
// 用户画像结果
if(null != resultObject["userRisk"]){
JObject userRisk = resultObject["userRisk"].ToObject<JObject>();
if(null != userRisk){
String taskId = userRisk["taskId"].ToObject<String>();
String dataId = userRisk["dataId"].ToObject<String>();
if (null != userRisk["details"]) {
JArray details = (JArray)userRisk.SelectToken("details");
Console.WriteLine(String.Format("用户画像结果,taskId: {0},dataId: {1},details: {2}", taskId, dataId, details));
if (details != null) {
foreach (var detailElement in details){
JObject detailItem = (JObject)detailElement;
String account = detailItem["account"].ToObject<String>();
int accountLevel = detailItem["accountLevel"].ToObject<int>();
}
}
}
}
}
// 语种检测结果
if(null != resultObject["language"]){
JObject language = resultObject["language"].ToObject<JObject>();
if(null != language){
String taskId = language["taskId"].ToObject<String>();
String dataId = language["dataId"].ToObject<String>();
if (null != language["details"]) {
JArray details = (JArray)language.SelectToken("details");
Console.WriteLine(String.Format("语种检测结果,taskId: {0},dataId: {1},details: {2}", taskId, dataId, details));
if (details != null) {
foreach (var detailElement in details){
JObject detailItem = (JObject)detailElement;
String type = detailItem["type"].ToObject<String>();
}
}
}
}
}
}
else
{
Console.WriteLine(String.Format("ERROR: code={0}, msg={1}", code, msg));
}
}
else
{
Console.WriteLine("Request failed!");
}
}
}
}