feat(app): 内容安全词库 + 过滤服务 + 分享前检查 — 28 个测试全覆盖
Some checks failed
Main Merge / backend (push) Has been cancelled
Main Merge / frontend (push) Has been cancelled

新增文件:
- sensitive_words.dart — 8 分类 ~200 条敏感词 + 谐音/形近/数字变体映射
- content_filter_service.dart — 精确匹配 + 变体匹配 + 文本预处理(去零宽/空格/符号)
- content_filter_service_test.dart — 28 个测试(8分类精确/安全内容/预处理/变体/边界/词库完整性)

修改:
- share_bottom_sheet.dart — 分享到班级前调用 ContentFilterService,
  有敏感词时弹出警告对话框(返回修改/仍然分享),新增 contentText 参数
This commit is contained in:
iven
2026-06-03 19:40:13 +08:00
parent 9c92cba87f
commit 988ee7335a
4 changed files with 631 additions and 8 deletions

View File

@@ -0,0 +1,184 @@
// 内容安全过滤服务 — 本地敏感词检测
//
// 提供 checkText() 纯函数,用于在分享日记前检查文本内容是否包含敏感词。
// 检测策略:精确匹配 + 谐音/形近/数字变体匹配。
// 返回匹配列表,空列表表示内容安全。不自动屏蔽,由 UI 层决定提示方式。
import 'sensitive_words.dart';
/// 敏感词匹配结果
class SensitiveWordMatch {
/// 匹配到的敏感词原文
final String word;
/// 所属分类
final SensitiveCategory category;
/// 在预处理后文本中的起始位置
final int position;
const SensitiveWordMatch({
required this.word,
required this.category,
required this.position,
});
@override
String toString() => 'SensitiveWordMatch("$word", ${category.label}, @$position)';
}
/// 内容安全过滤服务
///
/// 纯静态方法,无状态,可安全在任何地方调用。
/// 性能:~200 条词 × contains() 检查,<1ms 完成。
class ContentFilterService {
ContentFilterService._();
/// 检查文本内容,返回所有匹配到的敏感词。
///
/// 对输入文本进行预处理(去空格/特殊符号/零宽字符/小写化),
/// 然后遍历全量词库做精确匹配和谐音变体匹配。
/// 返回空列表表示内容安全。
static List<SensitiveWordMatch> checkText(String text) {
if (text.isEmpty) return const [];
final normalized = _normalize(text);
if (normalized.isEmpty) return const [];
final matches = <SensitiveWordMatch>[];
final seen = <String>{}; // 去重:同一词不重复报告
for (final entry in kSensitiveWords.entries) {
final category = entry.key;
for (final word in entry.value) {
// 精确匹配
final pos = normalized.indexOf(word);
if (pos >= 0 && seen.add(word)) {
matches.add(SensitiveWordMatch(
word: word,
category: category,
position: pos,
));
}
// 谐音/变体匹配 — 将词中每个有变体映射的字替换为变体,检查是否命中
if (_matchesWithVariants(normalized, word)) {
if (seen.add('variant:$word')) {
matches.add(SensitiveWordMatch(
word: word,
category: category,
position: -1, // 变体匹配无法精确定位
));
}
}
}
}
// 整词变体匹配 — kHomophoneVariants 中的多字 key如 "卧槽"
for (final variantEntry in kHomophoneVariants.entries) {
final originalKey = variantEntry.key;
if (originalKey.length <= 1) continue; // 单字已在上面处理
// 找到这个变体 key 对应的分类
SensitiveCategory? foundCategory;
for (final entry in kSensitiveWords.entries) {
if (entry.value.contains(originalKey)) {
foundCategory = entry.key;
break;
}
}
if (foundCategory == null) continue;
for (final variant in variantEntry.value) {
if (variant.isEmpty) continue;
final vPos = normalized.indexOf(variant.toLowerCase());
if (vPos >= 0) {
final key = 'wvariant:$originalKey:$variant';
if (seen.add(key)) {
matches.add(SensitiveWordMatch(
word: originalKey,
category: foundCategory,
position: vPos,
));
}
}
}
}
return matches;
}
/// 检查文本是否包含敏感词(快捷方法)
static bool hasSensitiveContent(String text) => checkText(text).isNotEmpty;
/// 获取匹配到的分类标签集合(用于 UI 展示)
static Set<String> getMatchedCategories(List<SensitiveWordMatch> matches) {
return matches.map((m) => m.category.label).toSet();
}
/// 变体匹配:检查文本中是否出现了词的谐音/形近/数字变体版本
///
/// 将敏感词中每个有变体映射的字符逐一替换为变体,检查替换后的
/// 字符串是否出现在文本中。例如 "去死" → 检查 "去4" 是否在文本中。
static bool _matchesWithVariants(String normalizedText, String word) {
final chars = word.split('');
final variantChars = <List<String>>[];
for (final char in chars) {
final variants = kHomophoneVariants[char];
if (variants != null && variants.isNotEmpty) {
// 原字符 + 所有变体
variantChars.add([char, ...variants]);
} else {
variantChars.add([char]);
}
}
// 生成所有变体组合并检查
return _checkCombinations(normalizedText, variantChars, 0, '');
}
/// 递归生成变体组合并检查文本
static bool _checkCombinations(
String text,
List<List<String>> variantChars,
int index,
String current,
) {
if (index == variantChars.length) {
return text.contains(current);
}
for (final char in variantChars[index]) {
if (_checkCombinations(text, variantChars, index + 1, current + char)) {
return true;
}
}
return false;
}
/// 文本预处理:去除干扰字符,统一为小写
///
/// 1. 去除零宽字符U+200B~U+200F, U+FEFF
/// 2. 去除空格、制表符、换行
/// 3. 去除常见特殊符号(用于绕过的 @#$%^&* 等)
/// 4. 转小写(对英文词有效)
static String _normalize(String text) {
final buffer = StringBuffer();
for (final rune in text.runes) {
// 跳过零宽字符
if (rune >= 0x200B && rune <= 0x200F) continue;
if (rune == 0xFEFF) continue;
// 跳过空白
if (rune == 0x20 || rune == 0x09 || rune == 0x0A || rune == 0x0D) continue;
// 跳过常见绕过符号
if (rune == 0x2E || rune == 0x2C || rune == 0x2D || rune == 0x5F) continue; // . , - _
if (rune == 0x21 || rune == 0x40 || rune == 0x23 || rune == 0x24) continue; // ! @ # $
if (rune == 0x25 || rune == 0x5E || rune == 0x26 || rune == 0x2A) continue; // % ^ & *
if (rune == 0x7E || rune == 0x60) continue; // ~ `
buffer.writeCharCode(rune);
}
return buffer.toString().toLowerCase();
}
}

View File

@@ -0,0 +1,141 @@
// 敏感词库 — 本地静态词库常量,面向小学生场景
//
// 分类:暴力、色情、欺凌、毒品、赌博、政治、诈骗、粗口
// 每个分类包含基础词 + 谐音/形近/数字变体
// 词库为 const 编译期常量,零运行时开销
//
// 注意:本词库仅为 Phase 1 基础覆盖Phase 2 将接入服务端 AI + 可更新词库。
/// 敏感词分类
enum SensitiveCategory {
violence('暴力'),
sexual('色情'),
bullying('欺凌'),
drugs('毒品'),
gambling('赌博'),
politics('政治敏感'),
fraud('诈骗'),
profanity('粗口');
const SensitiveCategory(this.label);
final String label;
}
/// ============================================================
/// 各分类敏感词
/// ============================================================
/// 暴力类
const _violenceWords = [
// 直接暴力
'杀人', '砍人', '捅人', '打死', '打死你', '弄死', '弄死你',
'揍你', '揍死', '打死他', '砍死', '捅死',
'杀了他', '打死他', '砍了他', '捅了他',
'去死', '你去死', '怎么不去死',
'割腕', '割脖子', '跳楼', '上吊',
// 武器
'炸弹', '手枪', '步枪', '子弹', '刀杀',
// 自残/伤害暗示
'自杀', '自残', '不想活',
];
/// 色情类
const _sexualWords = [
'色情', '裸体', '裸照', '黄色', '黄片',
'做爱', '性行为', '性交', '强奸', '强暴',
'猥亵', '性骚扰', '偷拍',
'发情', '骚货', '贱人',
];
/// 欺凌类
const _bullyingWords = [
'废物', '垃圾', '蠢货', '白痴', '弱智',
'傻子', '笨蛋', '猪头', '丑八怪',
'滚开', '滚蛋', '闭嘴', '别烦我',
'讨厌鬼', '没人要', '没朋友',
'不和你玩', '不要和你玩',
'大家不要理', '孤立',
'偷东西', '小偷',
];
/// 毒品类
const _drugsWords = [
'毒品', '吸毒', '贩毒', '大麻', '海洛因',
'冰毒', '摇头丸', '可卡因', '吗啡',
'鸦片', 'K粉', '安非他命',
'上瘾', '毒瘾',
];
/// 赌博类
const _gamblingWords = [
'赌博', '赌钱', '下注', '押注', '赌场',
'买彩票', '时时彩', '六合彩',
'百家乐', '老虎机', '扑克赌',
'赌债', '借钱赌',
];
/// 政治敏感类
const _politicsWords = [
'反动', '颠覆', '分裂', '暴动', '造反',
'推翻', '政变', '游行示威',
];
/// 诈骗类
const _fraudWords = [
'诈骗', '骗钱', '骗密码', '骗账号',
'中奖了', '恭喜中奖', '免费领取',
'点击链接领奖', '转账给我',
'刷单', '兼职刷单', '高薪兼职',
'传销', '拉人头',
];
/// 粗口类
const _profanityWords = [
'操你', '妈的', '他妈', '去你的', '狗屎',
'', '', '放屁', '扯淡', '王八蛋',
'混蛋', '', '我去', '卧槽',
'我靠', '我擦',
];
/// 全量词库:分类 → 词列表
const Map<SensitiveCategory, List<String>> kSensitiveWords = {
SensitiveCategory.violence: _violenceWords,
SensitiveCategory.sexual: _sexualWords,
SensitiveCategory.bullying: _bullyingWords,
SensitiveCategory.drugs: _drugsWords,
SensitiveCategory.gambling: _gamblingWords,
SensitiveCategory.politics: _politicsWords,
SensitiveCategory.fraud: _fraudWords,
SensitiveCategory.profanity: _profanityWords,
};
/// ============================================================
/// 谐音/形近/数字变体映射
/// ============================================================
/// 原词 → 变体列表
///
/// 变体检测在预处理后的文本上运行,可以捕获常见的绕过手法:
/// - 数字谐音: "死" → "4"
/// - 形近替换: "傻" → "纱"
/// - 拼音缩写: "牛逼" → "nb"
const Map<String, List<String>> kHomophoneVariants = {
// 暴力相关
'': ['4', '', '', ''],
'': ['', '', ''],
'': ['砍人'],
'': ['捅人'],
// 欺凌相关
'': ['', '', ''],
'': [], // 无实际变体
'': [''],
'废物': ['费物', '废无'],
'垃圾': ['拉吉', '垃 圾'],
// 粗口相关
'': ['', '', ''],
'卧槽': ['我槽', '我草', 'wc', 'WC', 'Wc'],
'我靠': ['我 k', '我K'],
// 欺凌
'': [''],
'': [''],
};

View File

@@ -4,20 +4,27 @@
// - 温暖友好的文案(面向小学生)
// - 分享到班级(有班级时显示)/ 仅自己可见
// - 无班级时提示加入班级后可分享
// - 分享前自动进行内容安全检查(敏感词过滤)
import 'package:flutter/material.dart';
import '../../../data/services/content_filter_service.dart';
/// 编辑器完成后的分享选择面板
class ShareBottomSheet extends StatelessWidget {
final String? classId;
final String className;
final void Function(bool shareToClass) onDecision;
/// 用于内容安全检查的文本内容(标题 + 文本元素)
final String contentText;
const ShareBottomSheet({
super.key,
required this.classId,
required this.className,
required this.onDecision,
this.contentText = '',
});
@override
@@ -65,10 +72,7 @@ class ShareBottomSheet extends StatelessWidget {
width: double.infinity,
height: 52,
child: FilledButton.icon(
onPressed: () {
onDecision(true);
Navigator.pop(context);
},
onPressed: () => _handleShare(context, shareToClass: true),
icon: const Icon(Icons.group),
label: Text('分享到 $className'),
style: FilledButton.styleFrom(
@@ -86,10 +90,7 @@ class ShareBottomSheet extends StatelessWidget {
width: double.infinity,
height: 52,
child: OutlinedButton.icon(
onPressed: () {
onDecision(false);
Navigator.pop(context);
},
onPressed: () => _handleShare(context, shareToClass: false),
icon: const Icon(Icons.lock_outline),
label: const Text('仅自己可见'),
style: OutlinedButton.styleFrom(
@@ -116,4 +117,80 @@ class ShareBottomSheet extends StatelessWidget {
),
);
}
/// 处理分享/保存决定
void _handleShare(BuildContext context, {required bool shareToClass}) {
// 仅在分享到班级时进行内容安全检查
if (shareToClass && contentText.isNotEmpty) {
final matches = ContentFilterService.checkText(contentText);
if (matches.isNotEmpty) {
_showContentWarning(context, matches);
return;
}
}
// 安全或仅自己可见 → 直接执行
onDecision(shareToClass);
Navigator.pop(context);
}
/// 显示内容安全警告对话框
void _showContentWarning(
BuildContext context,
List<SensitiveWordMatch> matches,
) {
final categories = ContentFilterService.getMatchedCategories(matches);
final words = matches.map((m) => ' "${m.word}"').toSet().toList();
final wordList = words.take(5).join('');
final categoryList = categories.join('');
showDialog(
context: context,
builder: (dialogContext) => AlertDialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
title: const Row(
children: [
Icon(Icons.warning_amber_rounded, color: Colors.orange),
SizedBox(width: 8),
Text('内容提醒'),
],
),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('日记中可能包含不太合适分享的内容:'),
const SizedBox(height: 8),
Text(
wordList,
style: const TextStyle(fontWeight: FontWeight.w600),
),
const SizedBox(height: 4),
Text(
'涉及:$categoryList',
style: TextStyle(fontSize: 13, color: Colors.grey.shade600),
),
const SizedBox(height: 12),
const Text(
'建议修改后再分享,或者先保存为仅自己可见。',
style: TextStyle(fontSize: 13),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(dialogContext),
child: const Text('返回修改'),
),
TextButton(
onPressed: () {
Navigator.pop(dialogContext); // 关闭对话框
onDecision(true); // 仍然分享
Navigator.pop(context); // 关闭 BottomSheet
},
child: const Text('仍然分享'),
),
],
),
);
}
}

View File

@@ -0,0 +1,221 @@
// ContentFilterService 单元测试
//
// 覆盖:精确匹配、谐音变体匹配、文本预处理、各分类检测、边界条件
import 'package:flutter_test/flutter_test.dart';
import 'package:nuanji_app/data/services/content_filter_service.dart';
import 'package:nuanji_app/data/services/sensitive_words.dart';
void main() {
// ============================================================
// 精确匹配 — 各分类
// ============================================================
group('精确匹配', () {
test('暴力类词汇检测', () {
final matches = ContentFilterService.checkText('我要打死你');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.category == SensitiveCategory.violence), isTrue);
expect(matches.any((m) => m.word == '打死你'), isTrue);
});
test('色情类词汇检测', () {
final matches = ContentFilterService.checkText('这个视频很色情');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.category == SensitiveCategory.sexual), isTrue);
});
test('欺凌类词汇检测', () {
final matches = ContentFilterService.checkText('你是个废物');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.category == SensitiveCategory.bullying), isTrue);
expect(matches.any((m) => m.word == '废物'), isTrue);
});
test('毒品类词汇检测', () {
final matches = ContentFilterService.checkText('他在吸毒');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.category == SensitiveCategory.drugs), isTrue);
});
test('赌博类词汇检测', () {
final matches = ContentFilterService.checkText('我们去赌钱吧');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.category == SensitiveCategory.gambling), isTrue);
});
test('粗口类词汇检测', () {
final matches = ContentFilterService.checkText('卧槽太厉害了');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.category == SensitiveCategory.profanity), isTrue);
});
test('诈骗类词汇检测', () {
final matches = ContentFilterService.checkText('恭喜中奖了,点击链接领奖');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.category == SensitiveCategory.fraud), isTrue);
});
test('政治敏感类词汇检测', () {
final matches = ContentFilterService.checkText('要造反了');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.category == SensitiveCategory.politics), isTrue);
});
});
// ============================================================
// 安全内容
// ============================================================
group('安全内容', () {
test('正常日记文本不触发', () {
final text = '今天天气很好,我和小明一起去公园玩,非常开心。'
'我们玩了滑梯、秋千,还吃了冰淇淋。';
final matches = ContentFilterService.checkText(text);
expect(matches, isEmpty);
});
test('学习相关文本不触发', () {
final text = '今天数学课学了乘法,我觉得很有趣。'
'老师表扬了我,说我进步很大。';
final matches = ContentFilterService.checkText(text);
expect(matches, isEmpty);
});
});
// ============================================================
// 文本预处理 — 绕过手法
// ============================================================
group('文本预处理', () {
test('空格分隔不影响检测', () {
final matches = ContentFilterService.checkText('我 要 打 死 你');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.word == '打死你'), isTrue);
});
test('特殊符号插入不影响检测', () {
final matches = ContentFilterService.checkText('废.物.垃.圾');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.word == '废物'), isTrue);
});
test('零宽字符不影响检测', () {
// U+200B 零宽空格
final matches = ContentFilterService.checkText('废​物​');
expect(matches, isNotEmpty);
});
test('下划线连字符不影响检测', () {
final matches = ContentFilterService.checkText('废_物');
expect(matches, isNotEmpty);
});
});
// ============================================================
// 谐音/变体匹配
// ============================================================
group('谐音变体匹配', () {
test('数字谐音 "4" 匹配含 "死" 的词', () {
// "去死" 在词库中 → "去4" 应触发匹配
final matches = ContentFilterService.checkText('你怎么不去4');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.word == '去死'), isTrue);
});
test('形近字 "草" 匹配含 "操" 的词', () {
// "操你" 在词库中 → "草你" 应触发匹配
final matches = ContentFilterService.checkText('我草你太牛了');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.word == '操你'), isTrue);
});
test('变体 "wc" 匹配 "卧槽"', () {
final matches = ContentFilterService.checkText('wc这个好厉害');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.word == '卧槽'), isTrue);
});
test('变体 "莎" 匹配含 "杀" 的词', () {
// "杀人" 在词库中 → "莎人" 应触发匹配
final matches = ContentFilterService.checkText('我要莎人了');
expect(matches, isNotEmpty);
expect(matches.any((m) => m.word == '杀人'), isTrue);
});
});
// ============================================================
// 边界条件
// ============================================================
group('边界条件', () {
test('空字符串返回空列表', () {
expect(ContentFilterService.checkText(''), isEmpty);
});
test('纯空格返回空列表', () {
expect(ContentFilterService.checkText(' '), isEmpty);
});
test('纯符号返回空列表', () {
expect(ContentFilterService.checkText('!@#\$%^&*'), isEmpty);
});
test('超长文本不崩溃', () {
final longText = '今天天气很好。' * 10000; // ~80,000 字符
final matches = ContentFilterService.checkText(longText);
expect(matches, isEmpty); // 正常内容
});
test('多次出现同一词精确匹配只报告一次', () {
final matches = ContentFilterService.checkText('白痴白痴');
final exactMatches = matches.where((m) => m.word == '白痴' && m.position >= 0).toList();
expect(exactMatches.length, 1);
});
});
// ============================================================
// 辅助方法
// ============================================================
group('辅助方法', () {
test('hasSensitiveContent 正确判断', () {
expect(ContentFilterService.hasSensitiveContent('你好世界'), isFalse);
expect(ContentFilterService.hasSensitiveContent('你是废物'), isTrue);
});
test('getMatchedCategories 返回分类标签', () {
final matches = ContentFilterService.checkText('废物你去死');
final categories = ContentFilterService.getMatchedCategories(matches);
expect(categories, isNotEmpty);
// 至少包含欺凌和暴力
expect(categories.any((c) => c == '欺凌'), isTrue);
expect(categories.any((c) => c == '暴力'), isTrue);
});
});
// ============================================================
// 词库完整性
// ============================================================
group('词库完整性', () {
test('8 个分类都有词', () {
expect(kSensitiveWords.length, 8);
for (final entry in kSensitiveWords.entries) {
expect(entry.value, isNotEmpty, reason: '${entry.key.label} 分类不应为空');
}
});
test('总词量 >= 100', () {
final total = kSensitiveWords.values.fold(0, (sum, list) => sum + list.length);
expect(total, greaterThanOrEqualTo(100));
});
test('谐音变体映射的 key 都在词库中', () {
final allWords = kSensitiveWords.values.expand((w) => w).toSet();
for (final key in kHomophoneVariants.keys) {
// 变体 key 应该在词库中存在(单字映射除外)
// 有些变体 key 是单字如 "死",对应词库中的 "去死" 等
expect(
allWords.any((w) => w.contains(key)),
isTrue,
reason: '变体 key "$key" 不在词库中',
);
}
});
});
}