-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncshake.py
351 lines (321 loc) · 13.1 KB
/
syncshake.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# coding: utf-8
import os
import re
import urllib.request
import xml.etree.ElementTree
import xml.dom.minidom
import time
import sqlite3
from typing import Dict, Text, List, Match, Any
MOBY_INDEX = [
("All's Well That Ends Well",
'http://www.ibiblio.org/xml/examples/shakespeare/all_well.xml'),
("As You Like It",
'http://www.ibiblio.org/xml/examples/shakespeare/as_you.xml'),
("Antony and Cleopatra",
'http://www.ibiblio.org/xml/examples/shakespeare/a_and_c.xml'),
("A Comedy of Errors",
'http://www.ibiblio.org/xml/examples/shakespeare/com_err.xml'),
("Coriolanus",
'http://www.ibiblio.org/xml/examples/shakespeare/coriolan.xml'),
("Cymbeline",
'http://www.ibiblio.org/xml/examples/shakespeare/cymbelin.xml'),
("A Midsummer Night's Dream",
'http://www.ibiblio.org/xml/examples/shakespeare/dream.xml'),
("Hamlet",
'http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml'),
("Henry IV, Part I",
'http://www.ibiblio.org/xml/examples/shakespeare/hen_iv_1.xml'),
("Henry IV, Part II",
'http://www.ibiblio.org/xml/examples/shakespeare/hen_iv_2.xml'),
("Henry V",
'http://www.ibiblio.org/xml/examples/shakespeare/hen_v.xml'),
("Henry VIII",
'http://www.ibiblio.org/xml/examples/shakespeare/hen_viii.xml'),
("Henry VI, Part 1",
'http://www.ibiblio.org/xml/examples/shakespeare/hen_vi_1.xml'),
("Henry VI, Part 2",
'http://www.ibiblio.org/xml/examples/shakespeare/hen_vi_2.xml'),
("Henry VI, Part 3",
'http://www.ibiblio.org/xml/examples/shakespeare/hen_vi_3.xml'),
("The Life and Death of King John",
'http://www.ibiblio.org/xml/examples/shakespeare/john.xml'),
("Julius Caesar",
'http://www.ibiblio.org/xml/examples/shakespeare/j_caesar.xml'),
("King Lear",
'http://www.ibiblio.org/xml/examples/shakespeare/lear.xml'),
("Love's Labor's Lost",
'http://www.ibiblio.org/xml/examples/shakespeare/lll.xml'),
("Macbeth",
'http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml'),
("The Merchant of Venice",
'http://www.ibiblio.org/xml/examples/shakespeare/merchant.xml'),
("Much Ado About Nothing",
'http://www.ibiblio.org/xml/examples/shakespeare/much_ado.xml'),
("Measure for Measure",
'http://www.ibiblio.org/xml/examples/shakespeare/m_for_m.xml'),
("The Merry Wives of Windsor",
'http://www.ibiblio.org/xml/examples/shakespeare/m_wives.xml'),
("Othello",
'http://www.ibiblio.org/xml/examples/shakespeare/othello.xml'),
("Pericles",
'http://www.ibiblio.org/xml/examples/shakespeare/pericles.xml'),
("Richard II",
'http://www.ibiblio.org/xml/examples/shakespeare/rich_ii.xml'),
("Richard III",
'http://www.ibiblio.org/xml/examples/shakespeare/rich_iii.xml'),
("Romeo and Juliet",
'http://www.ibiblio.org/xml/examples/shakespeare/r_and_j.xml'),
("The Taming of the Shrew",
'http://www.ibiblio.org/xml/examples/shakespeare/taming.xml'),
("The Tempest",
'http://www.ibiblio.org/xml/examples/shakespeare/tempest.xml'),
("Timon of Athens",
'http://www.ibiblio.org/xml/examples/shakespeare/timon.xml'),
("Titus Andronicus",
'http://www.ibiblio.org/xml/examples/shakespeare/titus.xml'),
("Troilus and Cressida",
'http://www.ibiblio.org/xml/examples/shakespeare/troilus.xml'),
("Two Gentlemen of Verona",
'http://www.ibiblio.org/xml/examples/shakespeare/two_gent.xml'),
("Twelfth Night",
'http://www.ibiblio.org/xml/examples/shakespeare/t_night.xml'),
("A Winter's Tale",
'http://www.ibiblio.org/xml/examples/shakespeare/win_tale.xml'),
]
DBFILE = "./syncshakedb.sqlite"
def create_tables(conn):
# Now create the tables
tables: List = ['''CREATE TABLE IF NOT EXISTS plays
(id INTEGER PRIMARY KEY,
title TEXT,
subtitle TEXT,
scene TEXT
);''',
'''CREATE TABLE IF NOT EXISTS acts
(id INTEGER PRIMARY KEY,
play_id INTEGER,
act_num INTEGER,
title TEXT
);''',
'''CREATE TABLE IF NOT EXISTS scenes
(id INTEGER PRIMARY KEY,
play_id INTEGER,
act_id INTEGER,
scene_num INTEGER,
title TEXT
);''',
'''CREATE TABLE IF NOT EXISTS actor_lines
(id INTEGER PRIMARY KEY,
play_id INTEGER,
act_id INTEGER,
scene_id INTEGER,
is_stagedir INTEGER,
the_text TEXT,
speaker TEXT
);''',
'''CREATE TABLE IF NOT EXISTS person
(id INTEGER PRIMARY KEY,
play_id INTEGER,
name TEXT,
description TEXT,
group_name TEXT
);'''
]
[conn.execute(sql) for sql in tables]
conn.commit()
def _get_text(nodelist: List):
rc: List = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc)
def handle_title(title):
return _get_text(title.childNodes)
def handle_personae_group(group):
people = []
for element in group.childNodes:
if isinstance(element, xml.dom.minidom.Text):
continue
if element.tagName == 'GRPDESCR':
group_name = _get_text(element.childNodes)
else:
people.append(_get_text(element.childNodes))
people_with_group = [(group_name, person) for person in people]
return (group_name, people_with_group)
def handle_personae(personae):
result = {}
result['persona'] = []
result['persona_group'] = []
for element in personae.childNodes:
if isinstance(element, xml.dom.minidom.Text):
continue
if element.tagName == 'TITLE':
continue
if element.tagName == "PERSONA":
result['persona'].append(('',_get_text(element.childNodes)))
else:
group = handle_personae_group(element)
result['persona_group'].append(group[0])
result['persona'] += group[1]
return result
def handle_stage_dir(stagedir):
return _get_text(stagedir.childNodes)
def handle_speech(speech):
result = {}
result['lines'] = []
for line in speech.childNodes:
if isinstance(line, xml.dom.minidom.Text):
continue
if line.tagName == "SPEAKER":
result['speaker'] = _get_text(line.childNodes)
else:
result['lines'].append(_get_text(line.childNodes))
return result
def handle_scene(scene):
result = {}
result['title'] = handle_title(scene.getElementsByTagName('TITLE')[0])
result['content'] = []
for element in scene.childNodes:
if isinstance(element,xml.dom.minidom.Text) or element.tagName == "TITLE":
continue
if element.tagName == "STAGEDIR":
result['content'].append((0,handle_stage_dir(element)))
else:
result['content'].append((1,handle_speech(element)))
return result
def handle_act(act):
result: Dict[Text, Text] = {}
result['title'] = handle_title(act.getElementsByTagName('TITLE')[0])
scenes = act.getElementsByTagName('SCENE')
result['scenes'] = []
for scene in scenes:
result['scenes'].append(handle_scene(scene))
return result
class Play():
def __init__(self, title: str, url: str):
self.title: str = title
self.url: str = url
def store(self, play: Dict[str, Dict], conn):
print(play['title'])
cursor = conn.execute('''
INSERT INTO plays
(title, subtitle, scene)
VALUES (?,?,?)''',
(play['title'], play['subtitle'], play['scene_description']))
play_id: int = cursor.lastrowid
# create the groups
for person in play['personae']['persona']:
# split the name
matches: List[Text] = re.split(r',', person[1])
match: Match[Any] = re.match(r'[A-z ]*', matches[0])
name = match.group(0).strip(' .')
if len(matches) == 1:
persona = (name,'')
elif len(matches) == 2:
persona = (name, re.match(r'[A-z ]*',matches[1]).group(0).strip(' .'))
else:
persona = (person[1],'')
conn.execute('''
INSERT INTO person
(play_id, name, description,group_name)
VALUES (?,?,?,?)''',
(play_id, persona[0], persona[1], person[0]))
act_num = 1
for act in play['acts']:
print(play['title'], act['title'])
cursor = conn.execute('''
INSERT INTO acts
(title, play_id, act_num)
VALUES (?,?,?)''',
(act['title'],play_id,act_num))
act_id = cursor.lastrowid
scene_num = 1
for scene in act['scenes']:
cursor = conn.execute('''
INSERT INTO scenes
(play_id, act_id, title, scene_num)
VALUES (?,?,?,?)''',
(play_id, act_id, scene['title'], scene_num))
scene_id = cursor.lastrowid
# and finally write the lines
for content in scene['content']:
if content[0] == 0:
# This is stage direction
conn.execute('''
INSERT INTO actor_lines
(play_id, act_id, scene_id,
is_stagedir, the_text, speaker)
VALUES (?,?,?,?,?,?)''',
(play_id, act_id, scene_id,
1, content[1], 'None'))
else:
try:
speaker = content[1]['speaker']
except KeyError:
print(content)
for line in content[1]['lines']:
cursor = conn.execute('''
INSERT INTO actor_lines
(play_id, act_id, scene_id,
is_stagedir, the_text, speaker)
VALUES (?,?,?,?,?,?)''',
(play_id, act_id, scene_id,
0, line, speaker))
# Increment scene_num
scene_num = scene_num + 1
# Increment act num
act_num = act_num + 1
# Commit this play.
conn.commit()
def fetch_and_store(self, conn):
with urllib.request.urlopen(self.url) as response:
play_text = response.read()
play = xml.dom.minidom.parseString(play_text)
result: Dict = {}
result['title'] = handle_title(play.getElementsByTagName('TITLE')[0])
result['subtitle'] = handle_title(play.getElementsByTagName('PLAYSUBT')[0])
result['scene_description'] = handle_title(play.getElementsByTagName('SCNDESCR')[0])
result['personae'] = handle_personae(play.getElementsByTagName('PERSONAE')[0])
result['acts'] = []
acts = play.getElementsByTagName('ACT')
for act in acts:
result['acts'].append(handle_act(act))
self.store(result, conn)
def main():
print('entering main')
#os.remove(DBFILE)
with sqlite3.connect(DBFILE) as db:
print('Creating tables')
create_tables(db)
plays = [Play(play[0], play[1]) for play in MOBY_INDEX]
for play in plays:
play.fetch_and_store(db)
def get_stuff():
conn = sqlite3.connect(DBFILE)
conn.row_factory = sqlite3.Row
cursor = conn.execute('''SELECT
actor_lines.the_text, actor_lines.speaker, plays.title
FROM
actor_lines, plays
WHERE
plays.id = actor_lines.play_id AND
actor_lines.the_text LIKE ?
''', ('%piss%',))
x = 0
for row in cursor:
x = x + 1
print(tuple(row))
print(x)
def get_tables():
conn = sqlite3.connect(DBFILE)
conn.row_factory = sqlite3.Row
cursor = conn.execute('''SELECT * from sqlite_master WHERE type='table' and name='plays' ''')
for row in cursor:
print(tuple(row))
if __name__ == "__main__":
t = time.time()
main()
get_stuff()
#get_tables()
print(time.time() - t)