This topic describes how to use Content Moderation SDK for Java to moderate text for spam such as pornographic and terrorist content.

Background information

Only synchronous moderation is supported for the text anti-spam operation. For more information about parameters, see Synchronous text moderation.

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 Overview.

Prerequisites

  • Java dependencies are installed. For more information, see Installation.
    Note You must use the required Java version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
  • The Extension.Uploader utility class is downloaded and imported into your project if you submit a local 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 result.

You can add terms in the Alibaba Cloud Content Moderation console or by calling an API operation.

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 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 use TextFeedbackRequest 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 text that meets the text pattern next time, the server returns the text anti-spam result based on the label parameter that you specified by using TextFeedbackRequest. For more information, 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.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.ImageScanFeedbackRequest;
import com.aliyuncs.green.model.v20180509.TextFeedbackRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.util.Arrays;


public class TextFeedbackSample {

    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();
        // Specify the response format of the operation. 
        textFeedbackRequest.setAcceptFormat(FormatType.JSON);
        // Specify the request method. 
        textFeedbackRequest.setMethod(MethodType.POST);
        textFeedbackRequest.setEncoding("utf-8");
        // Both HTTP and HTTPS are supported. 
        textFeedbackRequest.setProtocol(ProtocolType.HTTP);

        // label: The expected category of moderation results for the moderated text in the specified moderation scenario. 
        JSONObject httpBody = new JSONObject();
        httpBody.put("dataId", "ID of the moderated text");
        httpBody.put("taskId", "ID of the text moderation task");
        httpBody.put("content", "Text content");
        httpBody.put("label", "spam");
        httpBody.put("note", "Remarks");

        textFeedbackRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()),
                "UTF-8", FormatType.JSON);

        textFeedbackRequest.setConnectTimeout(3000);
        textFeedbackRequest.setReadTimeout(10000);
        try {
            HttpResponse httpResponse = client.doAction(textFeedbackRequest);

            if (httpResponse.isSuccess()) {
                JSONObject jsonObject = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(jsonObject, true));
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}