File size: 1,065 Bytes
78de534
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os

import pandas as pd
from datasets import load_dataset

RAW_CSV_PATH = "data/raw/jigsaw_train_hate_annotationprob.csv"
PARQUET_PATH = "data/raw/jigsaw.parquet"
HF_COLS = [
	"comment_text",
	"toxic",
	"severe_toxic",
	"obscene",
	"threat",
	"insult",
	"identity_hate",
]
CSV_COLS = [
	"text",
	"toxic",
	"severe_toxic",
	"obscene",
	"threat",
	"insult",
	"identity_hate",
]


def _load_from_csv() -> pd.DataFrame:
	df = pd.read_csv(RAW_CSV_PATH)
	df = df[CSV_COLS].dropna()
	return df.rename(columns={"text": "comment_text"})


def _load_from_hf() -> pd.DataFrame:
	dataset = load_dataset("jigsaw_toxicity_pred", split="train")
	dataset = dataset.select_columns(HF_COLS)
	return dataset.to_pandas().dropna()


def main() -> None:
	if os.path.exists(RAW_CSV_PATH):
		df = _load_from_csv()
		source = RAW_CSV_PATH
	else:
		df = _load_from_hf()
		source = "jigsaw_toxicity_pred"

	print(f"Loaded: {len(df)} comments from {source}")
	print(f"Toxic rate: {df['toxic'].mean():.1%}")

	df.to_parquet(PARQUET_PATH, index=False)


if __name__ == "__main__":
	main()