-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate
executable file
·273 lines (241 loc) · 8.71 KB
/
translate
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
#!/usr/bin/env python
import nltk.corpus
import re
import sys
def main():
write(translate(read()))
def read():
return [line.strip() for line in sys.stdin]
def translate(lines):
return compose([remove_empty,
filter_text,
scene_stage_directions,
fold_stage_directions,
convert,
shared_verse_lines,
add_wrapper])(lines)
def remove_empty(lines):
return filter(None, lines)
def filter_text(lines):
start = lines.index('ACT 1. SCENE 1.1.')
end = lines.index('*****')
return lines[start:end]
def scene_stage_directions(lines):
result = []
at_scene_start = False
stage_dir = []
for line in lines:
if at_scene_start:
if re.match(r"^[A-Z '.,]+$", line):
stage_dir.append(line)
else:
result.append(r'\stagedir{%s}' % ' '.join(stage_dir))
result.append(line)
at_scene_start = False
stage_dir = []
else:
if re.match(r'^.*SCENE [0-9]+\.[0-9]\.$', line):
at_scene_start = True
result.append(line)
return result
def fold_stage_directions(lines):
result = []
in_stage_dir = False
stage_dir = None
for line in lines:
if in_stage_dir:
stage_dir.append(line)
if ']' in line:
result.append(' '.join(stage_dir))
in_stage_dir = False
stage_dir = None
else:
if '[' in line and not ']' in line:
in_stage_dir = True
stage_dir = [line]
else:
result.append(line)
return result
def shared_verse_lines(lines):
dictionary = nltk.corpus.cmudict.dict()
def parse_speech(line):
m = re.match(r"^(\\[a-z]+ )?([A-Za-z.:;,\"' ?!()-]+)\\\\$", line)
if not m:
return None
return {'prefix': m.group(1), 'words': m.group(2)}
def is_speech(line):
return not not parse_speech(line)
def speech_words(speech):
return parse_speech(speech)['words']
def speech_prefix(speech):
return parse_speech(speech)['prefix'] or ''
def has_prefix(speech):
return not not parse_speech(speech)['prefix']
def is_complete(verse_line):
def syllables_from_dictionary(word):
candidates = dictionary[word]
return len([char for char in candidates[0] if char[-1].isdigit()])
def without_terminal_s(word):
return re.sub(r's$', '', word)
def word_syllables(word):
word = word.lower()
word = re.sub(r'[,.!:?;"()]', '', word)
word = re.sub(r"'[dst]$", '', word)
word = re.sub(r"'(ll|st|n|er|en|lt)$", '', word)
word = re.sub(r"t'", '', word)
word = re.sub(r"'", '', word)
if not word:
return 0
elif word in dictionary:
return syllables_from_dictionary(word)
elif without_terminal_s(word) in dictionary:
return syllables_from_dictionary(without_terminal_s(word))
else:
return len(re.sub(r'[^aeiou]', '', word))
def speech_syllables(speech):
speech = re.sub(r'-', ' ', speech)
return sum(map(word_syllables, speech_words(speech).split()))
return sum(map(speech_syllables, filter(is_speech, verse_line))) > 9
def is_same_speaker(line):
return is_speech(line) and not has_prefix(line)
def is_just_non_speech(verse_line):
return not any(map(is_speech, verse_line))
def is_at_end(lines, i):
return i == len(lines)-1
def phantom(lines):
speech_lines = filter(is_speech, lines)
if not speech_lines:
return ''
return r'\vinphantom{%s} ' % ' '.join(map(speech_words, speech_lines))
def should_output_verse_line(lines, i, verse_line):
return is_complete(verse_line) or \
is_at_end(lines, i) or \
is_same_speaker(lines[i+1]) or \
is_just_non_speech(verse_line)
def output_verse_line(result, verse_line):
for j in range(0, len(verse_line)):
current = verse_line[j]
if is_speech(current):
previous = verse_line[0:j]
result.append(r'%s%s%s\\' % (speech_prefix(current), phantom(previous), speech_words(current)))
else:
result.append(current)
result = []
verse_line = []
for i in range(len(lines)):
line = lines[i]
verse_line.append(line)
if should_output_verse_line(lines, i, verse_line):
output_verse_line(result, verse_line)
verse_line = []
return result
def convert(lines):
return map(compose([normalize_prefix_direction_combos,
fix_broken_prefix]+
fix_insertions()+
speech_prefixes()+
[stage_direction,
verse,
acts,
scenes,
mdashes]),
lines)
def normalize_prefix_direction_combos(line):
return re.sub(r'^([A-Z]+) \[(.+)\]\.(.*)$', r'\1. [\2]\3', line)
def fix_broken_prefix(line):
return re.sub(r'SUR\.--', r'SUR. --', line)
def fix_insertions():
def make_fix(word):
return lambda line: re.sub(r'^(.*)\[(%s)\](.*)$'%word, r'\1\2\3', line)
return map(make_fix, ['my', 'a'])
def speech_prefixes():
def speech_prefix(opts):
orig, new = opts
return lambda line: re.sub(r'^%s. (.+)$'%orig, r'\\%sspeaks \1'%new, line)
return map(speech_prefix, prefix_defs())
def prefix_defs():
return [('DOL', 'dol'),
('FACE', 'face'),
('SUB', 'subtle'),
('LOVE', 'lovewit'),
('KAS', 'kastril'),
('DRUG', 'drugger'),
('NAB', 'drugger'),
('ANA', 'ananias'),
('TRI', 'tribulation'),
('SUR', 'surly'),
('MAM', 'mammon'),
('DAP', 'dapper'),
('DAME P', 'pliant'),
('OFFI', 'officerone'),
('1 OFFI', 'officerone'),
('2 OFFI', 'officertwo'),
('1 NEI', 'neighone'),
('2 NEI', 'neightwo'),
('3 NEI', 'neighthree'),
('4 NEI', 'neighfour'),
('5 NEI', 'neighfive'),
('6 NEI', 'neighsix')]
def stage_direction(line):
return re.sub(r'^(.*)\[(.*)\](.*)$', r'\1\\direct*{\2}\3', line)
def verse(line):
if re.match(r'^(\\[a-z]+ )?\\direct\*\{[^{]+\}$', line):
# This line only has a stage direction on it (maybe preceeded
# by a speech prefix).
return line
if re.match(r'^\\[A-Za-z]+\{.*\}$', line):
return line
return r'%s\\' % line
def acts(line):
return re.sub(r'^ACT [0-9]+\. (.+)$', r'\\act\n\n\1', line)
def scenes(line):
return re.sub(r'SCENE.*', r'\\scene\n', line)
def mdashes(line):
return re.sub('--', '---', line)
def compose(funs):
return lambda x: reduce(lambda acc, f: f(acc), funs, x)
def add_wrapper(lines):
return prefix()+lines+suffix()
def write(lines):
print('\n'.join(lines))
def prefix():
return r'''\documentclass[a4paper,oneside]{memoir}
\usepackage{dramatist}
\usepackage{dramatist-mods}
\sloppybottom
\raggedbottom
\raggedright
\setlrmarginsandblock{90pt}{90pt}{*}
\begin{document}
\Character[Subtle, the Alchemist]{Subtle}{subtle}
\Character[Face, the housekeeper]{Face}{face}
\Character[Dol Common, their colleague]{Dol}{dol}
\Character[Dapper, a lawyer's clerk]{Dapper}{dapper}
\Character[Drugger, a tobacco man]{Drugger}{drugger}
\Character[Lovewit, master of the house]{Lovewit}{lovewit}
\Character[Sir Epicure Mammon, a knight]{Mammon}{mammon}
\Character[Pertinax Surly, a gamester]{Surly}{surly}
\Character[Tribulation Wholesome, a pastor of Amsterdam]{Tribulation}{tribulation}
\Character[Ananias, a deacon there]{Ananias}{ananias}
\Character[Kastril, the angry boy]{Kastril}{kastril}
\Character[Dame Pliant, his sister, a widow]{Pliant}{pliant}
\Character[A Neighbour]{Neigh. 1}{neighone}
\Character[Another Neighbour]{Neigh. 2}{neightwo}
\Character[Another Neighbour]{Neigh. 3}{neighthree}
\Character[Another Neighbour]{Neigh. 4}{neighfour}
\Character[Another Neighbour]{Neigh. 5}{neighfive}
\Character[Another Neighbour]{Neigh. 6}{neighsix}
\Character[An officer]{Officer 1}{officerone}
\Character[Another officer]{Officer 2}{officertwo}
\settowidth{\speakswidth}{\speaksfont{Tribulation}\speaksdel\hspace{\Dlabelsep}}
\setlength\speaksindent{0in}
\DramPer
\begin{drama*}
'''.split('\n')
def suffix():
return r'''
\end{drama*}
\end{document}
'''.split('\n')
if __name__ == '__main__':
main()