198 lines
5.8 KiB
Go
198 lines
5.8 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"contentSecurityDemo/conf"
|
|
"contentSecurityDemo/internal/model"
|
|
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
|
green20220302 "github.com/alibabacloud-go/green-20220302/v2/client"
|
|
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
)
|
|
|
|
// TextScanner 文本内容安全扫描器2.0
|
|
type TextScanner struct {
|
|
Client *green20220302.Client
|
|
Config *conf.Config
|
|
}
|
|
|
|
// TextServiceType 文本审核服务类型
|
|
type TextServiceType string
|
|
|
|
const (
|
|
// TextBaselineCheckGlobal 文国际业务多语言检测
|
|
TextBaselineCheckGlobal TextServiceType = "comment_multilingual_pro_global"
|
|
)
|
|
|
|
// NewTextScanner 创建文本扫描器
|
|
func NewTextScanner(config *conf.Config) (*TextScanner, error) {
|
|
// 获取有效的访问凭证
|
|
accessKeyID, accessKeySecret, securityToken := config.GetEffectiveCredentials()
|
|
|
|
// 创建配置
|
|
clientConfig := &openapi.Config{
|
|
AccessKeyId: tea.String(accessKeyID),
|
|
AccessKeySecret: tea.String(accessKeySecret),
|
|
SecurityToken: tea.String(securityToken),
|
|
RegionId: tea.String(config.Region),
|
|
Endpoint: tea.String(config.Endpoint),
|
|
}
|
|
|
|
// 创建客户端
|
|
client, err := green20220302.NewClient(clientConfig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("创建客户端失败: %v", err)
|
|
}
|
|
|
|
return &TextScanner{
|
|
Client: client,
|
|
Config: config,
|
|
}, nil
|
|
}
|
|
|
|
// ScanText 扫描文本内容
|
|
func (s *TextScanner) ScanText(content string, dataID string, serviceType TextServiceType) (*model.TextScanResponse, error) {
|
|
// 创建运行时配置
|
|
runtime := &util.RuntimeOptions{}
|
|
runtime.ReadTimeout = tea.Int(10000)
|
|
runtime.ConnectTimeout = tea.Int(10000)
|
|
|
|
serviceParameters, _ := json.Marshal(
|
|
map[string]interface{}{
|
|
"content": content,
|
|
},
|
|
)
|
|
// 构建请求参数
|
|
textScanRequest := &green20220302.TextModerationPlusRequest{
|
|
Service: tea.String(string(serviceType)),
|
|
ServiceParameters: tea.String(string(serviceParameters)),
|
|
}
|
|
|
|
// 调用API
|
|
response, err := s.Client.TextModerationPlusWithOptions(textScanRequest, runtime)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("请求失败: %v", err)
|
|
}
|
|
|
|
// 转换响应格式
|
|
scanResponse := &model.TextScanResponse{
|
|
Code: tea.Int32Value(response.Body.Code),
|
|
Message: tea.StringValue(response.Body.Message),
|
|
Data: make([]model.TextScanData, 0),
|
|
}
|
|
|
|
// 创建单个数据项
|
|
scanData := model.TextScanData{
|
|
DataID: dataID,
|
|
Code: tea.Int32Value(response.Body.Code),
|
|
Message: tea.StringValue(response.Body.Message),
|
|
DetectedLanguage: tea.StringValue(response.Body.Data.DetectedLanguage),
|
|
TranslatedContent: tea.StringValue(response.Body.Data.TranslatedContent),
|
|
Results: make([]*green20220302.TextModerationPlusResponseBodyDataResult, 0, len(response.Body.Data.Result)),
|
|
}
|
|
|
|
// 如果有结果数据,进行转换
|
|
if response.Body.Data != nil {
|
|
// 暂时创建一个基本的结果结构
|
|
data := response.Body.Data
|
|
scanData.Results = append(scanData.Results, data.Result...)
|
|
}
|
|
|
|
scanResponse.Data = append(scanResponse.Data, scanData)
|
|
|
|
return scanResponse, nil
|
|
}
|
|
|
|
// ScanTextBatch 批量扫描文本内容
|
|
func (s *TextScanner) ScanTextBatch(texts []string, dataIDs []string, serviceType TextServiceType) (*model.TextScanResponse, error) {
|
|
|
|
// 创建运行时配置
|
|
runtime := &util.RuntimeOptions{}
|
|
runtime.ReadTimeout = tea.Int(10000)
|
|
runtime.ConnectTimeout = tea.Int(10000)
|
|
|
|
// 构建请求参数 - 将多个文本合并为一个请求
|
|
combinedText := ""
|
|
for i, text := range texts {
|
|
if i > 0 {
|
|
combinedText += "\n"
|
|
}
|
|
combinedText += text
|
|
}
|
|
|
|
serviceParameters, _ := json.Marshal(
|
|
map[string]interface{}{
|
|
"content": combinedText,
|
|
},
|
|
)
|
|
// 构建请求参数
|
|
textScanRequest := &green20220302.TextModerationPlusRequest{
|
|
Service: tea.String(string(serviceType)),
|
|
ServiceParameters: tea.String(string(serviceParameters)),
|
|
}
|
|
|
|
// 调用API
|
|
response, err := s.Client.TextModerationPlusWithOptions(textScanRequest, runtime)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("请求失败: %v", err)
|
|
}
|
|
|
|
// 转换响应格式
|
|
scanResponse := &model.TextScanResponse{
|
|
Code: tea.Int32Value(response.Body.Code),
|
|
Message: tea.StringValue(response.Body.Message),
|
|
Data: make([]model.TextScanData, 0),
|
|
}
|
|
|
|
scanData := model.TextScanData{
|
|
Code: tea.Int32Value(response.Body.Code),
|
|
Message: tea.StringValue(response.Body.Message),
|
|
DetectedLanguage: tea.StringValue(response.Body.Data.DetectedLanguage),
|
|
TranslatedContent: tea.StringValue(response.Body.Data.TranslatedContent),
|
|
Results: make([]*green20220302.TextModerationPlusResponseBodyDataResult, 0, len(response.Body.Data.Result)),
|
|
}
|
|
|
|
// 如果有结果数据,进行转换
|
|
if response.Body.Data != nil {
|
|
data := response.Body.Data
|
|
scanData.Results = append(scanData.Results, data.Result...)
|
|
}
|
|
|
|
scanResponse.Data = append(scanResponse.Data, scanData)
|
|
|
|
return scanResponse, nil
|
|
}
|
|
|
|
// PrintResult 打印扫描结果
|
|
func (s *TextScanner) PrintResult(response *model.TextScanResponse) {
|
|
fmt.Println("=== 文本内容安全审核结果===")
|
|
fmt.Printf("状态码: %d\n", response.Code)
|
|
fmt.Printf("消息: %s\n", response.Message)
|
|
|
|
for _, data := range response.Data {
|
|
fmt.Printf("\n数据ID: %s\n", data.DataID)
|
|
fmt.Printf("处理状态: %d - %s\n", data.Code, data.Message)
|
|
fmt.Printf("检测语言: %s\n", data.DetectedLanguage)
|
|
fmt.Printf("翻译内容: %s\n", data.TranslatedContent)
|
|
for _, result := range data.Results {
|
|
|
|
if result.Confidence != nil {
|
|
fmt.Printf("置信度: %f\n", *result.Confidence)
|
|
}
|
|
if result.Description != nil {
|
|
fmt.Printf("描述: %s\n", *result.Description)
|
|
}
|
|
if result.Label != nil {
|
|
fmt.Printf("标签: %s\n", *result.Label)
|
|
}
|
|
if result.RiskWords != nil {
|
|
fmt.Printf("风险等级: %s\n", *result.RiskWords)
|
|
}
|
|
fmt.Println("---")
|
|
}
|
|
}
|
|
}
|