victorian_authorship / convert.py
contemmcm's picture
Upload 7 files
31ccd6c verified
import csv
import random
import pandas as pd
def read_train_data():
rows = []
with open(
"Gungor_2018_VictorianAuthorAttribution_data-train.csv", "r", encoding="latin1"
) as f:
csv_reader = csv.reader(f)
header = next(csv_reader)
for row in csv_reader:
rows.append({"text": row[0], "author": int(row[1]) - 1})
return rows
def read_test_data():
with open("test_labels.txt", "r") as f:
labels = [label.strip() for label in f.readlines()]
with open(
"Gungor_2018_VictorianAuthorAttribution_data.csv", "r", encoding="latin1"
) as f:
lines = f.readlines()
texts = [line.strip() for line in lines[1:]] # skip header
rows = []
for text, label in zip(texts, labels):
rows.append({"text": text, "author": int(label) - 1})
return rows
def run():
train_rows = read_train_data()
test_rows = read_test_data()
all_rows = train_rows + test_rows
random.shuffle(all_rows)
pd.DataFrame(train_rows).to_parquet("train.parquet")
pd.DataFrame(test_rows).to_parquet("test.parquet")
pd.DataFrame(all_rows).to_parquet("complete.parquet")
if __name__ == "__main__":
run()