-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_database.py
56 lines (43 loc) · 1.87 KB
/
create_database.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
import csv
from os import listdir
from os.path import isfile, join, isdir
SUBTITLES_DIR = "TBBT/transcripts"
DATABASE_PATH = 'data/tbbt_db.csv'
NEW_SCENE = "Scene"
DELIMITER = '§'
if __name__ == "__main__":
db_full = []
# loop over seasons
seasons_dir = [f for f in listdir(SUBTITLES_DIR) if isdir(join(SUBTITLES_DIR, f))]
for season_dir in seasons_dir:
i_season = season_dir.split("Season")[-1]
# loop over episodes
episodes_files = [f for f in listdir("{}/{}".format(SUBTITLES_DIR, season_dir)) if
isfile("{}/{}/{}".format(SUBTITLES_DIR, season_dir, f))]
for episodes_file in episodes_files:
# get episode number
if int(i_season) == 1:
i_episode = episodes_file[-2:]
else:
i_episode = episodes_file.split("-")[3]
with open("{}/{}/{}".format(SUBTITLES_DIR, season_dir, episodes_file)) as csvfile:
reader = csv.reader(csvfile, delimiter=':', quotechar="|")
i_scene = 0
scene_id = "{}_{}_{}".format(i_season, i_episode, i_scene)
for row in reader:
if len(row) == 0:
continue
# if new scene
elif row[0] == NEW_SCENE:
i_scene += 1
# if dialogue
else:
scene_id = "{}_{}_{}".format(i_season, i_episode, i_scene)
locutor = row[0]
sentences = "".join(row[1:])
db_full.append([i_season, i_episode, str(i_scene), scene_id, locutor, sentences])
# write database
with open(DATABASE_PATH, "w", newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=DELIMITER)
for row in db_full:
writer.writerow(row)