-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileparser.py
68 lines (54 loc) · 2.22 KB
/
fileparser.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
import json
import os
def split_nps_json(origin, destination, key, name=''):
"""Splits National Parks JSON into indivdual documents"""
with open(origin, 'r') as f:
full_json = json.load(f)
if not os.path.exists(destination):
os.makedirs(destination)
file_num = 1
for document in full_json[key]:
with open('{}/{}{}.json'.format(destination, name, file_num), 'w') as j:
j.write(json.dumps(document))
file_num += 1
def scripts_to_json(origin, destination='movie_jsons'):
"""Cleans script texts and saves as JSON in desired directory"""
if not os.path.exists(destination):
os.makedirs(destination)
count = 0
for subdir, dirs, files in os.walk(origin):
for file in files:
if count >= 650:
break
genre = subdir.split('/')[-1]
with open('{}/{}'.format(subdir, file), 'r') as f:
dirty_script = f.read()
clean = clean_script(dirty_script)
lines = clean.split('\n')
title = lines[0]
script_dict = {'title': title, 'genre': genre, 'body': clean}
with open('{}/{}.json'.format(destination, file.split('.')[0]), 'w') as j:
j.write(json.dumps(script_dict))
count += 1
def clean_script(text):
text = text.replace('Back to IMSDb', '')
text = text.replace(
'''<b><!--
</b>if (window!= top)
top.location.href=location.href
<b>// -->
</b>''', '')
text = text.replace(''' Scanned by http://freemoviescripts.com
Formatting by http://simplyscripts.home.att.net
''', '')
text = text.replace('<b><!--', '')
text = text.replace('</b>', '')
text = text.replace('<b>/*', '')
text = text.replace('(c) 1990 The Walt Disney Company', '')
text = text.replace('------------------------------------------------------------', '')
text = text.replace('<script>', '')
text = text.replace('for educational use only', '')
text = text.replace('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-', '')
return text.replace(r'\r', '').strip()
if __name__ == "__main__":
scripts_to_json('data/imsdb_movies', 'data/script_jsons')