跳到主要内容

文档

学习中心

通过完整指南、教程和 API 文档,掌握内容审核

文本分析:一次调用完成情感、垃圾内容和语言检测

Discuse 文本分析可通过一次 POST https://api.discuse.com/api/v2/check 请求,对消息进行毒性、垃圾内容和语言评分。将文本放在 content.text 中发送,使用 X-API-Key 请求头进行身份验证,然后在 results 下读取各项检测结果。

文本端点会检测什么?

一次 /api/v2/check 调用可以同时运行三项文本检测:

  • 情感:毒性、脏话、威胁和侮辱评分,以及负面/有毒判定。
  • 垃圾内容:推广垃圾信息、诈骗和机器人生成文本,返回 label 以及置信度分数。
  • 语言:检测到的语言代码,并可选择强制匹配预期语言。

每项检测只有在启用后才会运行,可在项目设置中启用,也可通过单次请求的 settings 对象启用。

如何调用文本端点?

curl -X POST https://api.discuse.com/api/v2/check \
  -H "Content-Type: application/json" \
  -H "X-API-Key: YOUR_API_KEY" \
  -d '{
    "content": {
      "text": "This is an example message to analyze"
    },
    "settings": {
      "check_sentiment": true,
      "check_spam": true,
      "check_language": true
    }
  }'

content.text 最多接受 10,000 个字符。请在 X-API-Key 请求头中发送 API 密钥。

响应

{
  "has_violations": false,
  "cached": false,
  "message": "Content appears safe",
  "results": {
    "hits": false,
    "sentiment": {
      "hit": false,
      "is_negative": false,
      "is_toxic": false,
      "score": 0.04,
      "toxicity": 0.02,
      "toxic": 0.02,
      "profanity": 0.01,
      "threat": 0.00,
      "insult": 0.03
    },
    "spamfinder": {
      "text": "This is an example message to analyze",
      "label": "ham",
      "confidence": 0.12,
      "is_spam": false,
      "hit": false
    },
    "language": {
      "language": "en",
      "confidence": 0.98,
      "hit": false
    }
  },
  "usage": {
    "api_requests_used": 41,
    "api_requests_limit": 5000,
    "api_requests_remaining": 4959
  }
}

has_violations 与 results.hits 保持一致:当任意已启用检测触发其 hit 标志时,它就是 true。只有在项目设置中启用计时后,响应中才会包含 processing_time_ms。

情感分析

情感检测会返回判定结果,以及 0.0 到 1.0 之间的各维度分数。

字段 类型 含义
is_negative bool 消息读起来偏负面
is_toxic bool 消息超过毒性阈值
score number 整体情感/严重程度分数
toxicity number 毒性置信度(0.0–1.0)
toxic number 毒性置信度(toxicity 的旧版别名)
profanity number 露骨语言置信度
threat number 伤害威胁置信度
insult number 人身攻击置信度
hit bool 当消息违反情感阈值时为 True
message string 可选说明

如何解读情感分数?

分数表示模型对该维度适用的置信度:

  • 0.0 – 0.3:低,通常安全。
  • 0.3 – 0.6:中等,可能需要审核。
  • 0.6 – 0.8:高,很可能存在问题。
  • 0.8 – 1.0:非常高,几乎肯定违规。

示例:有毒内容

请求:

{
  "content": {
    "text": "You're the worst person I've ever met. I hate everything about you."
  },
  "settings": { "check_sentiment": true }
}

响应:

{
  "has_violations": true,
  "results": {
    "hits": true,
    "sentiment": {
      "hit": true,
      "is_negative": true,
      "is_toxic": true,
      "score": 0.91,
      "toxicity": 0.89,
      "toxic": 0.89,
      "profanity": 0.15,
      "threat": 0.22,
      "insult": 0.95
    }
  }
}

垃圾内容检测

垃圾内容检测会对文本进行分类,并返回一个 label 和置信度分数。它可以捕捉那些会绕过关键词黑名单的模式:推广垃圾信息、诈骗与钓鱼,以及机器人生成文本。

示例:垃圾内容

请求:

{
  "content": {
    "text": "CONGRATULATIONS! You've won $10,000! Click here to claim: www.fake-prize.com"
  },
  "settings": { "check_spam": true }
}

响应:

{
  "has_violations": true,
  "results": {
    "hits": true,
    "spamfinder": {
      "text": "CONGRATULATIONS! You've won $10,000! Click here to claim: www.fake-prize.com",
      "label": "spam",
      "confidence": 0.97,
      "is_spam": true,
      "hit": true
    }
  }
}

is_spam 是模型的原始判定(模型将文本标记为垃圾内容)。hit 是考虑阈值后的决策:只有当 is_spam 为 true 且 confidence 超过你项目的垃圾内容阈值时,它才是 true。请基于 hit 进行审核拦截,而不是基于 is_spam。详情请参阅垃圾内容检测指南。

语言检测

设置 check_language 可检测 content.text 的语言代码。结果位于 results.language.language(例如 "en"、"fr"、"es")。

请求:

{
  "content": { "text": "Bonjour, comment allez-vous aujourd'hui?" },
  "settings": { "check_language": true }
}

响应:

{
  "results": {
    "language": {
      "language": "fr",
      "confidence": 0.99,
      "hit": false
    }
  }
}

如果要标记不符合预期语言的内容,请设置 expected_language。当检测到的语言不同时,language.hit 为 true,并会填充 expected/detected:

{
  "content": { "text": "Hola, cómo estás?" },
  "settings": { "check_language": true, "expected_language": "en" }
}
{
  "results": {
    "language": {
      "language": "es",
      "detected": "es",
      "expected": "en",
      "confidence": 0.95,
      "hit": true
    }
  }
}

完整语言列表和强制匹配模式请参阅语言检测。

可以切换哪些检测?

可选的 settings 对象会针对单次请求覆盖项目默认设置。与文本相关的开关包括:

设置 类型 作用
check_sentiment bool 运行情感分析
check_spam bool 运行垃圾内容检测
check_language bool 运行语言检测
check_badwords bool 运行禁用词黑名单
expected_language string 要强制匹配的语言代码(例如 "en")
{
  "content": { "text": "Your message here" },
  "settings": {
    "check_sentiment": true,
    "check_spam": true,
    "check_language": true,
    "expected_language": "en"
  }
}

使用限制

情感、垃圾内容和语言检测都会消耗对应套餐的情感分析配额:

套餐 每月情感分析次数
Basic 1,000
Gold 5,000
Platinum 15,000
Ultimate 30,000

缓存响应不会计入你的配额。每个响应中的 usage 对象都会报告 api_requests_used、api_requests_limit 和 api_requests_remaining。

最佳实践

按平台设置阈值

针对不同受众应用不同的情感阈值:

const THRESHOLDS = {
  kids:    { toxicity: 0.3, profanity: 0.2, threat: 0.2, insult: 0.3 },
  general: { toxicity: 0.5, profanity: 0.5, threat: 0.4, insult: 0.5 },
  adult:   { toxicity: 0.7, profanity: 0.8, threat: 0.5, insult: 0.7 }
};

考虑上下文

临床、新闻报道和虚构作品内容可能会合理地触发毒性和脏话分数。对于边界情况,请将自动评分与人工审核结合使用:

const sentiment = result.results.sentiment;
if (sentiment.toxicity > 0.9) {
  await removeContent(contentId);       // high confidence: auto-remove
} else if (sentiment.toxicity > 0.5) {
  await queueForReview(contentId);      // borderline: human review
} else {
  await approveContent(contentId);
}

集成示例

JavaScript

async function analyzeText(text) {
  const response = await fetch('https://api.discuse.com/api/v2/check', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-API-Key': process.env.DISCUSE_API_KEY
    },
    body: JSON.stringify({
      content: { text },
      settings: { check_sentiment: true, check_spam: true, check_language: true }
    })
  });

  return response.json();
}

const result = await analyzeText('Hello, how are you?');
console.log(result.has_violations); // false

Python

import os
import requests

def analyze_text(text):
    response = requests.post(
        'https://api.discuse.com/api/v2/check',
        headers={
            'Content-Type': 'application/json',
            'X-API-Key': os.environ['DISCUSE_API_KEY']
        },
        json={
            'content': {'text': text},
            'settings': {
                'check_sentiment': True,
                'check_spam': True,
                'check_language': True
            }
        }
    )
    return response.json()

result = analyze_text('Hello, how are you?')
print(result['has_violations'])  # False

准备好实现文本分析了吗?请参阅快速入门指南,按步骤完成设置。

由 Discuse 团队 撰写 · 最后更新于 June 2026