-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3. print_emotionalarc_metadata_to_csv.py
280 lines (233 loc) · 8.33 KB
/
3. print_emotionalarc_metadata_to_csv.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Create list of gutenberg IDs
# based on filters from metadata
# Must run code at bottom in Python 2.7 Environment
# to run readmetadata() function
import pandas as pd
md = readmetadata()
dataset = pd.read_csv('emotional-arc_data.csv')
gut_ids = dataset.iloc[:, 1].values
## COLLECT THIS DATA
authoryearofbirth = []
authoryearofdeath = []
subjects = []
for book in gut_ids:
authoryearofbirth.append(md[book]['authoryearofbirth'])
authoryearofdeath.append(md[book]['authoryearofdeath'])
subjects.append(md[book]['subjects'])
import csv
## Export Subjects
with open("emotional-arc_data_subjects.csv", "w") as f:
writer = csv.writer(f)
for subject in subjects:
writer.writerow([subject])
## Export Author Year of Birth
with open("emotional-arc_data_authyearofbirth.csv", "w") as f:
writer = csv.writer(f)
for year in authoryearofbirth:
writer.writerow([year])
## Export Author Year of Death
with open("emotional-arc_data_authyearofdeath.csv", "a") as f:
writer = csv.writer(f)
for year in authoryearofdeath:
writer.writerow([year])
##
## RUN THIS CODE TO GET READMETADATA() FUNCTION
##
"""Extract metadata from Project Gutenberg RDF catalog into a Python dict.
Based on https://bitbucket.org/c-w/gutenberg/
>>> md = readmetadata()
>>> md[123]
{'LCC': {'PS'},
'author': u'Burroughs, Edgar Rice',
'authoryearofbirth': 1875,
'authoryearofdeath': 1950,
'downloads': 401,
'formats': {'application/epub+zip': 'http://www.gutenberg.org/ebooks/123.epub.noimages',
'application/prs.plucker': 'http://www.gutenberg.org/ebooks/123.plucker',
'application/x-mobipocket-ebook': 'http://www.gutenberg.org/ebooks/123.kindle.noimages',
'application/x-qioo-ebook': 'http://www.gutenberg.org/ebooks/123.qioo',
'text/html; charset=iso-8859-1': 'http://www.gutenberg.org/files/123/123-h.zip',
'text/plain': 'http://www.gutenberg.org/ebooks/123.txt.utf-8',
'text/plain; charset=us-ascii': 'http://www.gutenberg.org/files/123/123.zip'},
'id': 123,
'language': ['en'],
'subjects': {'Adventure stories',
'Earth (Planet) -- Core -- Fiction',
'Fantasy fiction',
'Science fiction'},
'title': u"At the Earth's Core",
'type': 'Text'}
"""
import os
import re
import gzip
import tarfile
import urllib
import xml.etree.cElementTree as ElementTree
try:
import cPickle as pickle
except ImportError:
import pickle
PICKLEFILE = '/tmp/md.pickle.gz' # The Python dict produced by this module
RDFFILES = '/Users/home/Desktop/all_rdf_data.tar.bz2' # The catalog downloaded from Gutenberg
RDFURL = r'http://www.gutenberg.org/cache/epub/feeds/rdf-files.tar.bz2'
META_FIELDS = ('id', 'author', 'title', 'downloads', 'formats', 'type', 'LCC',
'subjects', 'authoryearofbirth', 'authoryearofdeath', 'language')
NS = dict(
pg='http://www.gutenberg.org/2009/pgterms/',
dc='http://purl.org/dc/terms/',
dcam='http://purl.org/dc/dcam/',
rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#')
LINEBREAKRE = re.compile(r'[ \t]*[\n\r]+[ \t]*')
ETEXTRE = re.compile(r'''
e(text|b?ook)
\s*
(\#\s*(?P<etextid_front>\d+)
|
(?P<etextid_back>\d+)\s*\#)
''', re.IGNORECASE | re.VERBOSE)
def readmetadata():
"""Read/create cached metadata dump of Gutenberg catalog.
Returns:
A dictionary with the following fields:
id (int): Gutenberg identifier of text
author (str): Last name, First name
title (str): title of work
subjects (list of str): list of descriptive subjects; a subject may be
hierarchical, e.g:
'England -- Social life and customs -- 19th century -- Fiction'
LCC (list of str): a list of two letter Library of Congress
Classifications, e.g., 'PS'
language (list of str): list of two letter language codes.
type (str): 'Text', 'Sound', ...
formats (dict of str, str pairs): keys are MIME types, values are URLs.
download count (int): the number of times this ebook has been
downloaded from the Gutenberg site in the last 30 days.
Fields that are not part of the metadata are set to None.
http://www.gutenberg.org/wiki/Gutenberg:Help_on_Bibliographic_Record_Page
"""
if os.path.exists(PICKLEFILE):
metadata = pickle.load(gzip.open(PICKLEFILE, 'rb'))
else:
metadata = {}
for xml in getrdfdata():
ebook = xml.find(r'{%(pg)s}ebook' % NS)
if ebook is None:
continue
result = parsemetadata(ebook)
if result is not None:
metadata[result['id']] = result
pickle.dump(metadata, gzip.open(PICKLEFILE, 'wb'), protocol=-1)
return metadata
#SOME python versions like this
# _, _ = urllib.request.urlopen(RDFURL, RDFFILES)
def getrdfdata():
"""Downloads Project Gutenberg RDF catalog.
Yields:
xml.etree.ElementTree.Element: An etext meta-data definition.
"""
if not os.path.exists(RDFFILES):
_, _ = urllib.urlretrieve(RDFURL, RDFFILES)
with tarfile.open(RDFFILES) as archive:
for tarinfo in archive:
yield ElementTree.parse(archive.extractfile(tarinfo))
def parsemetadata(ebook):
"""Parses an etext meta-data definition to extract fields.
Args:
ebook (xml.etree.ElementTree.Element): An ebook meta-data definition.
"""
result = dict.fromkeys(META_FIELDS)
# get etext no
about = ebook.get('{%(rdf)s}about' % NS)
result['id'] = int(os.path.basename(about))
# author
creator = ebook.find('.//{%(dc)s}creator' % NS)
if creator is not None:
name = creator.find('.//{%(pg)s}name' % NS)
if name is not None:
result['author'] = safeunicode(name.text, encoding='utf-8')
birth = creator.find('.//{%(pg)s}birthdate' % NS)
if birth is not None:
result['authoryearofbirth'] = int(birth.text)
death = creator.find('.//{%(pg)s}deathdate' % NS)
if death is not None:
result['authoryearofdeath'] = int(death.text)
# title
title = ebook.find('.//{%(dc)s}title' % NS)
if title is not None:
result['title'] = fixsubtitles(
safeunicode(title.text, encoding='utf-8'))
# subject lists
result['subjects'], result['LCC'] = set(), set()
for subject in ebook.findall('.//{%(dc)s}subject' % NS):
res = subject.find('.//{%(dcam)s}memberOf' % NS)
if res is None:
continue
res = res.get('{%(rdf)s}resource' % NS)
value = subject.find('.//{%(rdf)s}value' % NS).text
if res == ('%(dc)sLCSH' % NS):
result['subjects'].add(value)
elif res == ('%(dc)sLCC' % NS):
result['LCC'].add(value)
# formats
result['formats'] = {file.find('{%(dc)s}format//{%(rdf)s}value' % NS).text:
file.get('{%(rdf)s}about' % NS)
for file in ebook.findall('.//{%(pg)s}file' % NS)}
# type
booktype = ebook.find('.//{%(dc)s}type//{%(rdf)s}value' % NS)
if booktype is not None:
result['type'] = booktype.text
# languages
lang = ebook.findall('.//{%(dc)s}language//{%(rdf)s}value' % NS)
result['language'] = [a.text for a in lang] or None
# download count
downloads = ebook.find('.//{%(pg)s}downloads' % NS)
if downloads is not None:
result['downloads'] = int(downloads.text)
return result
def etextno(lines):
"""Retrieves the id for an etext.
Args:
lines (iter): The lines of the etext to search.
Returns:
int: The id of the etext.
Raises:
ValueError: If no etext id was found.
Examples:
>>> etextno(['Release Date: March 17, 2004 [EBook #11609]'])
11609
>>> etextno(['Release Date: July, 2003 [Etext# 4263]'])
4263
>>> etextno(['Release Date: November 29, 2003 [Eook #10335]'])
10335
>>> etextno(['December, 1998 [Etext 1576#]'])
1576
>>> etextno(['Some lines', 'without', 'Any [Etext] Number'])
Traceback (most recent call last):
...
ValueError: no etext-id found
"""
for line in lines:
match = ETEXTRE.search(line)
if match is not None:
front_match = match.group('etextid_front')
back_match = match.group('etextid_back')
if front_match is not None:
return int(front_match)
elif back_match is not None:
return int(back_match)
else:
raise ValueError('no regex match (this should never happen')
raise ValueError('no etext-id found')
def fixsubtitles(title):
"""Introduce any subtitle with (semi)colons instead of newlines.
The first subtitle is introduced with a colon, the rest with semicolons.
>>> fixsubtitles(u'First Across ...\r\nThe Story of ... \r\n'
... 'Being an investigation into ...')
u'First Across ...: The Story of ...; Being an investigation into ...'"""
tmp = LINEBREAKRE.sub(': ', title, 1)
return LINEBREAKRE.sub('; ', tmp)
def safeunicode(arg, *args, **kwargs):
"""Coerce argument to unicode, if it's not already."""
return arg if isinstance(arg, unicode) else unicode(arg, *args, **kwargs)
__all__ = ['readmetadata']