名称: glin-profanity
描述: 用于脏话检测和内容审核的库,支持火星文(Leetspeak)、Unicode 同形字和基于机器学习的检测。适用于过滤用户生成内容、审核评论、检查文本是否包含脏话、屏蔽消息或在应用中集成内容审核功能。支持 24 种语言。
脏话检测库,可识别规避检测的尝试,如火星文(f4ck、sh1t)、Unicode 欺骗(西里尔字母形似字)和混淆文本。
# JavaScript/TypeScript
npm install glin-profanity
# Python
pip install glin-profanity
import { checkProfanity, Filter } from 'glin-profanity';
// 简单检查
const result = checkProfanity("您的文本内容", {
detectLeetspeak: true,
normalizeUnicode: true,
languages: ['english']
});
result.containsProfanity // 布尔值
result.profaneWords // 检测到的脏词数组
result.processedText // 屏蔽后的版本
// 使用 Filter 实例
const filter = new Filter({
replaceWith: '***',
detectLeetspeak: true,
normalizeUnicode: true
});
filter.isProfane("text") // 布尔值
filter.checkProfanity("text") // 完整结果对象
from glin_profanity import Filter
filter = Filter({
"languages": ["english"],
"replace_with": "***",
"detect_leetspeak": True
})
filter.is_profane("text") # True/False
filter.check_profanity("text") # 完整结果字典
import { useProfanityChecker } from 'glin-profanity';
function ChatInput() {
const { result, checkText } = useProfanityChecker({
detectLeetspeak: true
});
return (
<input onChange={(e) => checkText(e.target.value)} />
);
}
| 特性 | 说明 |
|---|---|
| 火星文检测 | 识别 f4ck、sh1t、@$$ 等模式 |
| Unicode 标准化 | 将西里尔字母 fսck 转换为 fuck |
| 24 种语言支持 | 包括阿拉伯语、中文、俄语、印地语等 |
| 上下文白名单 | 支持医疗、游戏、技术等领域的特殊语境 |
| 机器学习集成 | 可选 TensorFlow.js 毒性检测 |
| 结果缓存 | LRU 缓存提升性能 |
const filter = new Filter({
languages: ['english', 'spanish'], // 要检查的语言
detectLeetspeak: true, // 检测火星文(如 f4ck、sh1t)
leetspeakLevel: 'moderate', // basic(基础) | moderate(中等) | aggressive(严格)
normalizeUnicode: true, // 检测 Unicode 欺骗
replaceWith: '*', // 替换字符
preserveFirstLetter: false, // f*** 与 **** 模式
customWords: ['badword'], // 添加自定义脏词
ignoreWords: ['hell'], // 忽略(白名单)词汇
cacheSize: 1000 // LRU 缓存条目数
});
import { analyzeContext } from 'glin-profanity';
const result = analyzeContext("患者患有乳腺肿瘤", {
domain: 'medical', // medical(医疗) | gaming(游戏) | technical(技术) | educational(教育)
contextWindow: 3, // 匹配词周围考虑的词汇数
confidenceThreshold: 0.7 // 标记的最小置信度
});
import { batchCheck } from 'glin-profanity';
const results = batchCheck([
"评论 1",
"评论 2",
"评论 3"
], { returnOnlyFlagged: true });
import { loadToxicityModel, checkToxicity } from 'glin-profanity/ml';
await loadToxicityModel({ threshold: 0.9 });
const result = await checkToxicity("你是最差的");
// { toxic: true, categories: { toxicity: 0.92, insult: 0.87 } }
const filter = new Filter({
detectLeetspeak: true,
normalizeUnicode: true,
languages: ['english']
});
bot.on('message', (msg) => {
if (filter.isProfane(msg.text)) {
deleteMessage(msg);
warnUser(msg.author);
}
});
const result = filter.checkProfanity(userContent);
if (result.containsProfanity) {
return {
valid: false,
issues: result.profaneWords,
suggestion: result.processedText // 屏蔽后的版本
};
}