All Products
Search
Document Center

Content Moderation:Text anti-spam

Last Updated:Aug 07, 2023

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

Description

Only synchronous moderation is supported for text anti-spam. For more information about the related parameters, see /green/text/scan.

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

Prerequisites

  • Java dependencies are installed. For more information, see Installation.

    Note

    You must use the 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 that you have added, 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 with the scenes parameter set to antispam.

  • cn-shanghai: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: 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.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class Main {

    public static void main(String[] args) throws Exception {
        /**
         * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M.  
         * Common ways to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
        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 period and read timeout period. 
        textScanRequest.setConnectTimeout(3000);
        textScanRequest.setReadTimeout(6000);
        try {
            HttpResponse httpResponse = client.doAction(textScanRequest);
            if (!httpResponse.isSuccess()) {
                System.out.println("response not success. status:" + httpResponse.getStatus());
                // Business processing. 
                return;
            }
            JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
            System.out.println(JSON.toJSONString(scrResponse, true));
            if (200 != scrResponse.getInteger("code")) {
                System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                // Business processing. 
                return;
            }
            JSONArray taskResults = scrResponse.getJSONArray("data");
            for (Object taskResult : taskResults) {
                if (200 != ((JSONObject) taskResult).getInteger("code")) {
                    System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
                    // Business processing. 
                    continue;
                }
                JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
                for (Object sceneResult : sceneResults) {
                    String scene = ((JSONObject) sceneResult).getString("scene");
                    String suggestion = ((JSONObject) sceneResult).getString("suggestion");
                    // Take 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 + "]");
                }
            }
        } 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 text specified in your feedback to a text pattern blacklist or whitelist. When you submit text that matches the text pattern next time, the server returns the text anti-spam result based on the label parameter that you specified when calling the TextFeedbackRequest operation. For more information, see /green/text/feedback.

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: China (Shanghai)

  • cn-beijing: China (Beijing)

  • cn-shenzhen: China (Shenzhen)

  • ap-southeast-1: Singapore

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


public class TextFeedbackSample {

    public static void main(String[] args) throws Exception {
        /**
         * The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M.  
         * Common ways to obtain environment variables:
         * Method 1:
         *     Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * Method 2:
         *     Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
                "We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
        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()) {
                System.out.println("response not success. status:" + httpResponse.getStatus());
                // Business processing. 
                return;
            }
            JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
            int requestCode = scrResponse.getIntValue("code");
            if (200 != requestCode) {
                // The request failed to be processed. Analyze the failure cause based on the actual situation. 
                System.out.println("the whole scan request failed. response:" + JSON.toJSONString(scrResponse));
                // Business processing. 
                return;
            }
            // The call was successful. 
            System.out.println(JSON.toJSONString(scrResponse, true));
            // Business processing. 
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}