-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdataset.py
More file actions
146 lines (123 loc) · 5.74 KB
/
dataset.py
File metadata and controls
146 lines (123 loc) · 5.74 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
from swda.swda import CorpusReader
from os import listdir
from os.path import isfile, join
from train_set_preferences import mrda_train_set_idx, mrda_valid_set_idx, mrda_test_set_idx
def load_swda_corpus_data(swda_directory):
print('Loading SwDA Corpus...')
corpus_reader = CorpusReader(swda_directory)
talks = []
talk_names = []
tags_seen = set()
tag_occurances = {}
for transcript in corpus_reader.iter_transcripts(False):
name = 'sw' + str(transcript.conversation_no)
talk_names.append(name)
conversation_content = []
conversation_tags = []
for utterance in transcript.utterances:
conversation_content.append( utterance.text_words(True) )
tag = utterance.damsl_act_tag()
conversation_tags.append( tag )
if tag not in tags_seen:
tags_seen.add(tag)
tag_occurances[tag] = 1
else:
tag_occurances[tag] += 1
talks.append( (conversation_content, conversation_tags) )
print('\nFound ' + str(len(tags_seen))+ ' different utterance tags.\n')
tag_indices = {tag:i for i, tag in enumerate(sorted(list(tags_seen)))}
for talk in talks:
talk_tags = talk[1]
for i, tag in enumerate(talk_tags):
talk_tags[i] = tag_indices[ tag ]
print('Loaded SwDA Corpus.')
return talks, talk_names, tag_indices, tag_occurances
def load_mrda_corpus_data(mrda_directory):
print('Loading MRDA Corpus...')
talks = []
talk_names = []
tags_seen = set()
tag_occurances = {}
talk_names_set = mrda_train_set_idx.union(mrda_valid_set_idx).union(mrda_test_set_idx)
talk_names = list(talk_names_set)
file_list = [f for f in listdir(mrda_directory) if isfile(join(mrda_directory, f))]
file_list = list(filter(lambda f: f in talk_names_set, file_list))
for talk in talk_names:
utterances = []
tags = []
with open(join(mrda_directory, '%s.out' % talk), 'r') as f:
for line in f:
line_components = line.split(',')
utterance_id = line_components[0]
utter_text = line_components[1]
utter_speech_act = line_components[3]
if len(utter_speech_act) == 0:
continue
utterances_to_add = []
for utterance in utter_text.split('|'):
utterances_to_add.append(utterance.split())
separated_tags = utter_speech_act.split('|')
if '.' in separated_tags[-1]:
last_tag = separated_tags[-1]
separated_tags = separated_tags[:-1] + last_tag.split('.', 1)
tags_to_add = []
if len(separated_tags) == 1 and separated_tags[0][0] == '.':
#Disruption form
tags_to_add.append('d')
elif len(separated_tags) == 1 and separated_tags[0][0] == 'z':
#Purposefully untagged utterance
for utter in utterances_to_add:
tags_to_add.append('z')
#continue
else:
for tag in separated_tags:
tag_initial = tag[0]
if tag_initial == 'h':
tag_initial = 'f'
elif tag_initial == 'x' or tag_initial == '%':
tag_initial = 'd'
elif tag_initial != 'f' and tag_initial != 'b' and tag_initial != 'q' and tag_initial != 's':
print("PROBLEM:")
print("Weird tag found!")
print("Talk name: %s" % talk)
print("Utterance ID: %s" % utterance_id)
print("Tag: %s" % tag_to_use)
print("Tag in question: %s" % tag)
exit(0)
tags_to_add.append(tag_initial)
for tag in tags_to_add:
if tag in tag_occurances:
tag_occurances[tag] += 1
else:
tags_seen.add(tag)
tag_occurances[tag] = 1
if len(utterances_to_add) == len(tags_to_add) - 1 and tags_to_add[-1] == 'd':
del tags_to_add[-1]
elif len(utterances_to_add) != len(tags_to_add):
print("PROBLEM A:")
print("Tags are not equal to utterances!")
print("Talk name: %s" % talk)
print("Utterance ID: %s" % utterance_id)
print("DA Label: %s" % tag_to_use)
print("Tag in question: %s" % tag)
print("len(utterance_to_add): %d" % len(utterances_to_add))
print("len(tags_to_add): %d" % len(tags_to_add))
print("utterance_to_add: %s" % str(utterances_to_add))
print("tags_to_add: %s" % str(tags_to_add))
utterances += utterances_to_add
tags += tags_to_add
if len(utterances) != len(tags):
print("PROBLEM:")
print("Talk name: %s" % talk)
print("Utterance: %d" % len(utterances))
print("Tags: %d" % len(tags))
exit(0)
talks.append( (utterances, tags) )
print('\nFound ' + str(len(tags_seen))+ ' different utterance tags.\n')
tag_indices = {tag:i for i, tag in enumerate(sorted(list(tags_seen)))}
for talk in talks:
talk_tags = talk[1]
for i, tag in enumerate(talk_tags):
talk_tags[i] = tag_indices[ tag ]
print('Loaded MRDA Corpus.')
return talks, talk_names, tag_indices, tag_occurances