diff --git a/near_deduplication/minhash_deduplication.py b/near_deduplication/minhash_deduplication.py index e423541..e442eed 100644 --- a/near_deduplication/minhash_deduplication.py +++ b/near_deduplication/minhash_deduplication.py @@ -4,6 +4,7 @@ # created : 10/4/22 from __future__ import annotations +import json import gc import hashlib import logging @@ -43,6 +44,19 @@ logger.setLevel(logging.INFO) datasets.logging.set_verbosity_error() +def fix_json(injsfile, outjsfile): + with open(injsfile, "rt") as fin: + lines = fin.readlines() + + fixed_lines = [] + for line in lines: + crt = json.dumps(json.loads(line)) + "\n" + fixed_lines.append(crt) + + with open(outjsfile, "wt") as fout: + fout.writelines(fixed_lines) + + def ngrams(sequence: List[str], n: int, min_ngram_size: int) -> Iterable: """ Directly taken from nltk package to avoid dependency. @@ -222,6 +236,7 @@ if __name__ == "__main__": threshold: float = typer.Option(0.7, help="Minhash threshold"), min_ngram_size: int = typer.Option(5, help="Shorter documents will be removed"), output: str = typer.Option(None, help="Store the deduplicated dataset"), + dedupjson: str = typer.Option(None, help="Store the deduplicated dataset as json"), ): global uf OUTPUT_BASE = Path(output or "output") @@ -238,16 +253,7 @@ if __name__ == "__main__": HASH_TABLES = [defaultdict(set) for _ in range(B)] time_measures["load_dataset"] = time.time() - ds = load_dataset( - dataset, - config, - data_dir=data_dir, - split=split, - use_auth_token=True, - cache_dir=cache_dir, - revision=revision, - num_proc=os.cpu_count(), - ) + ds = load_dataset("json", data_files=dataset)["train"] time_measures["load_dataset"] = time.time() - time_measures["load_dataset"] DATA_SIZE = len(ds) PERMUTATIONS = np.array( @@ -322,7 +328,8 @@ if __name__ == "__main__": time_measures["save"] = time.time() final_data = final_data.remove_columns(["__cluster__"]) - final_data.save_to_disk(output) + final_data.to_json(output) + fix_json(output, dedupjson) time_measures["save"] = time.time() - time_measures["save"] FINAL_DATA_SIZE = len(final_data)