-
Notifications
You must be signed in to change notification settings - Fork 1
/
construct_datafiles.py
156 lines (142 loc) · 7.18 KB
/
construct_datafiles.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""Make a zip file for submission."""
import os
from tkinter import Y
import pandas as pd
import sys
import util
SRC_EXT = '.xlsx'
def make_datafiles():
script_dir = os.path.dirname(os.path.realpath(__file__))
pages = []
isbns = []
levels = []
titles = []
wc = []
batch = []
page_nums = []
print('Walking files...')
book_list_df = pd.read_csv('../cs229_sp22_dataset/book_list.csv')
isbn_title_dictionary = dict(zip(book_list_df.ISBN, book_list_df.Title))
isbn_level_dictionary = dict(zip(book_list_df.ISBN, book_list_df.Level))
all_isbn_strings = set([str(s) for s in isbn_level_dictionary.keys()])
for base_path, dir_names, file_names in os.walk('../cs229_sp22_dataset'):
for file_name in file_names:
if file_name.endswith(SRC_EXT):
# Read file
file_path = os.path.join(base_path, file_name)
rel_path = os.path.relpath(file_path, script_dir)
batch_name = None
page_loc = None
if 'batch1' in rel_path:
df = pd.read_excel(rel_path, header=None)
curr_pages = df[1]
if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
begin = rel_path.rfind('/')
else:
begin = rel_path.rfind('\\')
end = rel_path.rfind('.')
isbn = rel_path[begin + 1:end]
if isbn in all_isbn_strings:
page = 1
for pg in curr_pages:
pg = str(pg)
if pg != pg:
pg = ""
isbns.append(isbn)
pages.append(pg)
wc.append(len(util.split(pg)))
levels.append(isbn_level_dictionary[int(isbn)])
titles.append(isbn_title_dictionary[int(isbn)])
batch.append('batch1')
page_nums.append(page)
page += 1
if 'batch2' in rel_path:
df = pd.read_excel(rel_path, header=None)
curr_pages = df[0]
if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
begin = rel_path.rfind('/')
else:
begin = rel_path.rfind('\\')
end = rel_path.rfind('.')
isbn = rel_path[begin + 1:end]
if isbn in all_isbn_strings:
page = 1
for pg in curr_pages:
pg = str(pg)
if pg != pg:
pg = ""
isbns.append(isbn)
pages.append(pg)
wc.append(len(util.split(pg)))
levels.append(isbn_level_dictionary[int(isbn)])
titles.append(isbn_title_dictionary[int(isbn)])
batch.append('batch2')
page_nums.append(page)
page += 1
if 'batch3' in rel_path:
df = pd.read_excel(rel_path, header=0)
curr_pages = df['Text']
if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
begin = rel_path.rfind('/')
else:
begin = rel_path.rfind('\\')
end = rel_path.rfind('.')
isbn = rel_path[begin + 1:end]
if isbn in all_isbn_strings:
page = 1
for pg in curr_pages:
pg = str(pg)
if pg != pg:
pg = ""
isbns.append(isbn)
pages.append(pg)
wc.append(len(util.split(pg)))
levels.append(isbn_level_dictionary[int(isbn)])
titles.append(isbn_title_dictionary[int(isbn)])
batch.append('batch3')
page_nums.append(page)
page += 1
if 'batch4' in rel_path:
df = pd.read_excel(rel_path, header=0)
curr_pages = df['Text']
if sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
begin = rel_path.rfind('/')
else:
begin = rel_path.rfind('\\')
end = rel_path.rfind('.')
isbn = rel_path[begin + 1:end]
if isbn in all_isbn_strings:
page = 1
for pg in curr_pages:
pg = str(pg)
if pg != pg:
pg = ""
isbns.append(isbn)
pages.append(pg)
wc.append(len(util.split(pg)))
levels.append(isbn_level_dictionary[int(isbn)])
titles.append(isbn_title_dictionary[int(isbn)])
batch.append('batch4')
page_nums.append(page)
page += 1
print('Verifying lengths...')
print('All lengths equal?')
lengths = [len(x) for x in [isbns, titles, pages, wc, batch, page_nums, levels]]
print(lengths)
print('Writing datasets...')
df_dict = {'isbn': isbns, 'title': titles, 'page_text': pages, 'page_word_count': wc, 'batch': batch, 'page_num': page_nums, 'level': levels }
df_separate_levels = pd.DataFrame(df_dict)
df_separate_levels.to_csv('../cs229_sp22_dataset/full_processed_dataset.csv')
df_pooled_levels = df_separate_levels.copy()
df_pooled_levels["level"].replace({"A": "X", "B": "X", "C": "X", "D": "Y", "E": "Y", "F": "Y", "G": "Y", "H": "Y", "I": "Y", "J": "Z", "K": "Z", "L": "Z", "M": "Z", "N" : "Z"}, inplace=True)
df_pooled_levels.to_csv('../cs229_sp22_dataset/full_processed_dataset_pooled.csv')
df_separate_reduced = df_separate_levels[['level', 'page_text']].copy()
df_separate_reduced.to_csv('../cs229_sp22_dataset/level_to_page_pooled.tsv', header=None, index=(), sep='\t', mode='w')
df_separate_reduced.to_csv('../cs229_sp22_dataset/level_to_page.csv', header=None, index=(), mode='w')
df_pooled_reduced = df_separate_reduced.copy()
df_pooled_reduced["level"].replace({"A": "X", "B": "X", "C": "X", "D": "Y", "E": "Y", "F": "Y", "G": "Y", "H": "Y", "I": "Y", "J": "Z", "K": "Z", "L": "Z", "M": "Z", "N" : "Z"}, inplace=True)
df_pooled_reduced.to_csv('../cs229_sp22_dataset/level_to_page_pooled.tsv', header=None, index=(), sep='\t', mode='w')
df_pooled_reduced.to_csv('../cs229_sp22_dataset/level_to_page_pooled.csv', header=None, index=(), mode='w')
print('Done.')
if __name__ == '__main__':
make_datafiles()