# 🧩 Summary
## 其他数据清洗技术
除了 MinHash,还有:
- **困惑度过滤:** 用小模型筛选"太简单"或"太混乱"的文本
- **毒性检测:** 过滤暴力/色情/歧视内容
- **语言检测:** 确保数据是目标语言
- **质量评分:** 基于启发式规则打分
- [[数据去重]] [[minHash]] = 快速找相似文本的算法,主要用于大规模去重!
1. 使用哈希而非原始行:用 SHA256 将行映射为固定长度哈希,节省内存
# 💡 Cues
# Notes
下面用真实用例片段+对应代码位置,直观说明两个测试如何基于 MinHash + LSH 做“精确/模糊”去重。
核心思路
- 归一化文本:小写、去标点、规整空白、去重音(normalize_text,cs336_data/deduplication.py:49-71)。
- 取词级 n-gram 集合(默认 5-gram),用多种种子计算每份文档的 MinHash 签名(cs336_data/deduplication.py:74-108)。
- LSH 分桶:把签名按 band 划分,同桶文档作为候选对(cs336_data/deduplication.py:150-173)。
- 对候选对再算真正的 n-gram Jaccard,相似度 > 阈值(0.8)则连边成“重复簇”(cs336_data/deduplication.py:184-211)。
- 每个簇保留一个文档(最小索引),输出其原文(cs336_data/deduplication.py:230-243)。
参数(两测试都用 5-gram,rows_per_band = 10)
- 精确重复:num_hashes=100, num_bands=10, jaccard_threshold=0.8(tests/test_deduplication.py:26-36)
- 模糊重复:num_hashes=500, num_bands=50, jaccard_threshold=0.8(tests/test_deduplication.py:66-76)
精确重复示例(test_minhash_deduplication_exact_duplicates)
- 文档对(完全相同内容):
- tests/fixtures/documents_with_line_duplicates/doc1.txt:1
- tests/fixtures/documents_with_line_duplicates/doc2.txt:1
- 文本内容(两文件一致):
- “Our servers have detected that you may be accessing this site with an automated crawler.”
- 归一化后(去标点、小写)示例词序列:
- our, servers, have, detected, that, you, may, be, accessing, this, site, with, an, automated, crawler
- 5-gram 示例(二者完全相同):
- “our servers have detected that”
- “servers have detected that you”
- “have detected that you may”
- …(直到“site with an automated crawler”)
- 结果
- MinHash 签名基本一致 → LSH 分到同一或多个同桶 → 进入候选对
- 真值 Jaccard(5-gram 集) = 1.0 > 0.8 → 判为重复
- 只保留簇中一个(通常是 doc1),总输出 4 个文件(tests/test_deduplication.py:37-53)
模糊重复示例(test_minhash_deduplication_fuzzy_duplicates)
- 三个文档:
- MIT 许可证(Rails 版本)tests/fixtures/documents_with_fuzzy_duplicates/rails_mit_license.txt:1
- MIT 许可证(React 版本)tests/fixtures/documents_with_fuzzy_duplicates/react_mit_license.txt:1
- PyTorch 许可证(BSD-3-Clause)tests/fixtures/documents_with_fuzzy_duplicates/pytorch_license.txt:1
- 关键相似片段(两份 MIT 几乎一致,仅抬头/署名不同):
- “Permission is hereby granted, free of charge, to any person obtaining a copy of this software…”
- “THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND…”
- 归一化后(示例 5-gram,二者重合度极高):
- “permission is hereby granted free of”
- “is hereby granted free of charge”
- “granted free of charge to”
- “free of charge to any”
- …(大量重合 5-gram 覆盖主体条款)
- 结果
- 两份 MIT 的 MinHash 签名高度相似 → 在 50 个 band 中极大概率共桶 → 成为候选对
- 真值 Jaccard(5-gram 集) ≫ 0.8 → 归为同一重复簇,仅保留其中一份
- PyTorch 许可证与 MIT 相似度低 → 单独保留
- 最终仅输出 2 份:一份 MIT + 一份 PyTorch(tests/test_deduplication.py:77-106)
流程小结(端到端)
- 候选召回(LSH)+ 严格确认(Jaccard 阈值)保障效率与准确性。
- 模糊去重依赖 n-gram 重叠覆盖主体语义;归一化消除大小写、标点、空白差异,适合“同模板/许可证/公告”类文本。
- 每簇只保留一份,按输入序的最小索引决定保留项(cs336_data/deduplication.py:230-236)。