文本分析:一次调用完成情感、垃圾内容和语言检测
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 个字符。API 密钥可以通过 X-API-Key 请求头发送,也可以作为请求体中的 api_key 发送。
响应
{
"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
准备好实现文本分析了吗?请参阅 快速入门指南,按步骤完成设置。