spam_detect / examples /preprocess /process_enron_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="SetFit/enron_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/enron_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 split, dataset in dataset_dict.items():
for sample in tqdm(dataset):
# print(sample)
# text = sample["text"]
subject = sample["subject"]
message = sample["message"]
label = sample["label_text"]
text = "{}\n\n{}".format(subject, message)
if label not in ("spam", "ham"):
raise AssertionError
row = {
"text": text,
"label": label,
"category": None,
"data_source": "enron_spam",
"split": split
}
row = json.dumps(row, ensure_ascii=False)
f.write("{}\n".format(row))
return
if __name__ == '__main__':
main()