This topic describes how to use Content Moderation SDK for Java to call text anti-spam operations and moderate text for spam such as pornography and terrorist content.

Background information

Only synchronous moderation is supported for text anti-spam operations.

You can send a request to moderate one or more text entries. You are charged based on the number of text entries that are moderated. For more information, see Billing methods.

Before you begin

Before you call operations, make the following preparations:
  • Create an AccessKey pair for your Alibaba Cloud account. For more information, see Create an AccessKey pair.
  • Install Java dependencies. For more information, see Installation.
  • Optional:Download and import the Extension.Uploader utility class into your project if you submit an on-premises image or a binary image stream for image moderation.

Moderate text for spam

Text anti-spam allows you to add custom terms, such as the brand terms of competitors. If the moderated text contains the terms you add, the value of the suggestion parameter is block in the returned machine-assisted moderation results.

You can add terms in the Alibaba Cloud Content Moderation console or by calling the corresponding operation. The terms are UTF-8-encoded. For more information, see Text moderation.

Operation Description Supported region
TextScanRequest Submits text moderation tasks. Set the scenes parameter to antispam.
  • cn-shanghai: China (Shanghai)
  • cn-beijing: China (Beijing)
  • cn-shenzhen: China (Shenzhen)
  • ap-southeast-1: Singapore (Singapore)
Sample code
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 java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        IClientProfile profile = DefaultProfile
            .getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
        DefaultProfile
            .addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");        
        IAcsClient client = new DefaultAcsClient(profile);
        TextScanRequest textScanRequest = new TextScanRequest();
        textScanRequest.setAcceptFormat(FormatType.JSON); // Specify the response format of the operation. 
        textScanRequest.setHttpContentType(FormatType.JSON);
        textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // Specify the request method. 
        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());
        /**
         * The text to be moderated, which can be up to 10,000 characters in length. 
         */
        task1.put("content", "test content");
        tasks.add(task1);
        JSONObject data = new JSONObject();

        /**
         * The moderation scenario. Set the scenes parameter to 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);
        // Specify the connection timeout and read timeout. 
        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");
                                // Take a further action on the text based on the values of the scene and suggestion parameters. 
                                // If the value of the suggestion parameter is pass, no spam is detected. If the value of the suggestion parameter is block, spam is detected. In this case, check the value of the label parameter to obtain the spam category. 
                                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();
        }
    }

}

Provide feedback on text anti-spam results

If a text anti-spam result does not meet your expectations, you can call the TextFeedbackRequest operation to provide feedback on the machine-assisted moderation result.

The server corrects the text anti-spam result based on your feedback and adds the feedback to a text pattern blacklist or whitelist. When you submit a text pattern next time, the server returns the text anti-spam result based on the label parameter that you specified by calling the TextFeedbackRequest operation. For more information about the operation, see Give feedback on moderation results.

Operation Description Supported region
TextFeedbackRequest Provides feedback on a text anti-spam result to correct the machine-assisted moderation result that does not meet your expectations.
  • cn-shanghai
  • cn-beijing
  • cn-shenzhen
  • ap-southeast-1
Sample code
import com.alibaba.fastjson.JSON;
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.TextFeedbackRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class Main {

    public static void main(String[] args) throws Exception {
        IClientProfile profile = DefaultProfile
            .getProfile("cn-shanghai", "Your AccessKey ID", "Your AccessKey secret");
        DefaultProfile
            .addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        IAcsClient client = new DefaultAcsClient(profile);
        TextFeedbackRequest textFeedbackRequest = new TextFeedbackRequest();
        textFeedbackRequest.setAcceptFormat(FormatType.JSON); // Specify the response format of the operation. 
        textFeedbackRequest.setHttpContentType(FormatType.JSON);
        textFeedbackRequest.setMethod(com.aliyuncs.http.MethodType.POST); // Specify the request method. 
        textFeedbackRequest.setEncoding("UTF-8");
        textFeedbackRequest.setRegionId("cn-shanghai");

        JSONObject data = new JSONObject();
        data.put("taskId", "txt6z3Na17XbrD4P7QdJzYbk1-1q4seJ");
        data.put("label", "spam");
        System.out.println(JSON.toJSONString(data, true));
        textFeedbackRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        // Specify the connection timeout and read timeout. 
        textFeedbackRequest.setConnectTimeout(3000);
        textFeedbackRequest.setReadTimeout(6000);
        try {
            HttpResponse httpResponse = client.doAction(textFeedbackRequest);
            if(httpResponse.isSuccess()){
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
            }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();
        }
    }

}