本文介绍了如何使用Python SDK管理自定义文本库,以满足文本反垃圾检测场景的个性化需求。

功能描述

根据文本类型的不同,文本库分为关键词文本库和相似文本文本库;根据管控目的不同,文本库分为白名单、黑名单、疑似名单。关于参数的详细信息,请参见自定义文本库API文档

您需要使用内容安全的API接入地址,调用本SDK接口。关于API接入地址的信息,请参见接入地址(Endpoint)

前提条件

获取文本库列表

  • 获取关键词文本库列表
    # coding=utf-8
    from aliyunsdkcore import client
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkgreen.request.v20170823 import DescribeKeywordLibRequest
    import json
    
    # 请填写您的AccessKey信息。
    clt = client.AcsClient("您的AccessKey ID", "您的AccessKey Secret", 'cn-shanghai')
    region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
    # 每次请求时需要新建Request,请勿复用Request对象。
    request = DescribeKeywordLibRequest.DescribeKeywordLibRequest()
    request.set_ServiceModule("open_api")
    response = clt.do_action_with_exception(request)
    print(response)
    result = json.loads(response)
    allLibs = result["KeywordLibList"]
    textAntispamKeywordLibs = []
    for keywordLib in allLibs:
        libType = keywordLib["LibType"]
        resourceType = keywordLib["ResourceType"]
        source = keywordLib["Source"]
        # 获取文本反垃圾自定义的关键词文本库。
        if "textKeyword" == libType and "TEXT" == resourceType and "MANUAL" == source:
            textAntispamKeywordLibs.append(keywordLib)
    
        # 获取文本反垃圾回流的关键词文本库。
        if "textKeyword" == libType and "TEXT" == resourceType and "FEEDBACK" == source:
            textAntispamKeywordLibs.append(keywordLib)
    print (json.dumps(textAntispamKeywordLibs))
  • 获取相似文本文本库列表(包含自定义和系统回流的相似文本文本库)
    # coding=utf-8
    from aliyunsdkcore import client
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkgreen.request.v20170823 import DescribeKeywordLibRequest
    import json
    
    # 请填写您的AccessKey信息。
    clt = client.AcsClient("您的AccessKey ID", "您的AccessKey Secret", 'cn-shanghai')
    region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
    # 每次请求时需要新建Request,请勿复用Request对象。
    request = DescribeKeywordLibRequest.DescribeKeywordLibRequest()
    request.set_ServiceModule("open_api")
    response = clt.do_action_with_exception(request)
    print(response)
    result = json.loads(response)
    allLibs = result["KeywordLibList"]
    similarTextLibs = []
    for keywordLib in allLibs:
        libType = keywordLib["LibType"]
        resourceType = keywordLib["ResourceType"]
        source = keywordLib["Source"]
        # 获取文本反垃圾自定义的相似文本文本库。
        if "similarText" == libType and "TEXT" == resourceType and "MANUAL" == source:
            similarTextLibs.append(keywordLib)
    
        # 获取文本反垃圾回流的相似文本文本库。
        if "similarText" == libType and "TEXT" == resourceType and "FEEDBACK" == source:
            similarTextLibs.append(keywordLib)
    print (json.dumps(similarTextLibs))

创建文本库

  • 创建关键词文本库
    # coding=utf-8
    from aliyunsdkcore import client
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkgreen.request.v20170823 import CreateKeywordLibRequest
    
    # 请填写您的AccessKey信息。
    clt = client.AcsClient("您的AccessKey ID", "您的AccessKey Secret", 'cn-shanghai')
    region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
    # 每次请求时需要新建Request,请勿复用Request对象。
    request = CreateKeywordLibRequest.CreateKeywordLibRequest()
    request.set_ServiceModule("open_api")
    request.set_Name("测试关键词文本库")
    # 设置为文本反垃圾场景使用。
    request.set_ResourceType("TEXT")
    # 设置类型为关键词。
    request.set_LibType("textKeyword")
    # 设置创建黑库。
    request.set_Category("BLACK")
    response = clt.do_action_with_exception(request)
    print(response)
  • 创建相似文本文本库
    # coding=utf-8
    from aliyunsdkcore import client
    from aliyunsdkcore.profile import region_provider
    from aliyunsdkgreen.request.v20170823 import CreateKeywordLibRequest
    
    # 请填写您的AccessKey信息。
    clt = client.AcsClient("您的AccessKey ID", "您的AccessKey Secret", 'cn-shanghai')
    region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
    # 每次请求时需要新建Request,请勿复用Request对象。
    request = CreateKeywordLibRequest.CreateKeywordLibRequest()
    request.set_ServiceModule("open_api")
    request.set_Name("测试相似文本库")
    # 设置为文本反垃圾场景使用。
    request.set_ResourceType("TEXT")
    # 设置类型为相似文本。
    request.set_LibType("similarText")
    # 设置创建黑库。
    request.set_Category("BLACK")
    response = clt.do_action_with_exception(request)
    print(response)

修改文本库

更新文本库库名以及修改文本库所适用的业务场景(BizType)。

# coding=utf-8

from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import UpdateKeywordLibRequest

# 请填写您的AccessKey信息。
clt = client.AcsClient("您的AccessKey ID", "您的AccessKey Secret",'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# 每次请求时需要新建Request,请勿复用Request对象。
request = UpdateKeywordLibRequest.UpdateKeywordLibRequest()
request.set_Id(7534001)
request.set_Name('测试修改名称')
response = clt.do_action_with_exception(request)
print(response)

删除文本库

注意 删除文本库也将删除文本库下的文本。系统回流的文本库不允许删除。
# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteKeywordLibRequest

# 请填写您的AccessKey信息。
clt = client.AcsClient("您的AccessKey ID", "您的AccessKey Secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# 每次请求时需要新建Request,请勿复用Request对象。
request = DeleteKeywordLibRequest.DeleteKeywordLibRequest()
request.set_Id(3353)
response = clt.do_action_with_exception(request)
print(response)

查找文本

默认分页获取所有文本。如果设置了Keyword字段,将模糊查找包含该字段值的文本。

# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DescribeKeywordRequest

# 请使用您自己的AccessKey信息。
clt = client.AcsClient("您的AccessKey ID", "您的AccessKey Secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# 每次请求时需要新建Request,请勿复用Request对象。
request = DescribeKeywordRequest.DescribeKeywordRequest()
request.set_KeywordLibId("2693")
request.set_PageSize(10)
request.set_CurrentPage(1)
response = clt.do_action_with_exception(request)
print(response)

添加文本

# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import CreateKeywordRequest

# 请填写您的AccessKey信息。
clt = client.AcsClient("您的AccessKey ID", "您的AccessKey Secret", 'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# 每次请求时需要新建Request,请勿复用Request对象。
request = CreateKeywordRequest.CreateKeywordRequest()
request.set_KeywordLibId(2693)
request.set_Keywords("['你好吗', '棒棒的']")
response = clt.do_action_with_exception(request)
print(response)

删除文本

# coding=utf-8
from aliyunsdkcore import client
from aliyunsdkcore.profile import region_provider
from aliyunsdkgreen.request.v20170823 import DeleteKeywordRequest

# 请填写您的AccessKey信息。
clt = client.AcsClient("您的AccessKey ID", "您的AccessKey Secret",'cn-shanghai')
region_provider.modify_point('Green', 'cn-shanghai', 'green.cn-shanghai.aliyuncs.com')
# 每次请求时需要新建Request,请勿复用Request对象。
request = DeleteKeywordRequest.DeleteKeywordRequest()
request.set_KeywordLibId(2693)
request.set_Ids("[1,2]")
response = clt.do_action_with_exception(request)
print(response)