spam_detect / examples /preprocess /process_sms_spam.py
qgyd2021's picture
update
8472d60
#!/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()