Download examples/preprocess/process_sms_spam.py from intelli-zen/spam_detect: direct link, hf CLI and curl.
- Browser
- Download file 1.72 kB
-
https://huggingface.co/datasets/intelli-zen/spam_detect/resolve/main/examples/preprocess/process_sms_spam.py
- Command line
-
hf download hf://datasets/intelli-zen/spam_detect/examples/preprocess/process_sms_spam.py
-
curl -L -o process_sms_spam.py https://huggingface.co/datasets/intelli-zen/spam_detect/resolve/main/examples/preprocess/process_sms_spam.py
1.72 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="sms_spam", 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/sms_spam.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 sample in tqdm(dataset_dict["train"]): | |
| # print(sample) | |
| text = sample["sms"] | |
| label = sample["label"] | |
| text = text.strip() | |
| label = "spam" if label == 1 else "ham" | |
| if label not in ("spam", "ham"): | |
| raise AssertionError | |
| row = { | |
| "text": text, | |
| "label": label, | |
| "category": None, | |
| "data_source": "sms_spam", | |
| "split": "train" | |
| } | |
| row = json.dumps(row, ensure_ascii=False) | |
| f.write("{}\n".format(row)) | |
| return | |
| if __name__ == '__main__': | |
| main() | |