Updated on 2025-07-02 GMT+08:00

Text Moderation (V3)

  1. This section describes how to use the Text Moderation SDK by AK/SK authentication.

    In the sample code, you can use the withText method of the TextDetectionDataReq class to configure the text to be detected. After the configuration is complete, run the code.

    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.core.region.Region;
    import com.huaweicloud.sdk.moderation.v3.*;
    import com.huaweicloud.sdk.moderation.v3.model.*;
    
    public class RunTextModerationSolution {
    
        public static void main(String[] args) {
           // Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
           // In this example, the AK and SK are stored in environment variables for identity authentication. Before running this example, configure environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
            String ak = System.getenv("HUAWEICLOUD_SDK_AK");
            String sk = System.getenv("HUAWEICLOUD_SDK_SK");
    
            ICredential auth = new BasicCredentials()
                    .withAk(ak)
                    .withSk(sk);
    
            ModerationClient client = ModerationClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(ModerationRegion.valueOf("xxx"))  //Replace xxx with the service region, for example, ap-southeast-3.
                    .build();
            RunTextModerationRequest request = new RunTextModerationRequest();
            TextDetectionReq body = new TextDetectionReq();
            TextDetectionDataReq databody = new TextDetectionDataReq();
            databody.withText("test");
            body.withData(databody);
            body.withEventType("comment");
            request.withBody(body);
            try {
                RunTextModerationResponse response = client.runTextModeration(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
  2. If 200 is displayed on the console, the code is successfully executed. The text moderation result is displayed on the console.
    class RunTextModerationResponse {
        requestId: 308b6ad2740e51de73597da9fdc94ee1
        result: class TextDetectionResult {
            suggestion: pass
            label: normal
            details: []
        }
    }