Kevin Peng
commited on
Add Cornell Movie Dialogue parser
Browse filesParses the Cornell Movie Dialogue Corpus from
https://www.cs.cornell.edu/~cristian/Cornell_Movie-Dialogs_Corpus.html
utils/cornell_movie_dialogue_parse.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Parses the Cornell Movie Dialogue Corpus https://www.cs.cornell.edu/~cristian/Cornell_Movie-Dialogs_Corpus.html
|
2 |
+
import argparse
|
3 |
+
import re
|
4 |
+
import tqdm
|
5 |
+
|
6 |
+
from clean import clean
|
7 |
+
|
8 |
+
|
9 |
+
class InvalidFormatError(Exception):
|
10 |
+
pass
|
11 |
+
|
12 |
+
|
13 |
+
class InvalidLineError(Exception):
|
14 |
+
pass
|
15 |
+
|
16 |
+
|
17 |
+
def format_name(name, raw_name):
|
18 |
+
return name if raw_name else name[0].upper() + name[1:].lower()
|
19 |
+
|
20 |
+
|
21 |
+
# parses movie_lines.txt into a dictionary with the lineID as the key and the line as the value
|
22 |
+
def get_lines(filename, raw_name=False):
|
23 |
+
lines = dict()
|
24 |
+
with open(filename, 'r', encoding='windows-1252') as file:
|
25 |
+
for row in tqdm.tqdm(file, desc='Reading lines'):
|
26 |
+
row = row.split(' +++$+++ ')
|
27 |
+
if len(row) != 5:
|
28 |
+
raise InvalidFormatError(f'{filename} is not formatted correctly')
|
29 |
+
|
30 |
+
lineID = row[0]
|
31 |
+
name = row[3]
|
32 |
+
line = row[4]
|
33 |
+
|
34 |
+
# It seems that the dataset contains lines that aren't actually lines and have no name
|
35 |
+
if len(name) == 0:
|
36 |
+
lines[lineID] = None
|
37 |
+
continue
|
38 |
+
lines[lineID] = clean(format_name(name, raw_name) + ': ' + line)
|
39 |
+
return lines
|
40 |
+
|
41 |
+
|
42 |
+
# parses movie_titles_metadata.txt into a dictionary with the movieID as the key and the value a dictionary
|
43 |
+
# with the title, year, rating, num_votes, and genres
|
44 |
+
def get_movie_metadata(filename):
|
45 |
+
metadata = dict()
|
46 |
+
with open(filename, 'r', encoding='windows-1252') as file:
|
47 |
+
for row in tqdm.tqdm(file, desc='Reading movie metadata'):
|
48 |
+
row = row.rstrip('\n').split( ' +++$+++ ')
|
49 |
+
if len(row) != 6:
|
50 |
+
raise InvalidFormatError(f'{filename} is not formatted correctly')
|
51 |
+
movieID = row[0]
|
52 |
+
metadata[movieID] = {'title': row[1], 'year': row[2], 'rating':row[3], 'num_votes': row[4], 'genres': row[5]}
|
53 |
+
return metadata
|
54 |
+
|
55 |
+
|
56 |
+
def format_movie_metadata(metadata):
|
57 |
+
return f'[Title: \'{metadata["title"]}\'; Year: {metadata["year"]}; Rating: {metadata["rating"]}; Num_votes: {metadata["num_votes"]}; Genres: {metadata["genres"]};]'
|
58 |
+
|
59 |
+
|
60 |
+
def construct_dialogue(lineIDs, lines):
|
61 |
+
dialogue = []
|
62 |
+
for lineID in lineIDs:
|
63 |
+
if lines[lineID] is None:
|
64 |
+
raise InvalidLineError()
|
65 |
+
dialogue.append(lines[lineID])
|
66 |
+
return '\n'.join(dialogue)
|
67 |
+
|
68 |
+
|
69 |
+
# parses movie_conversations.txt and puts the results in out_filename
|
70 |
+
def parse_conversations(conversation_filename, out_filename, lines, movie_metadata):
|
71 |
+
with open(conversation_filename, 'r', encoding='utf-8') as conversation_file, open(out_filename, 'w', encoding='utf-8') as out_file:
|
72 |
+
for row in tqdm.tqdm(conversation_file, desc='Constructing dialogue'):
|
73 |
+
row = row.split(' +++$+++ ')
|
74 |
+
if len(row) != 4:
|
75 |
+
raise InvalidFormatError(f'{conversation_filename} is not formatted correctly')
|
76 |
+
|
77 |
+
movieID = row[2]
|
78 |
+
lineIDs = re.findall(r'\'(\w+)\'', row[3])
|
79 |
+
try:
|
80 |
+
dialogue = construct_dialogue(lineIDs, lines)
|
81 |
+
except InvalidLineError:
|
82 |
+
continue
|
83 |
+
out_file.write(f'⁂\n{format_movie_metadata(movie_metadata[movieID])}\n⁂\n')
|
84 |
+
out_file.write(f'{dialogue}\n')
|
85 |
+
|
86 |
+
|
87 |
+
parser = argparse.ArgumentParser(description='Process Cornell Movie Dialogue Corpus', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
88 |
+
parser.add_argument('-i', '--in_dir', type=str, help='directory to process', default='.')
|
89 |
+
parser.add_argument('-o', '--out_file', type=str, help='file to output', default='cornell_movie_dialogue.txt')
|
90 |
+
parser.add_argument('-r', '--raw_name', action='store_true', help='use speaker tags as they appear in the dataset instead of normalized names')
|
91 |
+
args = parser.parse_args()
|
92 |
+
|
93 |
+
if __name__ == '__main__':
|
94 |
+
try:
|
95 |
+
lines = get_lines(args.in_dir + '/movie_lines.txt', args.raw_name);
|
96 |
+
movie_metadata = get_movie_metadata(args.in_dir + '/movie_titles_metadata.txt')
|
97 |
+
parse_conversations(args.in_dir + '/movie_conversations.txt', args.out_file, lines, movie_metadata)
|
98 |
+
except (FileNotFoundError, InvalidFormatError) as e:
|
99 |
+
print(e)
|
100 |
+
exit(1)
|
101 |
+
|