|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from argparse import ArgumentParser |
|
parser = ArgumentParser() |
|
|
|
|
|
parser.add_argument("--output_path", "-op", type=str, default="./") |
|
parser.add_argument("--input_path", "-ip", type=str, default="./data", |
|
help="Path to the input data files") |
|
args, opt = parser.parse_known_args() |
|
|
|
dataset = 'all2023' |
|
|
|
output_path = args.output_path |
|
input_path = args.input_path |
|
|
|
|
|
|
|
|
|
|
|
import pandas as pd |
|
import os |
|
import sys |
|
from tqdm import tqdm |
|
from datetime import datetime |
|
from util_preprocess import preprocess_primary_secondary |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file_path = os.path.join( |
|
input_path, |
|
'raw', |
|
'all2023', |
|
'all-2023-v2.tsv' |
|
) |
|
save_dir = os.path.join(output_path, dataset) |
|
os.makedirs(save_dir, exist_ok=True) |
|
train_save_path = os.path.join(save_dir, "train.json") |
|
test_save_path = os.path.join(save_dir, "test.json") |
|
print("Reading from file: ", file_path) |
|
|
|
FEATURES = [ |
|
'paper_id', |
|
'version', |
|
'yymm', |
|
'created', |
|
'title', |
|
'secondary_subfield', |
|
'abstract', |
|
'primary_subfield', |
|
'field', |
|
'fulltext' |
|
] |
|
def get_date(paper): |
|
|
|
paper_id = paper['paper_id'] |
|
|
|
yy = paper_id[:2] |
|
|
|
mm = paper_id[2:4] |
|
|
|
date_obj = datetime(year=int(f"20{yy}"), month=int(mm), day=1) |
|
|
|
return date_obj |
|
|
|
def get_fulltext(row): |
|
filename = f"{row['paper_id']}v{row['version']}.txt" |
|
filepath = os.path.join( |
|
input_path, |
|
'raw', |
|
'all2023', |
|
'fulltext', |
|
row['paper_id'].split('.')[0], |
|
filename, |
|
) |
|
try: |
|
with open(filepath, 'r') as f: |
|
fulltext = f.read() |
|
except FileNotFoundError: |
|
fulltext = None |
|
return fulltext |
|
|
|
count_orig = [] |
|
count_isvalid = [] |
|
count_hasfulltext = [] |
|
|
|
for i, original_df in tqdm(enumerate(pd.read_csv(file_path, sep='\t', chunksize=10, dtype=str, quoting=3))): |
|
count_orig.append( len(original_df) ) |
|
|
|
original_df.loc[:, 'yymm'] = original_df['paper_id'].apply(lambda x: x[:4]) |
|
|
|
original_df.loc[:, 'created'] = original_df.apply(lambda x: get_date(x), axis=1) |
|
|
|
for feature in FEATURES: |
|
if feature not in original_df.columns: |
|
original_df[feature] = "" |
|
|
|
original_df.loc[:, 'primary_subfield'] = original_df['categories'].apply(lambda x : x.split()[0]) |
|
|
|
original_df.loc[:, 'secondary_subfield'] = original_df['categories'] |
|
|
|
df = preprocess_primary_secondary(original_df) |
|
|
|
count_isvalid.append( len(df) ) |
|
|
|
df.loc[:, 'fulltext'] = df.apply(get_fulltext, axis=1) |
|
|
|
df = df.drop(columns=['categories']) |
|
|
|
df = df.dropna(subset=['fulltext']) |
|
count_hasfulltext.append( len(df) ) |
|
|
|
|
|
assert all([feature in df.columns for feature in FEATURES]), f"Missing features in df: {FEATURES}" |
|
|
|
|
|
DATE_CUTOFF = datetime(2023, 7, 1) |
|
if i == 0: |
|
|
|
df[df['created'] < DATE_CUTOFF].to_json(train_save_path, lines=True, orient="records") |
|
|
|
df[df['created'] >= DATE_CUTOFF].to_json(test_save_path, lines=True, orient="records") |
|
else: |
|
|
|
df[df['created'] < DATE_CUTOFF].to_json(train_save_path, lines=True, orient="records", mode='a') |
|
|
|
df[df['created'] >= DATE_CUTOFF].to_json(test_save_path, lines=True, orient="records", mode='a') |
|
|
|
|
|
df_err = pd.DataFrame({ |
|
'orig': count_orig, |
|
'isvalid': count_isvalid, |
|
'hasfulltext': count_hasfulltext, |
|
}) |
|
df_err.to_csv(f"error_log.csv", index=False) |
|
|
|
|