|
from arxiv_classifier.utils.maps import (CATEGORY_ALIASES, |
|
GENERAL_CATEGORIES, |
|
IGNORED_CATEGOREIES, |
|
SUBFIELD_MAP) |
|
|
|
IGNORE = GENERAL_CATEGORIES | IGNORED_CATEGOREIES |
|
|
|
def preprocess_primary_secondary(df): |
|
""" |
|
Applies substitutions and filtering to primary and secondary subfields in the DataFrame. |
|
Adds a new column `field` to the DataFrame based on the primary subfield. |
|
(If there exists a `field` column in the original DataFrame, it will be overwritten.) |
|
|
|
Args: |
|
df (pd.DataFrame): DataFrame with columns `primary_subfield` and `secondary_subfield` |
|
|
|
Returns: |
|
pd.DataFrame: DataFrame with columns `primary_subfield`, `secondary_subfield`, and `field` |
|
""" |
|
def substitute_aliases(s): |
|
if s in CATEGORY_ALIASES: |
|
return CATEGORY_ALIASES[s] |
|
return s |
|
|
|
df.loc[:, 'primary_subfield'] = df['primary_subfield'].apply(substitute_aliases) |
|
|
|
ignore_rows = df['primary_subfield'].isin(IGNORE) |
|
df = df[~ignore_rows] |
|
|
|
|
|
def preprocess_secondary(s): |
|
subfields = [] |
|
|
|
for subfield in s.split(): |
|
|
|
new_subfield = substitute_aliases(subfield) |
|
if new_subfield == 'cs.NA': |
|
print(f"Warning:25> Subfield {new_subfield} not in SUBFIELD_MAP") |
|
|
|
if new_subfield in IGNORE: |
|
continue |
|
|
|
if new_subfield not in SUBFIELD_MAP.keys(): |
|
print(f"Warning: Subfield {new_subfield} not in SUBFIELD_MAP") |
|
subfields.append(new_subfield) |
|
|
|
return subfields |
|
|
|
|
|
df.loc[:, 'secondary_subfield'] = df['secondary_subfield'].apply(preprocess_secondary) |
|
|
|
|
|
df.loc[:, 'field'] = df['primary_subfield'].apply(lambda x: x.split('.')[0]) |
|
|
|
|
|
assert all(df['primary_subfield'].isin(SUBFIELD_MAP.keys())), df[~df['primary_subfield'].isin(SUBFIELD_MAP.keys())] |
|
|
|
|
|
|
|
return df |
|
|