垃圾内容检测
Discuse 垃圾内容检测会对文本进行分类,判断其是否为垃圾内容,并给出置信度分数。将文本发送到 POST https://api.discuse.com/api/v2/check,启用 check_spam,然后从 results.spamfinder 读取判定结果。它能捕捉到简单关键词过滤器容易漏掉的推广垃圾内容、诈骗信息以及机器人生成的噪声。
垃圾内容检测能捕捉哪些内容?
该模型基于会绕过屏蔽列表的大规模常见模式进行训练:
- 推广垃圾内容和未经请求的广告
- 诈骗和钓鱼消息
- 机器人生成及复制粘贴的内容
它会返回一个 label(例如 spam 或 ham)以及一个 confidence 分数,方便你决定采用多严格的策略。
如何执行垃圾内容检查?
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": "CONGRATULATIONS! You won $10,000! Click here to claim: bit.ly/fake"
},
"settings": {
"check_spam": true
}
}'
响应格式
{
"has_violations": true,
"cached": false,
"message": "Spam content detected",
"results": {
"hits": true,
"spamfinder": {
"text": "CONGRATULATIONS! You won $10,000! Click here to claim: bit.ly/fake",
"label": "spam",
"confidence": 0.97,
"is_spam": true,
"hit": true
}
},
"usage": {
"api_requests_used": 8,
"api_requests_limit": 5000,
"api_requests_remaining": 4992
}
}
垃圾内容结果会返回哪些字段?
| 字段 | 类型 | 含义 |
|---|---|---|
text |
string | 被分类的文本 |
label |
string | 模型分类(例如 spam、ham) |
confidence |
number | 模型对该标签的置信度(0.0–1.0) |
is_spam |
bool | 原始模型判定 — label == spam,不考虑阈值 |
hit |
bool | 考虑阈值后的决策 — is_spam 且 confidence ≥ 你项目的垃圾内容阈值 |
is_spam 与 hit
is_spam 是原始判定:只要模型将文本标记为垃圾内容,无论置信度高低都会为真。hit 还要求置信度达到你项目配置的垃圾内容阈值。应基于 hit 执行审核动作,而不是基于 is_spam,这样低置信度的垃圾内容标签就不会误伤边界情况的消息。
如何解读置信度分数?
confidence 表示模型对其 label 的确定程度:
- 0.0 – 0.3:非常低 — 很可能是正常内容。
- 0.3 – 0.5:低 — 处于边界。
- 0.5 – 0.7:中等 — 可疑。
- 0.7 – 0.9:高 — 很可能是垃圾内容。
- 0.9 – 1.0:非常高 — 几乎可以确定是垃圾内容。
推荐阈值
根据你平台的容忍度设置项目的垃圾内容阈值:
const SPAM_THRESHOLDS = {
strict: 0.5, // professional platforms, financial services
standard: 0.7, // social media, forums
permissive: 0.85 // creative platforms, open communities
};
使用场景
评论区
async function moderateComment(comment) {
const result = await checkSpam(comment.text);
const spam = result.results.spamfinder;
if (spam.hit) {
if (spam.confidence > 0.9) {
return { action: 'reject', reason: 'spam_detected' };
}
return { action: 'review', reason: 'possible_spam' };
}
return { action: 'approve' };
}
用户注册
def validate_registration(user_data):
bio = user_data.get('bio')
if bio:
result = check_spam(bio)
if result['results']['spamfinder']['hit']:
return {'approved': False, 'reason': 'Spam content detected in profile'}
return {'approved': True}
消息平台
async function filterMessage(message, sender) {
const result = await checkSpam(message.text);
const spam = result.results.spamfinder;
if (spam.hit) {
await incrementSpamCount(sender.id);
const spamCount = await getSpamCount(sender.id);
if (spamCount > 3) {
await banUser(sender.id, 'repeated_spam');
}
return { delivered: false, reason: 'Message filtered as spam' };
}
return { delivered: true };
}
与其他检查组合使用
在一次请求中同时运行垃圾内容、情感和语言检测:
{
"content": {
"text": "Check out this amazing deal! Click here: example.com/offer"
},
"settings": {
"check_spam": true,
"check_sentiment": true,
"check_language": true
}
}
响应中随后会同时包含 results.spamfinder、results.sentiment 和 results.language。
最佳实践
使用分级响应
不要只做二元的拦截/放行,而是根据置信度分流:
function handleSpamResult(spam) {
if (!spam.hit) return 'allow';
if (spam.confidence > 0.95) return 'silent_delete';
if (spam.confidence > 0.8) return 'block_notify';
if (spam.confidence > 0.6) return 'flag_for_review';
return 'apply_friction';
}
跟踪重复违规者
async function assessUser(userId, spam) {
if (spam.hit) {
await incrementUserSpamScore(userId, spam.confidence);
}
const userScore = await getUserSpamScore(userId);
if (userScore > 10.0) await autoSuspendUser(userId);
else if (userScore > 5.0) await flagForManualReview(userId);
}
将可信用户加入白名单
对已验证或高信任度账号跳过垃圾内容检查,以减少误报并节省配额:
function shouldCheckSpam(user) {
if (user.isVerified) return false;
if (user.trustScore > 0.9) return false;
return true;
}
使用限制
垃圾内容检测会消耗你的情感分析配额:
| 套餐 | 每月分析次数 | 说明 |
|---|---|---|
| Basic | 1,000 | 包含垃圾内容 + 情感分析 |
| Gold | 5,000 | 包含垃圾内容 + 情感分析 |
| Platinum | 15,000 | 包含垃圾内容 + 情感分析 |
| Ultimate | 30,000 | 包含垃圾内容 + 情感分析 |
缓存响应不会计入你的配额。
集成示例
Express.js 中间件
const spamFilter = async (req, res, next) => {
if (req.body.text) {
const result = await checkSpam(req.body.text);
if (result.results.spamfinder.hit) {
return res.status(400).json({
error: 'spam_detected',
message: 'Your message was flagged as spam'
});
}
}
next();
};
app.post('/api/comments', spamFilter, createComment);
Python Flask
from functools import wraps
from flask import request, jsonify
def spam_filter(f):
@wraps(f)
def decorated(*args, **kwargs):
text = request.json.get('text')
if text:
result = check_spam(text)
if result['results']['spamfinder']['hit']:
return jsonify({
'error': 'spam_detected',
'message': 'Your message was flagged as spam'
}), 400
return f(*args, **kwargs)
return decorated
@app.route('/api/comments', methods=['POST'])
@spam_filter
def create_comment():
pass