All Products
Search
Document Center

Content Moderation:Manage custom text libraries

Last Updated:Dec 13, 2023

This topic describes how to use Content Moderation SDK for PHP to manage custom text libraries. This meets your personalized requirements for text anti-spam.

Description

Based on the text type, text libraries are classified into term libraries and text pattern libraries. Based on the management purposes, text libraries are classified into whitelists, blacklists, and review lists. For more information about the related parameters, see DescribeKeywordLib.

Prerequisites

The dependencies for Content Moderation SDK for PHP are installed. For more information, see Installation.

Note

You must use the required PHP version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Query text libraries

  • Query term libraries

    <?php
    
    use AlibabaCloud\Client\AlibabaCloud;
    use AlibabaCloud\Client\Exception\ClientException;
    use AlibabaCloud\Client\Exception\ServerException;
    use AlibabaCloud\Green\Green;
    
    try {
        /**
         * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
         * Common ways to obtain environment variables:
         * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        AlibabaCloud::accessKeyClient('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')
            ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
            ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
            ->regionId('cn-shanghai')
            ->asDefaultClient();
    
        $response = Green::v20170823()->describeKeywordLib()
            ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
            ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request.
            ->withServiceModule('open_api')
            ->request();
        print_r($response);
        $allLibs = $response->KeywordLibList;
        $textAntispamKeywordLibs = array();
        foreach ($allLibs as $keywordLib) {
            $libType = $keywordLib->LibType;
            $resourceType = $keywordLib->ResourceType;
            $source = $keywordLib->Source;
            # Query custom term libraries. 
            if ("textKeyword" == $libType and "TEXT" == $resourceType and "MANUAL" == $source) {
                array_push($textAntispamKeywordLibs, $keywordLib);
            }
            # Query feedback-based term libraries. 
            if ("textKeyword" == $libType and "TEXT" == $resourceType and "FEEDBACK" == $source) {
                array_push($textAntispamKeywordLibs, $keywordLib);
            }
        }
        print_r($textAntispamKeywordLibs);
    } catch (ClientException $exception) {
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }
  • Query text pattern libraries, including custom text pattern libraries and feedback-based text pattern libraries

    <?php
    
    use AlibabaCloud\Client\AlibabaCloud;
    use AlibabaCloud\Client\Exception\ClientException;
    use AlibabaCloud\Client\Exception\ServerException;
    use AlibabaCloud\Green\Green;
    
    try {
        /**
         * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
         * Common ways to obtain environment variables:
         * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        AlibabaCloud::accessKeyClient('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')
            ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
            ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
            ->regionId('cn-shanghai')
            ->asDefaultClient();
    
        $response = Green::v20170823()->describeKeywordLib()
            ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
            ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request.
            ->withServiceModule('open_api')
            ->request();
        print_r($response->toArray());
        $allLibs = $response->KeywordLibList;
        $similarTextLibs = array();
        foreach ($allLibs as $keywordLib) {
            $libType = $keywordLib->LibType;
            $resourceType = $keywordLib->ResourceType;
            $source = $keywordLib->Source;
            # Query the custom term libraries for text anti-spam. 
            if ('similarText' == $libType and 'TEXT' == $resourceType and 'MANUAL' == $source) {
                array_push($similarTextLibs, $keywordLib);
            }
            # Query the feedback-based term libraries for text anti-spam. 
            if ('similarText' == $libType and 'TEXT' == $resourceType and 'FEEDBACK' == $source) {
                array_push($similarTextLibs, $keywordLib);
            }
        }
        print_r($similarTextLibs);
    } catch (ClientException $exception) {
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }

Create text libraries

  • Create a term library

    <?php
    
    use AlibabaCloud\Client\AlibabaCloud;
    use AlibabaCloud\Client\Exception\ClientException;
    use AlibabaCloud\Client\Exception\ServerException;
    use AlibabaCloud\Green\Green;
    
    try {
        /**
         * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
         * Common ways to obtain environment variables:
         * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        AlibabaCloud::accessKeyClient('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')
            ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
            ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
            ->regionId('cn-shanghai')
            ->asDefaultClient();
    
        $response = Green::v20170823()->createKeywordLib()
            ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
            ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request.
            ->withServiceModule('open_api')
            ->withName ('Name of the custom term library')
            ->withResourceType('TEXT')
            ->withLibType('textKeyword')
            ->withCategory('BLACK')
            ->request();
        print_r($response->toArray());
    } catch (ClientException $exception) {
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }
  • Create a text pattern library

    <?php
    
    use AlibabaCloud\Client\AlibabaCloud;
    use AlibabaCloud\Client\Exception\ClientException;
    use AlibabaCloud\Client\Exception\ServerException;
    use AlibabaCloud\Green\Green;
    
    try {
        /**
         * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
         * Common ways to obtain environment variables:
         * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        AlibabaCloud::accessKeyClient('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')
            ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
            ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
            ->regionId('cn-shanghai')
            ->asDefaultClient();
    
        $response = Green::v20170823()->createKeywordLib()
            ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
            ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request.
            ->withServiceModule('open_api')
            ->withName ('Name of the text pattern library')
            ->withResourceType('TEXT')
            ->withLibType('similarText')
            ->withCategory('BLACK')
            ->request();
        print_r($response->toArray());
    } catch (ClientException $exception) {
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }

Modify a text library

Change the name of a text library and the business scenario to which the text library applies.

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;

try {
    /**
     * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
     * Common ways to obtain environment variables:
     * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('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')
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $response = Green::v20170823()->updateKeywordLib()
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request.
        ->withId ('ID of the text library')
        ->withName ('New name of the text library')
        ->request();
    print_r($response->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Delete a text library

Important

If you delete a text library, the text entries in the text library are also deleted. You cannot delete a feedback-based text library.

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;

try {
   /**
     * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
     * Common ways to obtain environment variables:
     * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('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')
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $response = Green::v20170823()->deleteKeywordLib()
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request.
        ->withId ('ID of the text library')
        ->request();
    print_r($response->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Search for text entries

By default, the system returns all text entries in a text library by page. If you set the Keyword parameter, the system searches for all text entries that match the specified term in fuzzy mode.

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;

try {
    /**
     * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
     * Common ways to obtain environment variables:
     * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('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')
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $response = Green::v20170823()->describeKeyword()
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request.
        ->withKeywordLibId ('ID of the text library')
        ->request();
    print_r($response->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Add text entries

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;

try {
    /**
     * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
     * Common ways to obtain environment variables:
     * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('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')
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $response = Green::v20170823()->createKeyword()
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request.
        ->withKeywordLibId(0)
        ->withKeywords('[\'Text ID_1\',\'Text ID_2\']') // The length of a single text entry is 20 to 10,000 characters, and a text library can contain up to 10,000 text entries. You can add up to 10 text entries at a time. 
        ->request();
    print_r($response->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}

Remove text entries

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Green\Green;

try {
    /**
     * Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections. 
     * Common ways to obtain environment variables:
     * Obtain the AccessKey ID of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
     * Obtain the AccessKey secret of your RAM user: getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
     */
    AlibabaCloud::accessKeyClient('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')
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect on the requests that are sent by using the client and that have no separate settings.
        ->regionId('cn-shanghai')
        ->asDefaultClient();

    $response = Green::v20170823()->deleteKeyword()
        ->timeout(10) // Set the timeout period to 10 seconds. This setting takes effect only on the current request.
        ->connectTimeout(3) // Set the connection timeout period to 3 seconds. If the value of this parameter is smaller than 1, the unit of this parameter is milliseconds. This setting takes effect only on the current request.
        ->withKeywordLibId ('ID of the text library')
        ->withIds('[\'Text ID_1\',\'Text ID_2\']')
        ->request();
    print_r($response->toArray());
} catch (ClientException $exception) {
    echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
    echo $exception->getMessage() . PHP_EOL;
    echo $exception->getErrorCode() . PHP_EOL;
    echo $exception->getRequestId() . PHP_EOL;
    echo $exception->getErrorMessage() . PHP_EOL;
}