
pip install text-dedupまたは
pip install git+https://github.com/ChenghaoMou/text-dedupgithubページ
このリポジトリには、使用可能なテキスト重複排除スクリプトのコレクションが含まれています。
私はまた、将来の大きな計画を持っています:
しかし、私は汎用延長ライブラリを構築するつもりはありません。これは、このレポの目標でした。私は徐々にPYPIパッケージを引退します。その背後にある理由は、各ユースケースが大きく異なる可能性があり、慎重な設計と考慮が必要であるためです。最初にスクリプトを読むことを心からお勧めします(比較的短いです)。それを使用して、独自のスクリプトをブートストラップするか、参照として使用することもできます。
このリポジトリは、以下のプロジェクトに触発されており、BigScience(Apache 2.0)とBigCode(Apache 2.0)への私自身の参加から学んだ教訓の影響を強く受けています。旅についてのブログ投稿があります。フィードバックは大歓迎です!
最初にあなた自身のプロジェクトとデータセットのためにtext_dedup/minhash_spark.pyを変更してください!
「./temp-data」の下にダウンロードされたデータセット(Parquetファイル)があると仮定すると、次のようなローカルコンピューティングでファイルを使用して処理できます。
export PYSPARK_PYTHON= " path to your python with scipy, xxhash, and numpy installed "
spark-submit --executor-memory 16g
--driver-memory 20g
--executor-cores 3
--num-executors 2
--packages graphframes:graphframes:0.8.2-spark3.2-s_2.12
--conf " spark.executor.extraJavaOptions=-Dlog4j.configuration=./log4j.properties "
--conf " spark.driver.extraJavaOptions=-Dlog4j.configuration=./log4j.properties "
text_dedup/minhash_spark.py
--input " ./temp-data "
--output " ./temp-output "
--column " text "
--threshold 0.7 DEBUG __main__ - ------------------------------------------------------------------------------------------------------------------------
DEBUG __main__ - Using B=25, R=10
DEBUG __main__ - Loaded documents: 88803
DEBUG __main__ - args.input='./temp-data'
DEBUG __main__ - args.output='./temp-output'
DEBUG __main__ - args.threshold=0.7
DEBUG __main__ - args.ngram_size=5
DEBUG __main__ - args.min_length=5
DEBUG __main__ - args.num_perm=250
DEBUG __main__ - args.column='text'
DEBUG __main__ - id : bigint
DEBUG __main__ - text : string
DEBUG __main__ - meta : struct<warc_headers:struct<warc-record-id:string,warc-date:string,content-type:string,content-length:int,warc-type:string,warc-identified-content-language:string,warc-refers-to:string,warc-target-uri:string,warc-block-digest:string>,identification:struct<label:string,prob:float>,annotations:array<string>,line_identifications:array<struct<label:string,prob:float>>>
DEBUG __main__ - __id__ : bigint
DEBUG __main__ - ------------------------------------------------------------------------------------------------------------------------
DEBUG __main__ - Initial edges: 52102
DEBUG __main__ - Edges DataFrame: 52102
DEBUG __main__ - Vertices DataFrame: 50206
DEBUG __main__ - Assignment DataFrame: 50206
DEBUG __main__ - Merging records: 88803
INFO __main__ - Saving with 1 partitions and 44092 rows each
DEBUG __main__ - ------------------------------------------------------------------------------------------------------------------------
DEBUG __main__ - Number of rows before: 88803
DEBUG __main__ - Number of rows after: 44092
DEBUG __main__ - Percentage of rows kept: 49.65%
DEBUG __main__ - Output: ./temp-output
DEBUG __main__ - Time: 68.80s
DEBUG __main__ - ------------------------------------------------------------------------------------------------------------------------
または、GCP Dataprocでジョブを実行する方法について、BigCode-V2/run.shをご覧ください。
GoogleのRetSIMモデル(Github、Arxiv)に基づいて、近親者に近い方法に基づいた埋め込みです。
大規模なデータセットの場合、迅速な推論にはGPUが必要になります。
python text_dedup/ann_unisim.py --path truthful_qa --name generation --split validation --output temp --column question出力:
INFO Load Dataset : 5.56s
INFO Index Dataset : 8.13s
INFO Clustering : 8.72s
INFO Filtering : 0.35s
INFO Saving : 0.01s
INFO Cleaning : 0.00s
INFO Total : 22.77s
INFO Before : 817
INFO After : 788
# input
python -m text_dedup.suffix_array
--path " oscar-corpus/OSCAR-2201 "
--name " gl "
--split " train "
--cache_dir " ./cache "
--output " output/suffix_array/oscar_gl_dedup "
--column " text "
--google_repo_path " /Users/chenghao/Downloads/Projects/text-dedup/deduplicate-text-datasets "
--use_auth_token true
# output
INFO Loading : 2.75 seconds
INFO Preprocessing : 4.78 seconds
INFO SuffixArray : 98.29 seconds
INFO SelfSimilar : 4.24 seconds
INFO Restore : 0.25 seconds
INFO Deduplicate : 6.23 seconds
INFO Saving : 8.91 seconds
INFO Total : 125.45 seconds
INFO Before : 180332342 bytes (88803)
INFO After : 97646271 bytes (40404) # input
python -m text_dedup.minhash
--path " oscar-corpus/OSCAR-2201 "
--name " gl "
--split " train "
--cache_dir " ./cache "
--output " output/minhash/oscar_gl_dedup "
--column " text "
--batch_size 10000
--use_auth_token true
# output
INFO Loading : 2.62 seconds
INFO MinHashing : 0.08 seconds
INFO Clustering : 2.20 seconds
INFO Filtering : 0.53 seconds
INFO Saving : 9.86 seconds
INFO Total : 15.29 seconds
INFO Data Number (before) : 88803
INFO Data Number (after) : 44124 (49.69%)
INFO Duplicate Number : 44679 (50.31%)
INFO ? Happy Deduplicating ? # input
python -m text_dedup.simhash
--path " oscar-corpus/OSCAR-2201 "
--name " gl "
--split " train "
--cache_dir " ./cache "
--output " output/simhash/oscar_gl_dedup "
--column " text "
--batch_size 10000
--use_auth_token true
# output
INFO Loading : 2.60 seconds
INFO SimHashing : 0.04 seconds
INFO Indexing : 28.88 seconds
INFO Filtering : 0.88 seconds
INFO Saving : 10.41 seconds
INFO Total : 42.80 seconds
INFO Data Number (before) : 88803
INFO Data Number (after) : 46163 (51.98%)
INFO Duplicate Number : 42640 (48.02%)
INFO ? Happy Deduplicating ? # input
python -m text_dedup.exact_hash
--path " oscar-corpus/OSCAR-2201 "
--name " gl "
--split " train "
--cache_dir " ./cache "
--output " output/exact_hash/oscar_gl_dedup "
--column " text "
--batch_size 1000
--use_auth_token true
# output
INFO Loading : 2.95s
INFO Processing : 3.79s
INFO Filtering : 0.10s
INFO Saving : 2.89s
INFO Total : 9.72s
INFO Before : 88803
INFO After : 47049 # input
python -m text_dedup.bloom_filter
--path " oscar-corpus/OSCAR-2201 "
--name " gl "
--split " train "
--cache_dir " ./cache "
--output " output/bloom_filter/oscar_gl_dedup "
--error_rate 1e-5
--column " text "
--use_auth_token true --batch_size 1000
# output
INFO Loading : 2.72s
INFO Processing : 4.84s
INFO Filtering : 0.10s
INFO Saving : 2.88s
INFO Total : 10.54s
INFO Before : 88803
INFO After : 47045注記
Sparkの実装には小さなデータセット用のオーバーヘッドがあるため、大きなデータセットと十分な計算リソースがある場合にのみ、スクリプトを使用することをお勧めします。
複製については、 tests/benchmark_core.pyを参照してください。
| アルゴリズム | 精度(複製) | リコール(複製) | 精度(非重複) | リコール(非重複) | マクロF1スコア | 正確さ | 時間 |
|---|---|---|---|---|---|---|---|
| ユニシム | 0.9307 | 0.8924 | 0.9055 | 0.9394 | 0.9181 | 0.9054 | 1305.79S |
| ミンハッシュスパーク | 0.957 | 0.9445 | 0.9471 | 0.959 | 0.952 | 0.9202 | 691.77S |
| ミンハッシュ | 0.9594 | 0.9445 | 0.9474 | 0.9616 | 0.9534 | 0.924 | 18.88S |
| Simhash | 0.9042 | 0.721 | 0.792 | 0.9329 | 0.8481 | 0.8321 | 644.36s |
| 正確なタイトル | 0.8302 | 0.5521 | 0.7098 | 0.9065 | 0.77 | 0.7456 | - |
| 正確なタイトルマッチング1 | 0.830 | 0.50 | 0.709 | 0.992 | 0.757 | 0.746 | - |
| Simhashマッチング1 | 0.697 | 0.247 | 0.598 | 0.985 | 0.631 | 0.616 | - |
| ドキュメントベクトルの類似性1 | 0.912 | 0.779 | 0.861 | 0.986 | 0.885 | 0.883 | - |
| ハイブリッド方法1 | 0.908 | 0.828 | 0.899 | 0.979 | 0.904 | 0.903 | - |
| ラボ2 | 0.937 | 0.923 | 0.930 | 0.943 | 0.933 | 0.919 | - |
| 多言語使用2 | 0.917 | 0.907 | 0.918 | 0.927 | 0.917 | 0.909 | - |
| 多言語E5ベース2 | 0.931 | 0.908 | 0.919 | 0.939 | 0.924 | 0.920 | - |
| Minhash + LSH 2 | 0.929 | 0.902 | 0.915 | 0.938 | 0.921 | 0.918 | - |
| Retsim Partial-Dup 2 | 0.945 | 0.941 | 0.945 | 0.949 | 0.945 | 0.928 | - |
| Retsim近Dup2 | 0.928 | 0.937 | 0.942 | 0.934 | 0.935 | 0.926 | - |
複製については、 tests/benchmark_news.pyを参照してください。
ニュースコピーデータセットの調整済みRANDインデックス(ARI):
| モデル/アルゴリズム | アリ |
|---|---|
| Simhash | 0.612 |
| ミンハッシュ(火花) | 0.740 |
| ミンハッシュ | 0.742 |
| Retsim Near-Dup + Ann* | 0.051 |
| n-gram 3 | 0.440 |
| Simhash2 | 0.695 |
| ミンハッシュ3 | 0.737 |
| ミンハッシュ2 | 0.783 |
| 多言語使用2 | 0.730 |
| 多言語E5ベース2 | 0.742 |
| S-Bert 3 | 0.700 |
| Retsim Partial-Dup 2 | 0.831 |
| Retsim近Dup2 | 0.704 |
| 再ランク3 | 0.937 |
| Bi-Encoder 3 | 0.915 |
*:紙の結果を再現できないようです。
Apache 2.0
通常、このリポジトリを次のように引用できます。
@software { chenghao_mou_2023_8364980 ,
author = { Chenghao Mou and
Chris Ha and
Kenneth Enevoldsen and
Peiyuan Liu } ,
title = { ChenghaoMou/text-dedup: Reference Snapshot } ,
month = sep,
year = 2023 ,
publisher = { Zenodo } ,
version = { 2023.09.20 } ,
doi = { 10.5281/zenodo.8364980 } ,
url = { https://doi.org/10.5281/zenodo.8364980 }
}Sparkバージョンは、BigCode(Apache 2.0)とBigScience(Apache 2.0)から生まれました。必要に応じて、元の論文を引用できます。
@article {
kocetkov2023the,
title = { The Stack: 3 {TB} of permissively licensed source code } ,
author = { Denis Kocetkov and Raymond Li and Loubna Ben allal and Jia LI and Chenghao Mou and Yacine Jernite and Margaret Mitchell and Carlos Mu{~n}oz Ferrandis and Sean Hughes and Thomas Wolf and Dzmitry Bahdanau and Leandro Von Werra and Harm de Vries } ,
journal = { Transactions on Machine Learning Research } ,
issn = { 2835-8856 } ,
year = { 2023 } ,
url = { https://openreview.net/forum?id=pxpbTdUEpD } ,
note = { }
}地域の敏感なハッシュと単語の埋め込みを使用した学術文書の重複数↩2↩3↩4
Retsim :回復力と効率的なテキストの類似性
スケールでのノイズ-ロバスト脱体複合