-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShakespeareRehearser.py
138 lines (114 loc) · 3.33 KB
/
ShakespeareRehearser.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
# Set URL here:
home = "http://shakespeare.mit.edu/"
# Get the HTML.
import urllib
page = urllib.urlopen(home).read()
# Get plays stored as [(PLAY NAME,www.page.com)]
html = page
urlj = 0
plays = []
while True:
urli = html.find('a href=\"',urlj)
urlj = html.find('\">',urli)
anchori = urlj
anchorj = html.find('<',anchori)
if urli == -1:
break
url = home+html[urli+8:urlj].replace('index','full')
anchor = html[anchori+2:anchorj].replace('\n','')
# switch them if wrong
plays.append((anchor,url))
# list the plays
i = 1
while i < len(plays):
print str(i)+'. '+plays[i][0]
i = i + 1
# ask for user to enter an int to choose a play.
selection = int(raw_input('Enter a number to select a play:'))
# Clear screen
import os
os.system('cls' if os.name=='nt' else 'clear')
# Announce user character choice.
print 'You chose '+plays[selection][0]+'.'
# Get the HTML.
play = plays[selection][1]
page = urllib.urlopen(play).read()
from bs4 import BeautifulSoup
soup = BeautifulSoup(page)
# Get characters stored as <b>CHARACTER NAME</b>
characters = []
unsortedCharacters = soup.findAll('b')
# Strip tags to leave strings
for character in unsortedCharacters:
characters.append(character.string)
# Remove duplicates.
characters = list(set(characters))
# Display ordered list of characters.
i = 1
for character in characters:
print str(i)+'. '+character
i = i + 1
# ask for user to enter an int to choose a character.
selection = raw_input('Enter a number to select a character:')
# turn selection into a 0-based index int
selection = int(selection) - 1
# Clear screen
os.system('cls' if os.name=='nt' else 'clear')
# Announce user character choice.
print 'You chose '+characters[selection]+'. Press enter to continue:'
# Get character's lines.
html = page
i=0
j=0
act = ''
scene = ''
from AppKit import NSSpeechSynthesizer
import time
import sys
# if len(sys.argv) < 2:
text = raw_input('> ')
# else:
# text = sys.argv[1]
nssp = NSSpeechSynthesizer
ve = nssp.alloc().init()
voices = ["com.apple.speech.synthesis.voice.Alex","com.apple.speech.synthesis.voice.Vicki","com.apple.speech.synthesis.voice.Victoria","com.apple.speech.synthesis.voice.Zarvox" ]
# for voice in nssp.availableVoices():
ve.setVoice_(voices[0])
def speak(txt):
ve.startSpeakingString_(txt)
while ve.isSpeaking():
time.sleep(1)
return txt
# from os import system
# def speak(txt):
# system('say'+txt)
# return txt
while True:
oldi = i
# get indices around your speech
i = html.find('<b>'+characters[selection]+'</b>',j)
j = html.find('</blockquote>',i)
# get indices around previous speech
previ = html.rfind('<b>', oldi, i)
prevj = html.find('</blockquote>', previ, i)
# end the loop before printing if reached EOF
if i == -1 or j == -1:
break
# prev speaker
prevSpeech = ''
for string in BeautifulSoup(html[previ:prevj].replace('<blockquote>',':')).stripped_strings:
line = str(repr(string))
# join lines and remove u for unicode
prevSpeech = prevSpeech + line[1:len(line)]
# remove line breaks and quotes
prevSpeech = prevSpeech.replace('\'\'',' ')
# remove 'exeunt'
prevSpeech = prevSpeech.replace('Exeunt','')
# remove double quotation marks
prevSpeech = prevSpeech.replace('"','')
print prevSpeech
speak(prevSpeech)
# your character
for string in BeautifulSoup(html[i:j]).stripped_strings:
print repr(string)[1:]
raw_input('Press enter to continue...')