Download examples/preprocess/process_spam_detection.py from intelli-zen/spam_detect: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/datasets/intelli-zen/spam_detect/resolve/main/examples/preprocess/process_spam_detection.py
- Command line
-
hf download hf://datasets/intelli-zen/spam_detect/examples/preprocess/process_spam_detection.py
-
curl -L -o process_spam_detection.py https://huggingface.co/datasets/intelli-zen/spam_detect/resolve/main/examples/preprocess/process_spam_detection.py
1.83 kB
| #!/usr/bin/python3 | |
| # -*- coding: utf-8 -*- | |
| import argparse | |
| from collections import defaultdict | |
| import json | |
| import os | |
| from pathlib import Path | |
| import random | |
| import re | |
| import sys | |
| pwd = os.path.abspath(os.path.dirname(__file__)) | |
| sys.path.append(os.path.join(pwd, '../../')) | |
| from datasets import load_dataset | |
| from tqdm import tqdm | |
| from project_settings import project_path | |
| def get_args(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--dataset_path", default="Deysi/spam-detection-dataset", type=str) | |
| parser.add_argument( | |
| "--dataset_cache_dir", | |
| default=(project_path / "hub_datasets").as_posix(), | |
| type=str | |
| ) | |
| parser.add_argument( | |
| "--output_file", | |
| default=(project_path / "data/spam_detection.jsonl"), | |
| type=str | |
| ) | |
| args = parser.parse_args() | |
| return args | |
| def main(): | |
| args = get_args() | |
| dataset_dict = load_dataset( | |
| path=args.dataset_path, | |
| cache_dir=args.dataset_cache_dir, | |
| ) | |
| print(dataset_dict) | |
| with open(args.output_file, "w", encoding="utf-8") as f: | |
| for split, dataset in dataset_dict.items(): | |
| for sample in tqdm(dataset): | |
| # print(sample) | |
| text = sample["text"] | |
| label = sample["label"] | |
| label = "spam" if label == "spam" else "ham" | |
| if label not in ("spam", "ham"): | |
| raise AssertionError | |
| row = { | |
| "text": text, | |
| "label": label, | |
| "category": None, | |
| "data_source": "spam_detection", | |
| "split": split | |
| } | |
| row = json.dumps(row, ensure_ascii=False) | |
| f.write("{}\n".format(row)) | |
| return | |
| if __name__ == '__main__': | |
| main() | |