Demo Project of Text Moderation

This demo project corresponds to the POST /v1.0/moderation/text URI. Replace the AK/SK information with the actual AK/SK to run the demo.

Sample Code

  1. Configure the AK and SK pair and region information for accessing a service in the ModerationTextContentDemo.java file.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    // 1. Configure the basic information for accessing Text Moderation and generate a client connection object.
    AisAccess service = ServiceAccessBuilder.builder()		
            .ak("######")                       // your ak
            .sk("######")                       // your sk
           .region("cn-north-4")               // Content Moderation in CN North-Beijing4 (cn-north-4) and CN East-Shanghai1 (cn-east-3)
            .connectionTimeout(5000)            // Timeout limit for connecting to the target URL
            .connectionRequestTimeout(1000)     // Timeout limit for obtaining available connections from the connection pool
            .socketTimeout(20000)               // Timeout limit for obtaining server response data
            .build();
    
  2. Enter the text that you want to check in the ModerationTextContentDemo.java file, for example, 6666666666.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    //
    // 2. Construct the parameters required for accessing Text Moderation.
    //
    String uri = "/v1.0/moderation/text";
    			
    JSONObject json = new JSONObject();
    json.put("categories", new String[] {"porn","politics","flood"}) //Text for moderation
    
    JSONObject text = new JSONObject();
    text.put("text", "6666666666");
    text.put("type", "content");
    			
    JSONArray items = new JSONArray();
    items.add(text);			
    json.put("items", items);			
    
    StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8");
    
    // 3. Input the URI and required parameters of Text Moderation.
    // Input the parameters in JSON objects and call the service using POST.
    HttpResponse response = service.post(uri, stringEntity);
    
    // 4. Check whether the API call is successful. If 200 is returned, the API call succeeds. Otherwise, it fails.
    ResponseProcessUtils.processResponseStatus(response);
    
  3. Execute the ModerationTextContentDemo.java file. If 200 is displayed on the console, the program is successfully executed. The text moderation result is displayed on the console. See Figure 1.
    Figure 1 Execution result