-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstar_trek_parse.py
executable file
·153 lines (130 loc) · 4.44 KB
/
star_trek_parse.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
#!/usr/bin/python
import script as s
import sys
import re
keywords = {
'ENTERS': s.ENTER,
'ENTER': s.ENTER,
'EXIT': s.EXIT,
'EXITS': s.EXIT,
'WITHDRAW': s.EXIT,
'WITHDRAWS': s.EXIT,
'LEAVE': s.EXIT,
'LEAVES': s.EXIT
}
def parse(path):
f = open(path, 'r')
script = s.Script()
# Get characters!
for line in f:
if line.strip() == "CAST": break
print "Cast:"
for line in f:
if ("STAR TREK" in line):
break
line = line.strip()
if line == "": continue
chars = re.split("\s\s+", line)
for char in chars:
if char == "Non-Speaking":continue # Special case!!!
script.addCharacter(char)
print "Character:", char
# Get sets!
for line in f:
if line.strip() == "SETS": break
print "Sets:"
for line in f:
if ("STAR TREK" in line):
break
line = line.strip()
if line == "": continue
sets = re.split("\s\s+", line)
for setting in sets:
if script.addSetting(setting):
print "Set:", setting
# Get scenes!
scenetexts = []
curscene = []
for line in f:
if line.strip() == "THE END" or re.match("^\d+.*$", line):
if "CONTINUED" in line or "ANGLE" in line or \
not ("INT." in line or "EXT." in line or "OMITTED" in line):
print ":::", line
curscene[0] = scenetexts[-1][0]
scenetexts.append(curscene)
curscene = []
if line.strip() == "THE END":
break
curscene.append(line)
scenetexts = scenetexts[1:] # Remove some crap.
seenchars = set()
for text in scenetexts:
descr = text[0].strip()
setting = script.getSetting(descr)
print descr, "==>", setting.name
scene = s.Scene(text[0])
scene.setSetting(setting)
actuallines = []
lastline = (0,"")
for line in text[1:]:
tabs = 0
for rune in line:
if rune == "\t":
tabs += 1
else:
break
line = line.strip()
if tabs == 0 or line == "":
continue
if tabs == lastline[0]:
lastline = (tabs, lastline[1] + " " + line)
else:
actuallines.append(lastline)
lastline = (tabs, line)
if lastline[0] != 0:
actuallines.append(lastline)
curchar = None
for line in actuallines:
tabs, line = line
if tabs == 1: # Stage direction
direction = s.StageDirection(line)
scene.addDirection(direction)
sentences = line.split('.')
for sentence in sentences:
sentence = re.sub('[^\s\w]', '', sentence.upper())
words = sentence.split()
action = s.BACKGROUND
for act in [keywords[k] for k in keywords if k in sentence]:
action = act
break
chars = set()
for character in script.characters.values():
if character.name in words:
chars.add(character)
for char in chars:
direction.addAction(action, char)
print (action, char.name)
elif tabs == 3: # Dialog
scene.addDirection(s.Dialog(curchar, line))
elif tabs == 4: # Stage direction for character
scene.addDirection(s.StageDirection(line, character=curchar))
elif tabs == 5: # Character
curchar = script.getCharacter(line)
if not line in seenchars:
print line, "==>", curchar.name
seenchars.add(line)
else:
pass # Eh, this probably shouldn't happen. Whatever.
script.addScene(scene)
return script
if __name__=="__main__":
if len(sys.argv) != 2:
print "USAGE: ./star_trek_parse <scriptpath>"
exit(1)
print "Parsing script from %s" % sys.argv[1]
script = parse(sys.argv[1])
print "Scene 1:"
scene1 = script.scenes[0]
for action in scene1.directions:
if isinstance(action, s.Dialog):
print "%s: %s" % (action.character.name, action.text)