全部产品
Search
文档中心

Content Moderation:Kelola pustaka teks kustom

更新时间:Jul 02, 2025

Topik ini menjelaskan cara menggunakan Content Moderation SDK untuk Java dalam mengelola pustaka teks kustom, membantu memenuhi kebutuhan personalisasi dalam skenario anti-spam teks.

Deskripsi

Berdasarkan jenis teks, pustaka teks diklasifikasikan menjadi pustaka istilah dan pustaka pola teks. Berdasarkan tujuan pengelolaan, pustaka teks dibagi menjadi daftar putih, daftar hitam, dan daftar tinjauan. Untuk informasi lebih lanjut tentang parameter terkait, lihat DescribeKeywordLib.

Prasyarat

  • Dependensi Java telah diinstal. Untuk informasi lebih lanjut, lihat Instalasi.

    Catatan

    Gunakan versi Java yang dijelaskan dalam topik Instalasi untuk menginstal dependensi. Jika tidak, panggilan operasi berikutnya akan gagal.

  • Kelas utilitas Extension.Uploader telah diunduh dan diimpor ke proyek Anda jika Anda mengirimkan gambar lokal atau aliran (stream) gambar biner untuk moderasi gambar.

Kueri pustaka teks

  • Kueri pustaka istilah

    DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
    describeKeywordLibRequest.setServiceModule("open_api");
    
    try {
        // Mengembalikan semua pustaka teks, termasuk pustaka istilah dan pustaka pola teks untuk anti-spam teks, pustaka istilah untuk mendeteksi iklan dalam gambar, dan pustaka istilah untuk anti-spam audio.
        DescribeKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(describeKeywordLibRequest);
        System.out.println(JSON.toJSONString(describeKeywordLibResponse));
    
        // Daftar pustaka teks yang dikonfigurasi untuk anti-spam teks.
        List<DescribeKeywordLibResponse.KeywordLib> allLibs = describeKeywordLibResponse.getKeywordLibList();
        List<DescribeKeywordLibResponse.KeywordLib> textAntispamKeywordLibs = new ArrayList<DescribeKeywordLibResponse.KeywordLib>();
        for (DescribeKeywordLibResponse.KeywordLib keywordLib : allLibs) {
            String LibType = keywordLib.getLibType();
            String resourceType = keywordLib.getResourceType();
            String source =  keywordLib.getSource();
            // Daftar pustaka istilah kustom untuk anti-spam teks.
            if ("textKeyword".equals(LibType) && "TEXT".equals(resourceType)  && "MANUAL".equals(source)) {
                textAntispamKeywordLibs.add(keywordLib);
            }
            // Daftar pustaka istilah berbasis umpan balik untuk anti-spam teks.
            if ("textKeyword".equals(LibType) && "TEXT".equals(resourceType)  && "FEEDBACK".equals(source)) {
                textAntispamKeywordLibs.add(keywordLib);
            }
        }
    
         System.out.println(JSON.toJSONString(textAntispamKeywordLibs));
     } catch (ClientException e) {
        e.printStackTrace();
    }
  • Kueri pustaka pola teks, termasuk pustaka pola teks kustom dan pustaka pola teks berbasis umpan balik

    DescribeKeywordLibRequest describeKeywordLibRequest = new DescribeKeywordLibRequest();
    describeKeywordLibRequest.setServiceModule("open_api");
    
    try {
        // Mengembalikan semua pustaka teks, termasuk pustaka istilah dan pustaka pola teks untuk anti-spam teks, pustaka istilah untuk mendeteksi iklan dalam gambar, dan pustaka istilah untuk anti-spam audio.
        DescribeKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(describeKeywordLibRequest);
        System.out.println(JSON.toJSONString(describeKeywordLibResponse));
    
        // Daftar pustaka pola teks.
        List<DescribeKeywordLibResponse.KeywordLib> allLibs = describeKeywordLibResponse.getKeywordLibList();
        List<DescribeKeywordLibResponse.KeywordLib> similarTextLibs = new ArrayList<DescribeKeywordLibResponse.KeywordLib>();
        for (DescribeKeywordLibResponse.KeywordLib keywordLib : allLibs) {
            String LibType =  keywordLib.getLibType();
            String resourceType = keywordLib.getResourceType();
            String source =  keywordLib.getSource();
            // Daftar pustaka pola teks kustom untuk anti-spam teks.
            if("similarText".equals(LibType) && "TEXT".equals(resourceType)  && "MANUAL".equals(source)) {
                similarTextLibs.add(keywordLib);
            }
    
            // Daftar pustaka pola teks berbasis umpan balik untuk anti-spam teks.
            if("similarText".equals(LibType) && "TEXT".equals(resourceType)  && "FEEDBACK".equals(source)) {
                similarTextLibs.add(keywordLib);
            }
        }
        System.out.println(JSON.toJSONString(similarTextLibs));
    } catch (ClientException e) {
        e.printStackTrace();
    }

Buat pustaka teks

  • Buat pustaka istilah

    CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
    createKeywordLibRequest.setServiceModule("open_api");
    createKeywordLibRequest.setName("Pustaka istilah untuk pengujian");
    // Tentukan bahwa pustaka yang dibuat digunakan untuk anti-spam teks.
    createKeywordLibRequest.setResourceType("TEXT");
    // Tentukan bahwa pustaka yang dibuat adalah pustaka istilah.
    createKeywordLibRequest.setLibType("textKeyword");
    // Tentukan bahwa pustaka yang dibuat adalah daftar hitam.
    createKeywordLibRequest.setCategory("BLACK");
    
    try {
        CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
        // ID pustaka istilah. Jika tidak ada kesalahan yang dilaporkan, pustaka istilah dibuat.
        String libId = describeKeywordLibResponse.getId();
    } catch (ClientException e) {
        e.printStackTrace();
    }
  • Buat pustaka pola teks

    CreateKeywordLibRequest createKeywordLibRequest = new CreateKeywordLibRequest();
    createKeywordLibRequest.setServiceModule("open_api");
    createKeywordLibRequest.setName("Pustaka pola teks untuk pengujian");
    // Tentukan bahwa pustaka yang dibuat digunakan untuk anti-spam teks.
    createKeywordLibRequest.setResourceType("TEXT");
    // Tentukan bahwa pustaka yang dibuat adalah pustaka pola teks.
    createKeywordLibRequest.setLibType("similarText");
    // Tentukan bahwa pustaka yang dibuat adalah daftar hitam. Anda juga dapat menentukan bahwa pustaka yang dibuat adalah daftar putih atau daftar tinjauan.
    createKeywordLibRequest.setCategory("BLACK");
    
    try {
        CreateKeywordLibResponse describeKeywordLibResponse = client.getAcsResponse(createKeywordLibRequest);
        // ID pustaka pola teks. Jika tidak ada kesalahan yang dilaporkan, pustaka pola teks dibuat.
        String libId = describeKeywordLibResponse.getId();
    } catch (ClientException e) {
        e.printStackTrace();
    }

Modifikasi pustaka teks

Ubah nama pustaka teks dan skenario moderasi tempat pustaka teks digunakan.

UpdateKeywordLibRequest updateKeywordLibRequest = new UpdateKeywordLibRequest();
// Tentukan ID pustaka teks yang akan dimodifikasi.
updateKeywordLibRequest.setId(0);
// Tentukan nama baru pustaka teks.
updateKeywordLibRequest.setName("Nama baru pustaka teks");
// Tentukan skenario moderasi baru tempat pustaka teks berlaku.
updateKeywordLibRequest.setBizTypes(JSON.toJSONString(Arrays.asList("New BizType_1", "New BizType_2")));

try {
    UpdateKeywordLibResponse updateKeywordLibResponse = client.getAcsResponse(updateKeywordLibRequest);
    // ID permintaan. Jika tidak ada kesalahan yang dilaporkan, modifikasi berhasil.
    String requestId = updateKeywordLibResponse.getRequestId();
} catch (ClientException e) {
    e.printStackTrace();
}

Hapus pustaka teks

Penting

Jika Anda menghapus pustaka teks, entri teks dalam pustaka tersebut juga akan dihapus. Pustaka teks berbasis umpan balik tidak dapat dihapus.

DeleteKeywordLibRequest deleteKeywordLibRequest = new DeleteKeywordLibRequest();
// Tentukan ID pustaka teks yang akan dihapus.
deleteKeywordLibRequest.setId(3353);

try {
    DeleteKeywordLibResponse deleteKeywordLibResponse = client.getAcsResponse(deleteKeywordLibRequest);
    // ID permintaan. Jika tidak ada kesalahan yang dilaporkan, pustaka teks dihapus.
    String requestId = deleteKeywordLibResponse.getRequestId();
} catch (ClientException e) {
    e.printStackTrace();
}

Cari entri teks

Secara default, sistem mengembalikan semua entri teks dalam pustaka teks per halaman. Jika Anda menyetel parameter Kata Kunci, sistem mencari semua entri teks yang cocok dengan istilah yang ditentukan dalam mode kabur.

DescribeKeywordRequest describeKeywordRequest = new DescribeKeywordRequest();
// Tentukan ID pustaka teks tempat Anda ingin mencari entri teks.
describeKeywordRequest.setKeywordLibId(0);
describeKeywordRequest.setPageSize(10);
describeKeywordRequest.setCurrentPage(1);
// (Opsional) Tentukan istilah yang digunakan untuk pencarian kabur.
describeKeywordRequest.setKeyword("Entri teks yang akan dicari");
try {
    DescribeKeywordResponse describeKeywordResponse = client.getAcsResponse(describeKeywordRequest);
    // Jumlah total entri yang dikembalikan.
    Integer totalCount = describeKeywordResponse.getTotalCount();
    // Nomor halaman yang dikembalikan.
    Integer currentPage = describeKeywordResponse.getCurrentPage();
    // Jumlah entri yang dikembalikan per halaman.
    Integer pageSize = describeKeywordResponse.getPageSize();
    for (DescribeKeywordResponse.Keyword keywordItem : describeKeywordResponse.getKeywordList()) {
        // ID kunci utama istilah.
        Integer id = keywordItem.getId();
        // Waktu saat istilah dibuat.
        String createTime = keywordItem.getCreateTime();
        // Jumlah kali istilah tersebut ditemukan.
        Integer hitCount = keywordItem.getHitCount();
        // Istilah.
        String keyword = keywordItem.getKeyword();
    }
} catch (ClientException e) {
    e.printStackTrace();
}

Tambahkan entri teks

CreateKeywordRequest createKeywordRequest = new CreateKeywordRequest();
// Tentukan ID pustaka teks tempat Anda ingin menambahkan entri teks.
createKeywordRequest.setKeywordLibId(0L);
// Entri teks yang akan ditambahkan.
createKeywordRequest.setKeywords(JSON.toJSONString(Arrays.asList("Istilah_1", "Istilah_2")));

try {
    CreateKeywordResponse createKeywordResponse = client.getAcsResponse(createKeywordRequest);
    // Jumlah istilah yang ditambahkan.
    Integer successCount = createKeywordResponse.getSuccessCount();
    // Daftar istilah tidak valid, yaitu istilah yang gagal ditambahkan.
    List<String> invalidKeywordList = createKeywordResponse.getInvalidKeywordList();
} catch (ClientException e) {
    e.printStackTrace();
}

Hapus entri teks

DeleteKeywordRequest deleteKeywordRequest = new DeleteKeywordRequest();
// Tentukan ID pustaka teks tempat Anda ingin menghapus entri teks.
deleteKeywordRequest.setKeywordLibId(String.valueOf("ID pustaka teks"));
// Entri teks yang akan dihapus.
deleteKeywordRequest.setIds(JSON.toJSONString(Arrays.asList(1, 2)));

try {
    DeleteKeywordResponse deleteKeywordResponse = client.getAcsResponse(deleteKeywordRequest);
    // ID permintaan. Jika tidak ada kesalahan yang dilaporkan, entri teks dihapus.
    String requestId = deleteKeywordResponse.getRequestId();
} catch (ClientException e) {
    e.printStackTrace();
}