-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
209 lines (189 loc) · 8.28 KB
/
stats.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
207
208
209
import json
import fileinput
import sys
import codecs
"""Creates a JSON file with statistics.
Usage: sed -nre "s/^[^{]*//p" <ol_dump_file> | python stats.py output.json
This script reads the standard input, one JSON record from Open Library per line,
and basically counts keys, classifications and identifiers. Keys are counted for
separate record types (author, edition, work, etc.); classifications and identifiers are
counted when available.
"""
if sys.argv[len(sys.argv)-1] != sys.argv[0]:
outputfile = codecs.open(sys.argv[len(sys.argv)-1], 'wb','utf-8')
else:
sys.exit("No filename supplied!")
# Create base stats dict. countr is the record count
stats = {
"confused": [],
"error": []
}
# special keys: classifications and identifiers that are lists (hence no ocaid),
# outside the classifications and identifiers objects.
special_class = ["lc_classifications", "dewey_decimal_class"]
special_id = ["isbn_10", "isbn_13", "lccn", "oclc_numbers"]
def classifications_stats(record, type):
"""Counts classifications in the record.
"""
"""for c in special_class:
if c in record.keys():
stats[type]["sc"][c][0] = stats[type]["sc"][c][0] + 1
stats[type]["sc"][c][1] = stats[type]["sc"][c][1] + len(record[c])
if len(record[c]) == 0:
stats[type]["sc"][c][2] = stats[type]["sc"][c][2] + 1
"""
for k in record["classifications"].keys():
if k in stats[type]["classifications"].keys():
stats[type]["classifications"][k][0] = stats[type]["classifications"][k][0] + 1
if isinstance(record["classifications"][k], list):
stats[type]["classifications"][k][1] = stats[type]["classifications"][k][1] + len(record["classifications"][k])
if len(record["classifications"][k]) == 0: # empty list
stats[type]["classifications"][k][2] = stats[type]["classifications"][k][2] + 1
else:
stats[type]["classifications"][k][1] = stats[type]["classifications"][k][1] + 1
else:
stats[type]["classifications"][k] = [1,1,0]
if isinstance(record["classifications"][k], list):
stats[type]["classifications"][k][1] = len(record["classifications"][k])
if len(record["classifications"][k]) == 0: # empty list
stats[type]["classifications"][k][2] = stats[type]["classifications"][k][2] + 1
def identifiers_stats(record, type):
"""Counts identifiers in the record.
"""
# 'top-level' identifiers
"""for i in special_id:
if i in record.keys():
stats[type]["si"][i][0] = stats[type]["si"][i][0] + 1
stats[type]["si"][i][1] = stats[type]["si"][i][1] + len(record[i])
if len(record[i]) == 0:
stats[type]["si"][i][2] = stats[type]["si"][i][2] + 1
"""
# 'normal' identifiers
for k in record["identifiers"].keys():
if k in stats[type]["identifiers"].keys():
stats[type]["identifiers"][k][0] = stats[type]["identifiers"][k][0] + 1
if isinstance(record["identifiers"][k], list):
stats[type]["identifiers"][k][1] = stats[type]["identifiers"][k][1] + len(record["identifiers"][k])
if len(record["identifiers"][k]) == 0: # empty list
stats[type]["identifiers"][k][2] = stats[type]["identifiers"][k][2] + 1
else:
stats[type]["identifiers"][k][1] = stats[type]["identifiers"][k][1] + 1
else:
stats[type]["identifiers"][k] = [1,1,0]
if isinstance(record["identifiers"][k], list):
stats[type]["identifiers"][k][1] = len(record["identifiers"][k])
if len(record["identifiers"][k]) == 0: # empty list
stats[type]["identifiers"][k][2] = stats[type]["identifiers"][k][2] + 1
def key_stats(record, type):
"""Counts keys in the record.
If there are identifiers and / or classifications, they are counted separately.
"""
for k in record.keys():
if k in stats[type]["keys"].keys():
stats[type]["keys"][k][0] = stats[type]["keys"][k][0] + 1
if isinstance(record[k], list):
stats[type]["keys"][k][1] = stats[type]["keys"][k][1] + len(record[k])
if len(record[k]) == 0: # empty list
stats[type]["keys"][k][2] = stats[type]["keys"][k][2] + 1
else:
stats[type]["keys"][k][1] = stats[type]["keys"][k][1] + 1
else:
stats[type]["keys"][k] = [1,1,0]
if isinstance(record[k], list):
stats[type]["keys"][k][1] = len(record[k])
if len(record[k]) == 0: # empty list
stats[type]["keys"][k][2] = stats[type]["keys"][k][2] + 1
if "identifiers" in record.keys():
identifiers_stats(record, type)
if "classifications" in record.keys():
classifications_stats(record, type)
def determine_type(object):
if object["key"][0:8] == "/authors":
if object["type"]["key"] == "/type/author":
return "author"
elif object["type"]["key"] == "/type/redirect":
return "redirect_author"
elif object["type"]["key"] == "/type/delete":
return "deleted_author"
else:
stats["confused"].append((object["type"]["key"],object["key"]))
return "confused_author"
elif object["key"][0:6] == "/books":
if object["type"]["key"] == "/type/edition":
return "edition"
elif object["type"]["key"] == "/type/redirect":
return "redirect_edition"
elif object["type"]["key"] == "/type/delete":
return "deleted_edition"
elif object["type"]["key"] == "/type/volume":
return "volume"
else:
stats["confused"].append((object["type"]["key"],object["key"]))
return "confused_edition"
elif object["key"][0:6] == "/works":
if object["type"]["key"] == "/type/work":
return "work"
elif object["type"]["key"] == "/type/redirect":
return "redirect_work"
elif object["type"]["key"] == "/type/delete":
return "deleted_work"
else:
stats["confused"].append((object["type"]["key"],object["key"]))
return "confused_work"
elif object["type"]["key"] == "/type/redirect":
return "redirect"
elif object["type"]["key"] == "/type/delete":
return "delete"
elif object["type"]["key"] == "/type/subject":
return "subject"
elif object["type"]["key"] == "/type/page":
return "page"
elif object["type"]["key"] == "/type/template":
return "template"
elif object["type"]["key"] == "/type/i18n":
return "i18n"
elif object["type"]["key"] == "/type/language":
return "language"
elif object["type"]["key"] == "/type/type":
return "type"
elif object["type"]["key"] == "/type/property":
return "property"
elif object["type"]["key"] == "/type/library":
return "library"
elif object["type"]["key"] == "/type/macro":
return "macro"
elif object["type"]["key"] == "/type/rawtext":
return "rawtext"
elif object["type"]["key"] == "/type/home":
return "home"
elif object["type"]["key"] == "/type/usergroup":
return "usergroup"
elif object["type"]["key"] == "/type/i18n_page":
return "i18n_page"
else:
stats["confused"].append((object["type"]["key"],object["key"]))
return "confused"
# Open standard input
f = fileinput.input('-')
for line in f:
record = json.loads(line)
# determine record type
t = determine_type(record)
if t == "confused":
continue
# do stuff
if t in stats.keys():
stats[t]["countr"] = stats[t]["countr"] + 1
else:
stats[t] = {"countr": 1, "keys": {}, "classifications": {}, "sc": {}, "identifiers": {}, "si": {}}
#for c in special_class:
# stats[t]["sc"][c] = [0,0,0]
#for i in special_id:
# stats[t]["si"][i] = [0,0,0]
print "new type:", t
try:
key_stats(record, t)
except Exception as e:
print record["key"], sys.exc_info()
stats["error"].append((record, str(e)))
json.dump(stats, outputfile, indent=2)