-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathword-parser.py
161 lines (136 loc) · 7.08 KB
/
word-parser.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
import ijson
import time
import codecs
from datetime import datetime
from time import strftime, localtime
f = codecs.open("/media/storage/dpla-data/dplanew", 'r', encoding='utf-8')
# Main output file
#This bit of data structure won't work in standard IO.
#To do this right, I'll need a database (or mongo)
#out = codecs.open('/media/storage/dpla-data/new/words/words.json', 'w', encoding='utf-8')
fields = ["identifier","collection","contributor","creator","date","description",
"extent","format","identifier","isPartOf","language","publisher",
"relation","rights","spatial","specType","stateLocatedIn","subject",
"temporal","title","type","provider","subprov","thumbnail"]
colls = { "ARTstor": "artstor",
"Biodiversity Heritage Library": "biodiv",
"David Rumsey": "rumsey",
"Digital Commonwealth": "commonwealth",
"Digital Library of Georgia": "georgia",
"Harvard Library": "harvard",
"HathiTrust": "hathi",
"Internet Archive": "ia",
"J. Paul Getty Trust": "getty",
"Kentucky Digital Library": "kentucky",
"Minnesota Digital Library": "minnesota",
"Missouri Hub": "missouri",
"Mountain West Digital Library": "mwdl",
"National Archives and Records Administration": "nara",
"North Carolina Digital Heritage Center": "nocar",
"Smithsonian Institution": "smiths",
"South Carolina Digital Library": "socar",
"The New York Public Library": "nypl",
"The Portal to Texas History": "texas",
"United States Government Printing Office (GPO)": "gpo",
"University of Illinois at Urbana-Champaign": "illinois",
"University of Southern California. Libraries": "usc",
"University of Virginia Library": "virginia",
"nocoll": "nocoll"
}
fieldfiles = {}
collfiles = {}
#fieldfiles[s] = [codecs.open('/media/storage/dpla-data/new/words/fields/%s.txt' %s, 'w', encoding='utf-8') for s in fields]
#collfiles[s] = [codecs.open('/media/storage/dpla-data/new/words/colls/%s.txt' %s, 'w', encoding='utf-8') for s in colls.itervalues()]
# for field in fieldfiles: print field
for s in fields:
fieldfiles[s] = codecs.open('/media/storage/dpla-data/words/fields/%s.txt' %s, 'w', encoding='utf-8')
for s in colls.itervalues():
collfiles[s] = codecs.open('/media/storage/dpla-data/words/colls/%s.txt' %s, 'w', encoding='utf-8')
# for k,v in fieldfiles.iteritems():
# print k
# print v
# collfiles = [codecs.open('/media/storage/dpla-data/new/words/colls/%s.txt' %s, 'w', encoding='utf-8') for s in colls.itervalues()]
nocoll = codecs.open('/media/storage/dpla-data/new/words/colls/none.txt', 'w', encoding='utf-8')
#write
now = ""
start = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
print "Start: " + start
counter = 0
for item in ijson.items(f, "item"):
counter = counter + 1
if counter % 10000 == 0:
now = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
print str(counter) + ": " + now
if "provider" in item:
prov = item['provider']['name']
cname = colls[prov]
#print cname
else:
prov = "Null"
cname = "nocoll"
if "dataProvider" in item:
if isinstance(item['dataProvider'], basestring): subprov = item['dataProvider']
else: subprov = ", ".join(item['dataProvider'])
else: subprov = "Null"
string = ""
# collection, contributor, creator, date, description, extent, format, @id, identifier,
# isPartOf, language, publisher, relation, rights, spatial, specType, stateLocatedIn,
# subject, temporal, title, type
for field in ["collection", "contributor", "creator", "date", "description",
"extent", "format", "identifier", "isPartOf", "language", "publisher",
"relation", "rights", "spatial", "specType", "stateLocatedIn",
"subject", "temporal", "title", "type"]:
if field in item['sourceResource'] and item['sourceResource'][field] is not None:
if isinstance(item['sourceResource'][field], list):
for entry in item['sourceResource'][field]:
if isinstance(entry, dict):
# print field
# print entry
if 'title' in entry.keys():
string += unicode(entry['title']) + "\n"
fieldfiles[field].write(unicode(entry['title']) + "\n")
elif 'displayDate' in entry.keys():
string += unicode(entry['displayDate']) + "\n"
fieldfiles[field].write(unicode(entry['displayDate']) + "\n")
elif 'name' in entry.keys():
string += unicode(entry['name']) + "\n"
fieldfiles[field].write(unicode(entry['name']) + "\n")
else:
string += unicode(entry) + "\n"
fieldfiles[field].write(unicode(entry) + "\n")
elif entry is not None:
string += unicode(entry) + "\n"
fieldfiles[field].write(unicode(entry) + "\n")
elif isinstance(item['sourceResource'][field], dict):
#print item['sourceResource'][field]
if 'title' in item['sourceResource'][field].keys():
string += unicode(item['sourceResource'][field]['title']) + "\n"
fieldfiles[field].write(unicode(item['sourceResource'][field]['title']) + "\n")
elif 'displayDate' in item['sourceResource'][field].keys():
string += unicode(item['sourceResource'][field]['displayDate']) + "\n"
fieldfiles[field].write(unicode(item['sourceResource'][field]['displayDate']) + "\n")
elif 'name' in item['sourceResource'][field].keys():
string += unicode(item['sourceResource'][field]['name']) + "\n"
fieldfiles[field].write(unicode(item['sourceResource'][field]['name']) + "\n")
else:
string += unicode(item['sourceResource'][field]) + "\n"
fieldfiles[field].write(unicode(item['sourceResource'][field]) + "\n")
#fieldfiles[f].write(item['sourceResource'][f]['title'])
else:
#print item['sourceResource'][field]
string += unicode(item['sourceResource'][field]) + "\n"
fieldfiles[field].write(unicode(item['sourceResource'][field]) + "\n")
string += " " + prov + " " + subprov + "\n"
#print string
#collfiles[cname].write(string + "\n")
collfiles[cname].write(string + "\n")
#objects = ijson.items(f, 'sourceResource')
#for o in objects:
# print 'title'
end = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
print "Start: " + start
print "End: " + end
# for f in fieldfiles:
# close()
# for f in collfiles:
# close()