0
点赞
收藏
分享

微信扫一扫

阿里云文本垃圾检测

SPEIKE 2022-02-20 阅读 46
java

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

 

 

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;

import com.aliyun.oss.ClientException;

import com.aliyuncs.DefaultAcsClient;

import com.aliyuncs.IAcsClient;

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 lombok.Data;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.context.annotation.Configuration;

import java.util.*;

/**

* 功能:阿里云文本垃圾检测

*/

@Configuration

@ConfigurationProperties(prefix = "app")

@RefreshScope

@Data

public class ALiYunCheckText {

/**

* 阿里云 AccessKey ID 和 AccessKey Secret

*/

private String accessKeyID;

private String accessKeySecret;

/**

* 日志信息

*/

private Logger logger = LoggerFactory.getLogger(ALiYunCheckText.class);

public Map checkText(String content) throws Exception {

IClientProfile profile = DefaultProfile

.getProfile("cn-shanghai", accessKeyID, accessKeySecret);

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);

logger.info(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"));

logger.info(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==review表示人工审核、suggestion==block表示命中了垃圾,可以通过label字段查看命中的垃圾分类。

logger.info("args = [" + scene + "]");

logger.info("args = [" + suggestion + "]");

Map<String, String> map = new HashMap<>();

map.put("results",taskResults.toJSONString());

map.put("suggestion",suggestion);

return map;

}

} else {

logger.info("task process fail:" + ((JSONObject) taskResult).getInteger("code"));

}

}

} else {

logger.info("detect not success. code:" + scrResponse.getInteger("code"));

}

} else {

logger.info("response not success. status:" + httpResponse.getStatus());

}

} catch (ServerException e) {

logger.error(e.getMessage());

} catch (ClientException e) {

logger.error(e.getMessage());

} catch (Exception e) {

logger.error(e.getMessage());

}

return null;

}

}

举报

相关推荐

0 条评论