forked from tedunderwood/HathiGenreTrainingset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetadataSorter.py
212 lines (153 loc) · 5.71 KB
/
MetadataSorter.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# Figures out what call numbers mean for genre
import os, sys
import SonicScrewdriver as utils
rowindices, columns, metadata = utils.readtsv("/Users/tunder/Dropbox/pagedata/metascrape/EnrichedMetadata.tsv")
options = ["non", "bio", "poe", "dra", "fic"]
modelindices, modelcolumns, modeldata = utils.readtsv("/Users/tunder/Dropbox/PythonScripts/hathimeta/newgenretable.txt")
def keywithmaxval(dictionary):
maxval = 0
maxkey = ""
for key, value in dictionary.items():
if value > maxval:
maxval = value
maxkey = key
return maxkey
def sequence_to_counts(genresequence):
'''Converts a sequence of page-level predictions to
a dictionary of counts reflecting the number of pages
assigned to each genre. Also reports the largest genre.'''
genrecounts = dict()
genrecounts['fic'] = 0
genrecounts['poe'] = 0
genrecounts['dra'] = 0
genrecounts['non'] = 0
for page in genresequence:
indexas = page
# For this purpose, we treat biography and indexes as equivalent to nonfiction.
if page == "bio" or page == "index" or page == "back" or page == "trv":
indexas = "non"
utils.addtodict(indexas, 1, genrecounts)
# Convert the dictionary of counts into a sorted list, and take the max.
genretuples = utils.sortkeysbyvalue(genrecounts, whethertoreverse = True)
maxgenre = genretuples[0][1]
return genrecounts, maxgenre
def maxoption(fivetuple):
maxcol = 0
maxval = 0.0
nexthighest = 0
for i in range(5):
if float(fivetuple[i]) > maxval:
nexthighest = maxval
maxval = float(fivetuple[i])
maxcol = i
if maxval > 0.94 and nexthighest < 0.4:
return maxcol
else:
return -1
def choose_cascade(htid):
'''Reads metadata about this volume and uses it to decide what metadata-level features should be assigned.'''
global rowindices, columns, metadata, modelindices, modeldata
probablydrama = False
probablypoetry = False
probablybiography = False
probablyfiction = False
maybefiction = False
htid = utils.pairtreelabel(htid)
# convert the clean pairtree filename into a dirty pairtree label for metadata matching
if htid not in rowindices:
# We have no metadata for this volume.
print("Volume missing from ExtractedMetadata.tsv: " + htid)
else:
genrestring = metadata["genres"][htid]
genreinfo = genrestring.split(";")
# It's a semicolon-delimited list of items.
for info in genreinfo:
if info == "Biography" or info == "Autobiography":
probablybiography = True
if info == "Fiction" or info == "Novel":
probablyfiction = True
if (info == "Poetry" or info == "Poems"):
probablypoetry = True
if (info == "Drama" or info == "Tragedies" or info == "Comedies"):
probablydrama = True
if htid in modelindices:
title = metadata["title"][htid].lower()
titlewords = title.split()
maxgenre = maxoption((modeldata["bio"][htid], modeldata["dra"][htid], modeldata["fic"][htid], modeldata["non"][htid], modeldata["poe"][htid]))
if maxgenre == 4 and "poems" in titlewords or "poetical" in titlewords:
probablypoetry = True
if maxgenre == 1:
probablydrama = True
if maxgenre == 2:
maybefiction = True
return probablybiography, probablydrama, probablyfiction, probablypoetry, maybefiction
def letterpart(locnum):
if locnum == "<blank>":
return "<blank>"
letterstring = ""
for char in locnum:
if char.isalpha():
letterstring += char.upper()
else:
break
if len(letterstring) > 2:
letterstring = letterstring[:2]
if len(letterstring) > 1 and letterstring[0] == "N":
letterstring = "N"
if len(letterstring) > 1 and letterstring[0] == "V":
letterstring = "V"
return letterstring
allLCs = dict()
bio = dict()
fic = dict()
poe = dict()
dra = dict()
ctr = 0
for rowidx in rowindices:
loc = metadata["LOCnum"][rowidx]
probablybiography, probablydrama, probablyfiction, probablypoetry, maybefiction = choose_cascade(rowidx)
LC = letterpart(loc)
utils.addtodict(LC, 1, allLCs)
if probablybiography:
utils.addtodict(LC, 1, bio)
if probablydrama:
utils.addtodict(LC, 1, dra)
if probablyfiction:
utils.addtodict(LC, 1, fic)
if probablypoetry:
utils.addtodict(LC, 1, poe)
if maybefiction:
utils.addtodict(LC, 0.1, fic)
ctr += 1
if ctr % 1000 == 1:
print(ctr)
if ctr > 100000:
break
littuples = list()
biotuples = list()
fictuples = list()
poetuples = list()
dratuples = list()
print("Done reading data. Now creating tuples.")
ctr = 0
for key, totalcount in allLCs.items():
totallit = dra.get(key, 0) + fic.get(key, 0) + poe.get(key, 0)
litpercent = (totallit + 0.01) / (totalcount + 0.01)
littuples.append((litpercent, key, totalcount))
biopercent = (bio.get(key, 0) + 0.01) / (totalcount + 0.01)
biotuples.append((biopercent, key, totalcount))
totalfic = fic.get(key, 0)
ficpercent = (totalfic + 0.01) / (totalcount + 0.01)
fictuples.append((ficpercent, key, totalcount))
totalpoe = poe.get(key, 0)
poepercent = (totalpoe + 0.01) / (totalcount + 0.01)
poetuples.append((poepercent, key, totalcount))
totaldra = dra.get(key, 0)
drapercent = (totaldra + 0.01) / (totalcount + 0.01)
dratuples.append((drapercent, key, totalcount))
ctr += 1
if ctr % 1000 == 1:
print(ctr)
print("Done creating tuples. Now sorting tuples.")
littuples.sort()
biotuples.sort()