-
Notifications
You must be signed in to change notification settings - Fork 3
/
1-scripts_to_data_char_lines.py
153 lines (116 loc) · 4.56 KB
/
1-scripts_to_data_char_lines.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
import os
from collections import defaultdict
import re
##Change paths to relative paths
mychardict = defaultdict(list)
def process_file(fi):
# Creates list of (character, line) tuples identifying the speaker of each line (in script character's
# will have multi-line dialogue that is assigned the last character name found to link them to speaker
newlines = []
with open(fi, encoding="utf-8") as infile:
lines = infile.readlines()
inside_script = False
previous_char = ""
previous_lines = ""
inside_parenthesis = False
for line in lines:
if line.startswith("<Back"):
inside_script=False
elif inside_script:
if line.count(":")==1: #some weird lines with timestamps in stage notes
character, punct, current_line = line.partition(":")
character = clean(character)
line = clean(line)
line = clean_line(line)
if is_valid(character):
previous_char = character
previous_lines = current_line
newlines.append((character, current_line))
else:
line = clean(line)
line = clean_line(line)
current_line = line.strip()
character = previous_char
previous_lines += current_line
newlines.append((previous_char, current_line))
elif "Original Airdate:" in line:
inside_script = True
else:
pass
return newlines
def clean(char):
# flags = ["{", "[", "("]
char = re.sub("[\(\[].*?[\)\]]", "", char)
char = char.upper()
## if any(f in char for f in flags):
## return re.sub("[\(\[].*?[\)\]]", "", char)
return char
def clean_line(line):
try:
if "(" in line:
line_keep, line_discard = line.split("(")
line = line_keep
elif ")" in line:
line_discard, line_keep = line.split(")")
line = line_keep
except ValueError:
line=""
return line
def create_char_dict(newlines):
mydict = defaultdict(list)
for i in range(0, len(newlines) - 1):
ch, line = newlines[i]
ch = re.sub("[\(\[].*?[\)\]]", "", ch) #remove parens and brackets from char name
ch = ch.strip()
ch = ch.upper() #standardise all name to uppercass
line = re.sub("[\(\[].*?[\)\]]", "", line) #remove text inside parens and brackets
mydict[ch].append(line)
mychardict[ch].append(line)
return mydict
def dir_dir(dirname):
if os.path.isdir(dirname):
pass
#print("directory already exists")
else:
os.mkdir(dirname)
def is_valid(charname):
if charname=="":
return False
flags=["/", "\\", "?", "|", "<", ">", "(", "[", "+", ","]
if any(flag in charname for flag in flags):
return False
return True
def create_dest(folder):
_, series = folder.split("_")
dirname = os.path.join(".\data_char_lines", series)
dir_dir(dirname)
return dirname
def write_lines(mydict, dest_folder):
for char, lines in mydict.items():
#if check_keys(k):
fo = os.path.join(dest_folder, char + ".txt")
with open(fo, "a", encoding="utf-8") as outfile:
lines_clean = " ".join(i.strip() for i in lines)
outfile.write(lines_clean)
outfile.write("\n")
root = ".\data"
folders = os.listdir(root)
root_dest = ".\data_char_lines"
dir_dir(root_dest)
##if destination directory exists, it will be removed and replaced
def process_all_scripts():
for folder in folders: # folders in data/ are series folder with scripts within
files = os.listdir(os.path.join(root, folder))
for f in files:
# give me the file locations
file_loc = os.path.join(root, folder, f)
# assign lines of dialogue to character they belong to, returns list
character_lines = process_file(file_loc)
# create dictionary of characters with lines as value, and normalize to reduce set
char_dict = create_char_dict(character_lines)
# create destination folders/files to save text
dest_folder = create_dest(folder)
# write character lines to files
write_lines(char_dict, dest_folder)
process_all_scripts()
print(len(mychardict))