forked from gdamaskinos/unsupervised_topic_segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
253 lines (208 loc) · 8.4 KB
/
eval.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python3
import logging
from bisect import bisect
from typing import Dict
from core import topic_segmentation
from dataset import (
ami_dataset,
icsi_dataset,
)
from types import (
TopicSegmentationAlgorithm,
TopicSegmentationDatasets,
TopicSegmentationConfig,
)
from nltk.metrics.segmentation import pk, windowdiff
def compute_metrics(prediction_segmentations, binary_labels, metric_name_suffix=""):
print(prediction_segmentations)
print(binary_labels)
_pk, _windiff = [], []
for meeting_id, reference_segmentation in binary_labels.items():
predicted_segmentation_indexes = prediction_segmentations[meeting_id]
# we need to convert from topic changes indexes to topic changes binaries
predicted_segmentation = [0] * len(reference_segmentation)
for topic_change_index in predicted_segmentation_indexes:
predicted_segmentation[topic_change_index] = 1
reference_segmentation = "".join(map(str, reference_segmentation))
predicted_segmentation = "".join(map(str, predicted_segmentation))
_pk.append(pk(reference_segmentation, predicted_segmentation))
# setting k to default value used in CoAP (pk) function for both evaluation functions
k = int(
round(
len(reference_segmentation) / (reference_segmentation.count("1") * 2.0)
)
)
_windiff.append(windowdiff(reference_segmentation, predicted_segmentation, k))
avg_pk = sum(_pk) / len(binary_labels)
avg_windiff = sum(_windiff) / len(binary_labels)
print("Pk on {} meetings: {}".format(len(binary_labels), avg_pk))
print("WinDiff on {} meetings: {}".format(len(binary_labels), avg_windiff))
return {
"average_Pk_" + str(metric_name_suffix): avg_pk,
"average_windiff_" + str(metric_name_suffix): avg_windiff,
}
def binary_labels_flattened(
input_df,
labels_df,
meeting_id_col_name: str,
start_col_name: str,
end_col_name: str,
caption_col_name: str,
):
"""
Binary Label [0, 0, 1, 0] for topic changes as ntlk format.
Hierarchical topic strutcure flattened.
see https://www.XXXX.com/intern/anp/view/?id=434543
"""
labels_flattened = {}
meeting_ids = list(set(input_df[meeting_id_col_name]))
for meeting_id in meeting_ids:
logging.info("\n\nMEETING ID:{}".format(meeting_id))
if meeting_id not in list(labels_df[meeting_id_col_name]):
logging.info("{} not found in `labels_df`".format(meeting_id))
continue
meeting_data = input_df[
input_df[meeting_id_col_name] == meeting_id
].sort_values(by=[start_col_name])
meeting_sentences = [*map(lambda s: s.lower(), list(meeting_data["caption"]))]
caption_start_times = list(meeting_data[start_col_name])
segment_start_times = list(
labels_df[labels_df[meeting_id_col_name] == meeting_id][start_col_name]
)
meeting_labels_flattened = [0] * len(caption_start_times)
# we skip first and last labaled segment cause they are naive segments
for sst in segment_start_times[1:]:
try:
topic_change_index = caption_start_times.index(sst)
except ValueError:
topic_change_index = bisect(caption_start_times, sst)
if topic_change_index == len(meeting_labels_flattened):
topic_change_index -= 1 # bisect my go out of boundary
meeting_labels_flattened[topic_change_index] = 1
labels_flattened[meeting_id] = meeting_labels_flattened
logging.info("MEETING TRANSCRIPTS")
for i, sentence in enumerate(meeting_sentences):
if meeting_labels_flattened[i] == 1:
logging.warning("\n\n<<------ Topic Change () ------>>\n")
logging.info(sentence)
return labels_flattened
def binary_labels_top_level(
input_df,
labels_df,
meeting_id_col_name: str,
start_col_name: str,
end_col_name: str,
caption_col_name: str,
):
"""
Binary Label [0, 0, 1, 0] for topic changes as ntlk format.
Hierarchical topic strutcure only top level topics
see https://www.XXXX.com/intern/anp/view/?id=434543
"""
labels_top_level = {}
meeting_ids = list(set(input_df[meeting_id_col_name]))
for meeting_id in meeting_ids:
logging.info("\n\nMEETING ID:{}".format(meeting_id))
if meeting_id not in list(labels_df[meeting_id_col_name]):
logging.info("{} not found in `labels_df`".format(meeting_id))
continue
meeting_data = input_df[
input_df[meeting_id_col_name] == meeting_id
].sort_values(by=[start_col_name])
meeting_sentences = [*map(lambda s: s.lower(), list(meeting_data["caption"]))]
caption_start_times = list(meeting_data[start_col_name])
segment_start_times = list(
labels_df[labels_df[meeting_id_col_name] == meeting_id][start_col_name]
)
segment_end_times = list(
labels_df[labels_df[meeting_id_col_name] == meeting_id][end_col_name]
)
meeting_labels_top_level = [0] * len(caption_start_times)
high_level_topics_indexes = []
i = 0
while i < len(segment_end_times):
end = segment_end_times[i]
high_level_topics_indexes.append(i)
if segment_end_times.count(end) == 2:
# skip all the subtopics of this high level topic
i = (
segment_end_times.index(end)
+ segment_end_times[segment_end_times.index(end) + 1 :].index(end)
+ 2
)
else:
i += 1
segment_start_times_high_level = [
segment_start_times[i] for i in high_level_topics_indexes
]
# we skip first and last labaled segment cause they are naive segments
for sst in segment_start_times_high_level[1:]:
try:
topic_change_index = caption_start_times.index(sst)
except ValueError:
topic_change_index = bisect(caption_start_times, sst)
if topic_change_index == len(meeting_labels_top_level):
topic_change_index -= 1 # bisect my go out of boundary
meeting_labels_top_level[topic_change_index] = 1
labels_top_level[meeting_id] = meeting_labels_top_level
logging.info("MEETING TRANSCRIPTS")
for i, sentence in enumerate(meeting_sentences):
if meeting_labels_top_level[i] == 1:
logging.warning("\n\n<<------ Topic Change () ------>>\n")
logging.info(sentence)
return labels_top_level
MEETING_ID_COL_NAME = "meeting_id"
START_COL_NAME = "st"
EN_COL_NAME = "en"
CAPTION_COL_NAME = "caption"
def eval_topic_segmentation(
dataset_name: TopicSegmentationDatasets,
topic_segmentation_algorithm: TopicSegmentationAlgorithm,
topic_segmentation_config: TopicSegmentationConfig,
) -> Dict[str, float]:
if dataset_name == TopicSegmentationDatasets.AMI:
input_df, label_df = ami_dataset()
elif dataset_name == TopicSegmentationDatasets.ICSI:
input_df, label_df = icsi_dataset()
elif dataset_name == TopicSegmentationDatasets.TEST:
input_df, label_df = test_video_dataset()
else:
raise NotImplementedError("Unknown dataset_name given.")
prediction_segmentations = topic_segmentation(
topic_segmentation_algorithm,
input_df,
MEETING_ID_COL_NAME,
START_COL_NAME,
EN_COL_NAME,
CAPTION_COL_NAME,
topic_segmentation_config,
)
flattened = binary_labels_flattened(
input_df,
label_df,
MEETING_ID_COL_NAME,
START_COL_NAME,
EN_COL_NAME,
CAPTION_COL_NAME,
)
top_level = binary_labels_top_level(
input_df,
label_df,
MEETING_ID_COL_NAME,
START_COL_NAME,
EN_COL_NAME,
CAPTION_COL_NAME,
)
flattened_metrics = compute_metrics(
prediction_segmentations, flattened, metric_name_suffix="flattened"
)
top_level_metrics = compute_metrics(
prediction_segmentations, top_level, metric_name_suffix="top_level"
)
def merge_metrics(*metrics):
res = {}
for m in metrics:
for k, v in m.items():
res[k] = v
return res
return merge_metrics(flattened_metrics, top_level_metrics)