forked from K10ForTheWin/Text_Analysis_Technobabble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessAllScripts2.py
More file actions
240 lines (196 loc) · 5.52 KB
/
ProcessAllScripts2.py
File metadata and controls
240 lines (196 loc) · 5.52 KB
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
import os
from collections import defaultdict
##Change paths to relative paths
def process_file(fi):
## fname = os.path.join(root, name)
newlines = []
with open(fi, encoding='utf-8') as infile:
lines = infile.readlines()
previous_char = ''
previous_lines = ''
seen_previous_line = False
for line in lines:
if ":" in line:
character, punct, current_line = line.partition(":")
previous_char = character
previous_lines = current_line
seen_previous_line = True
newlines.append((character, current_line))
else:
current_line = line.strip()
character = previous_char
previous_lines += current_line
newlines.append((previous_char, current_line))
return newlines
def clean_values(mydict2):
md = {}
for k, v in mydict2.items():
if '/' in k:
pass
elif "?" in k:
pass
else:
v=v[0]
vv=[line.strip() for line in v]
vv=' '.join(vv)
md[k] = vv
return md
## print(vv)
def remove_dups(dict1):
dup_keys = {}
undup_keys = []
newdict = defaultdict(list)
for k in dict1.keys():
if len(k.split("["))==2:
kk, k0 = k.split("[")
k0 = k0.strip()
elif ")" in k:
#"Elsewhere -)SARIN": 'SARIN
try:
k0, kk = k.split(")")
except ValueError:
print(kk) #2 errors
else:
kk = k.strip()
dup_keys[k] = kk.strip()
undup_keys.append(kk)
## print(dup_keys)
## print(undup_keys)
for k, v in dict1.items():
if k not in undup_keys:
try:
newdict[dup_keys[k]].append(v)
except KeyError:
pass
else:
newdict[k].append(v)
return newdict
def create_char_dict(newlines):
mydict = defaultdict(list)
for i in range(0, len(newlines)-1):
ch, line = newlines[i]
mydict[ch].append(line)
mydict2 = remove_dups(mydict)
mydict2 = clean_values(mydict2)
return mydict2
def create_episode_folder(name):
if os.path.isdir(name):
pass
else:
os.mkdir(name)
def dir_dir(dirname):
if os.path.isdir(dirname):
pass
else:
os.mkdir(dirname)
def get_fo(root, k):
series = root.split("/")[-1]
fo = os.path.join("C:\Python3\data_char_lines\\", series, k+".txt")
def check_keys(k):
if k=='':
return False
elif k=='Original Airdate':
return False
elif k=='Stardate':
return False
#check if multiple words/character mentions
elif k.startswith(" "):
return False #idk
elif len(k.split(" "))>3:
print(k)
return False
else:
return True
def create_dest(folder):
_, series = folder.split("_")
dirname = os.path.join("C:\Python3\data_char_lines", series)
dir_dir(dirname)
return dirname
def write_lines(char_line_dict, dest_folder):
for k, v in char_line_dict.items():
if check_keys(k):
fo = os.path.join(dest_folder, k+".txt")
with open(fo, 'a', encoding='utf-8') as outfile:
print(fo)
outfile.write(v)
outfile.write("\n")
def fix_enterprise():
ent = "C:\Python3\data\scripts_Enterprise"
files = os.listdir(ent)
for file in files:
file_loc = os.path.join(ent, file)
## print(file_loc)
flines=process_file(file_loc)
ch_line_dict = create_char_dict(flines)
dest_folder="C:\Python3\data_char_lines\Enterprise"
write_lines(ch_line_dict, dest_folder)
root = "C:\Python3\data"
contents = os.listdir(root)
folders = [f for f in contents if os.path.isdir(f)]
root_dest = "C:\Python3\data_char_lines"
dir_dir(root_dest)
def process_all_scripts():
for folder in folders:
files = os.listdir(os.path.join(root, folder))
for f in files:
file_loc = os.path.join(folder, f)
## print(file_loc)
flines=process_file(file_loc)
ch_line_dict = create_char_dict(flines)
## print(sorted(ch_line_dict.keys()))
dest_folder = create_dest(folder)
write_lines(ch_line_dict, dest_folder)
##process_all_scripts()
##os.remove_dir("C:\Python3\data\scripts_Enterprise")
##os.mkdir("C:\Python3\data\scripts_Enterprise")
##fix_enterprise()
## for m in more:
## for mm in m:
## statinfo = os.stat(mm)
## size = statinfo.st_size
## sz = file_size(mm)
## print(file, sz)
def convert_bytes(num):
"""
this function will convert bytes to MB.... GB... etc
"""
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f %s" % (num, x)
num /= 1024.0
def file_size(file_path):
"""
this function will return the file size
"""
if os.path.isfile(file_path):
file_info = os.stat(file_path)
return convert_bytes(file_info.st_size)
root = "C:\Python3\data_char_lines"
contents = os.listdir(root)
##print(contents)
more = [os.listdir(os.path.join(root,c)) for c in contents]
mydict = {}
keep_files=[]
for c in contents:
file_sizes = []
r = os.path.join(root, c)
for file in os.listdir(r):
f = os.path.join(r, file)
statinfo = os.stat(f)
size = statinfo.st_size
if size > 9228:
keep_files.append(file)
file_sizes.append(size)
## print(f)
## sz = file_size(f)
## if not sz.endswith("bytes"):
## print(file, sz)
mydict[c] = file_sizes
print(c, len(file_sizes), min(file_sizes), max(file_sizes), sum(file_sizes)/len(file_sizes))
print(len(keep_files))
# 10 GB
##>>> os.stat("C:\Python3\data_char_lines\Voyager\GEGEN.txt")
##os.stat_result(st_mode=33206, st_ino=281474977348190, st_dev=606102944, st_nlink=1, st_uid=0, st_gid=0, st_size=9228, st_atime=1542864677, st_mtime=1542864677, st_ctime=1542864677)
##>>> x=_
##>>> x.st_size
##9228