forked from vzhou842/profanity-check
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.sync.py
More file actions
72 lines (60 loc) · 2.18 KB
/
data.sync.py
File metadata and controls
72 lines (60 loc) · 2.18 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import re
import pandas as pd
# %%
clear_data = pd.read_csv('./profanity_protector/data/clean_data.csv')
len(clear_data)
offensive = clear_data[clear_data["is_offensive"] == 1]
normal = clear_data[clear_data["is_offensive"] == 0]
print(len(offensive))
print(len(normal))
print(len(normal)/(len(offensive)+len(normal)))
def add_to_df(base, to_add, is_offensive):
return pd.concat([base, pd.DataFrame(
[{"text": x, "is_offensive": is_offensive} for x in to_add])])
# %%
df = pd.read_csv("data/surgeai_list.csv", sep=",")
texts_1 = list(df["Canonical Form 1"].astype(str))
texts_2 = list(df[~df["Canonical Form 2"].notna()
]["Canonical Form 2"].astype(str))
texts_3 = list(df[~df["Canonical Form 3"].notna()
]["Canonical Form 3"].astype(str))
texts = texts_1 + texts_2 + texts_3
print(f"Surge adds {len(texts)}")
clear_data = add_to_df(clear_data, texts, 1)
len(clear_data)
# %%
df = pd.read_csv("data/kaggle_hate_speech.csv", sep=",")
pattern = r'@\w+:?'
good = list(df[df["class"] == 2]["tweet"].apply(
lambda x: re.sub('"', '', x)).apply(lambda x: re.sub(pattern, '', x).strip()))
bad = list(df[df["class"] == 1]["tweet"].apply(
lambda x: re.sub('"', '', x)).apply(lambda x: re.sub(pattern, '', x).strip()))
print(f"Kaggle adds good {len(good)}")
print(f"Kaggle adds bad {len(bad)}")
clear_data = add_to_df(clear_data, good, 0)
clear_data = add_to_df(clear_data, bad, 1)
len(clear_data)
# %%
words = list(pd.read_csv("data/words.csv", sep=",")["word"])
print(f"MIT adds {len(words)}")
clear_data = add_to_df(clear_data, words, 0)
len(clear_data)
# %%
offensive = clear_data[clear_data["is_offensive"] == 1]
normal = clear_data[clear_data["is_offensive"] == 0]
print(len(offensive))
print(len(normal))
print(len(normal)/(len(offensive)+len(normal)))
# %%
cd_len = len(clear_data)
clear_data = clear_data[clear_data["text"].notna()].sample(
frac=1).reset_index(drop=True)
print(f"NaN entries: {cd_len-len(clear_data)}")
train_split = 1
cd_len = len(clear_data)
train_len = int(cd_len*train_split)
clear_data\
.iloc[:train_len].to_csv("data/train_data.csv", index=False)
clear_data\
.iloc[train_len:].to_csv("data/test_data.csv", index=False)
clear_data