日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

阿里云文本反垃圾检测接口调用

發布時間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 阿里云文本反垃圾检测接口调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

阿里云文檔
開發準備文檔

調用阿里云文本反垃圾檢測接口時報detect not success. code:596
這里是由于沒有授權。正確的user創建過程:

  • 復制ID和密碼
  • 編程訪問打勾
  • 返回用戶列表頁時,點擊授權,權限為AliyunYundunGreenWebFullAccess
  • Java代碼

    import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.green.model.v20180509.TextScanRequest; import com.aliyuncs.http.FormatType; import com.aliyuncs.http.HttpResponse; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile;import java.util.*;public class Test {public static void main(String[] args) throws Exception {IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "自己的ID","自己的secret");IAcsClient client = new DefaultAcsClient(profile);TextScanRequest textScanRequest = new TextScanRequest();textScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式textScanRequest.setHttpContentType(FormatType.JSON);textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定請求方法textScanRequest.setEncoding("UTF-8");textScanRequest.setRegionId("cn-shanghai");List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();Map<String, Object> task1 = new LinkedHashMap<String, Object>();task1.put("dataId", UUID.randomUUID().toString());/*** 待檢測的文本,長度不超過10000個字符*/task1.put("content", "陳杭是多利羊");tasks.add(task1);JSONObject data = new JSONObject();/*** 檢測場景,文本垃圾檢測傳遞:antispam**/data.put("scenes", Arrays.asList("antispam"));data.put("tasks", tasks);System.out.println(JSON.toJSONString(data, true));textScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);// 請務必設置超時時間textScanRequest.setConnectTimeout(3000);textScanRequest.setReadTimeout(6000);try {HttpResponse httpResponse = client.doAction(textScanRequest);if(httpResponse.isSuccess()){JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));System.out.println(JSON.toJSONString(scrResponse, true));if (200 == scrResponse.getInteger("code")) {JSONArray taskResults = scrResponse.getJSONArray("data");for (Object taskResult : taskResults) {if(200 == ((JSONObject)taskResult).getInteger("code")){JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");for (Object sceneResult : sceneResults) {String scene = ((JSONObject)sceneResult).getString("scene");String suggestion = ((JSONObject)sceneResult).getString("suggestion");//根據scene和suggetion做相關處理//suggestion == pass 未命中垃圾 suggestion == block 命中了垃圾,可以通過label字段查看命中的垃圾分類System.out.println("args = [" + scene + "]");System.out.println("args = [" + suggestion + "]");}}else{System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));}}} else {System.out.println("detect not success. code:" + scrResponse.getInteger("code"));}}else{System.out.println("response not success. status:" + httpResponse.getStatus());}} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}

    pom.xml依賴

    <dependencies><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>3.5.0</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-green</artifactId><version>3.5.0</version></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.10</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.7</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency><dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.2.1</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-csv</artifactId><version>1.3</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency></dependencies>

    總結

    以上是生活随笔為你收集整理的阿里云文本反垃圾检测接口调用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。