|
def download_civilcomments(data_path: str | Path) -> None: |
|
print("Downloading CivilComments...") |
|
civilcomments_dir = Path(data_path) / "civilcomments" |
|
civilcomments_dir.mkdir(parents=True, exist_ok=True) |
|
download_and_extract( |
|
"https://worksheets.codalab.org/rest/bundles/0x8cd3de0634154aeaad2ee6eb96723c6e/contents/blob/", |
|
str(civilcomments_dir / "civilcomments.tar.gz"), |
|
) |
|
|
|
|
|
def process_civilcomments(data_path: str | Path, dst: str | Path, keep_raw: bool = False) -> None: |
|
print("Processing CivilComments...") |
|
df = pd.read_csv(Path(data_path) / "civilcomments/all_data_with_identities.csv", index_col=0) |
|
if keep_raw: |
|
ds = Dataset.from_pandas(df, preserve_index=False) |
|
|
|
|
|
ds.save_to_disk(str(Path(dst) / "civilcomments-wilds" / "raw")) |
|
|
|
|
|
ds.push_to_hub("<repo name>", "raw") |
|
|
|
|
|
input_output_vars = ["id", "split", "comment_text", "toxicity"] |
|
identity_vars = ["male", "female", "LGBTQ", "christian", "muslim", "other_religions", "black", "white"] |
|
auxiliary_vars = [ |
|
"identity_any", |
|
"severe_toxicity", |
|
"obscene", |
|
"threat", |
|
"insult", |
|
"identity_attack", |
|
"sexual_explicit", |
|
] |
|
|
|
|
|
df = df.loc[df[input_output_vars[-2:]].isna().sum(1) == 0] |
|
|
|
|
|
df = df.loc[df[input_output_vars[-1]] >= 0] |
|
|
|
|
|
df = df.loc[:, input_output_vars + identity_vars + auxiliary_vars] |
|
|
|
|
|
cols = [input_output_vars[-1]] + identity_vars + auxiliary_vars |
|
df[cols] = (df[cols] >= 0.5).astype(int) |
|
|
|
|
|
|
|
gdf = df.groupby("comment_text")[identity_vars + ["split", "toxicity"]].agg("nunique") |
|
gdf["multiple"] = (gdf != 1).sum(1) |
|
|
|
print(f""" |
|
There are {df["comment_text"].duplicated().sum()} exact duplicates (i.e., same `comment_text`). |
|
Of these, only {len(gdf.query("multiple > 0"))} are unique `comment_text`. |
|
|
|
Some duplicates appear with different attributes and labels, and some even in multiple splits. |
|
In particular, |
|
|
|
{(gdf[identity_vars + ["split", "toxicity"]] > 1).sum()} |
|
""") |
|
|
|
|
|
|
|
|
|
|
|
|
|
print(f"Length before deduplication: {len(df)}") |
|
df = ( |
|
df.sort_values(["comment_text", "split", "toxicity"] + identity_vars + auxiliary_vars, ascending=False) |
|
.drop_duplicates(subset="comment_text", keep="first") |
|
) |
|
print(f"Length after deduplication: {len(df)}") |
|
|
|
|
|
df = ( |
|
df.assign(active_attributes=lambda _df: _df[identity_vars].values.tolist()) |
|
.assign( |
|
active_attributes=lambda _df: _df["active_attributes"].map( |
|
lambda lst: [name for idx, name in zip(lst, identity_vars, strict=True) if idx == 1] |
|
) |
|
) |
|
) |
|
|
|
|
|
|
|
assert ((df[identity_vars].sum(1) != 0) == (df["active_attributes"].map(len) > 0)).all() |
|
df["has_active_attrs"] = df[identity_vars].sum(1) != 0 |
|
|
|
|
|
df["uid"] = list(range(len(df))) |
|
|
|
|
|
cols = df.columns.tolist() |
|
df = df[["uid"] + input_output_vars + ["has_active_attrs", "active_attributes"] + identity_vars + auxiliary_vars] |
|
|
|
|
|
ds_dict = {} |
|
for split in df["split"].unique().tolist(): |
|
ds = Dataset.from_pandas(df.query(f"split == '{split}'").drop(columns=["split"]), preserve_index=False) |
|
ds = ds.cast_column("toxicity", ClassLabel(num_classes=2, names=["non-toxic", "toxic"])) |
|
ds_dict[split if split != "val" else "validation"] = ds |
|
ds_dict = DatasetDict(ds_dict) |
|
|
|
|
|
ds_dict.save_to_disk(str(Path(dst) / "civilcomments-wilds" / "texts")) |
|
|
|
|
|
ds_dict.push_to_hub("<repo name>", "default") |