阿里雲AI文字識別
阿新 • • 發佈:2021-09-22
阿里雲文件位置: https://help.aliyun.com/document_detail/53427.html
返回引數
執行完畢只有判斷返回的引數是否正確 然後返回給使用者即可
if (suggestion.equals("pass")) { return ArticleStatusEnums.audit_success.getType(); } else if (suggestion.equals("review")) { return ArticleStatusEnums.manual_audit.getType(); } else if (suggestion.equals("block")) { return ArticleStatusEnums.audit_failed.getType(); }
@Component public class ContentDetectionUtils { @Autowired private AliyunResources aliyunResources; public Integer contentDetection(String content) { IClientProfile profile = DefaultProfile .getProfile("cn-shanghai", aliyunResources.getAccessKeyId(), aliyunResources.getAccessKeySecret()); DefaultProfile .addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com"); 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", 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(StandardCharsets.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 + "]"); if (suggestion.equals("pass")) { return ArticleStatusEnums.audit_success.getType(); } else if (suggestion.equals("review")) { return ArticleStatusEnums.manual_audit.getType(); } else if (suggestion.equals("block")) { return ArticleStatusEnums.audit_failed.getType(); } } }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(); } return null; } }
當進行文章插入時 呼叫工具工具類 檢測 然後將文章狀態更新即可
Integer contentDetection = contentDetectionUtils.contentDetection(articleBO.getContent()); if (Objects.equals(contentDetection, ArticleStatusEnums.audit_failed.getType())) { article.setArticleStatus(ArticleStatusEnums.audit_failed.getType()); } else if (Objects.equals(contentDetection, ArticleStatusEnums.audit_success.getType())) { article.setArticleStatus(ArticleStatusEnums.audit_success.getType()); } else if (Objects.equals(contentDetection, ArticleStatusEnums.manual_audit.getType())) { article.setArticleStatus(ArticleStatusEnums.audit_success.getType()); } this.updateArticleStatus(article);
完整service
public void addArticle(NewArticleBO articleBO, Category category) {
Article article = new Article();
BeanUtils.copyProperties(articleBO, article);
article.setArticleStatus(ArticleStatusEnums.auditing.getType());
// 設定初始值
article.setReadCounts(0);
article.setCommentCounts(0);
article.setIsDelete(YesOrNoEnums.NO.getType());
article.setCreateTime(new Date());
article.setUpdateTime(new Date());
article.setCategoryId(category.getId());
// 判斷定時任務型別
if (Objects.equals(articleBO.getIsAppoint(), AppointStatusEnums.regular_update.getType())) {
article.setPublishTime(articleBO.getPublishTime());
} else {
article.setPublishTime(new Date());
}
// 進行入庫操作
int result = articleMapper.insert(article);
if (result != 1) {
GraceException.display(ResponseStatusEnum.ARTICLE_CREATE_ERROR);
}
Integer contentDetection = contentDetectionUtils.contentDetection(articleBO.getContent());
if (Objects.equals(contentDetection, ArticleStatusEnums.audit_failed.getType())) {
article.setArticleStatus(ArticleStatusEnums.audit_failed.getType());
} else if (Objects.equals(contentDetection, ArticleStatusEnums.audit_success.getType())) {
article.setArticleStatus(ArticleStatusEnums.audit_success.getType());
} else if (Objects.equals(contentDetection, ArticleStatusEnums.manual_audit.getType())) {
article.setArticleStatus(ArticleStatusEnums.audit_success.getType());
}
this.updateArticleStatus(article);
}