File size: 1,220 Bytes
31ccd6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()