-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_metrics.py
186 lines (155 loc) · 6.71 KB
/
compute_metrics.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
from data_overlap_spec import DataOverlapStatsKey, EntryOverlapNgrams, OverlapMetric, MetricProtocolSpec, PartialOverlapSpec, FrequencySpec, EntryOverlapMetric
from collections import defaultdict
import ast
from nltk import ngrams
def compute_binary_overlap(instance_str, overlapping_ngram_counts, tokenizer, frequency = 0):
"""
Compute binary overlap
If pass in frequency, include only the ngrams with count <= frequency
"""
tokens = tokenizer.tokenize(instance_str)
ngram_counts_dict = defaultdict(int)
# construct a dict of ngram -> count
for ngram, count in overlapping_ngram_counts:
ngram = tuple(ast.literal_eval(ngram))
ngram_counts_dict[ngram] = count
metric_score = 0
for ngram in ngrams(tokens, 13):
count = ngram_counts_dict[ngram]
if frequency == 0 or count <= frequency:
if count != 0:
metric_score = 1
break
overlap_metric = OverlapMetric(
metric_score = metric_score,
metric_protocol_spec = MetricProtocolSpec(
partial_overlap_spec = PartialOverlapSpec.binary,
frequency_spec = FrequencySpec(
filter_value = frequency,
weighting = False
)
)
)
return overlap_metric
def compute_jaccard_overlap(instance_str, overlapping_ngram_counts, tokenizer, frequency = 0):
"""
Compute weighted and unweighted jaccard overlap
If pass in frequency, include only the ngrams with count <= frequency
"""
tokens = tokenizer.tokenize(instance_str)
ngram_counts_dict = defaultdict(int)
# construct a dict of ngram -> count
for ngram, count in overlapping_ngram_counts:
ngram = tuple(ast.literal_eval(ngram))
ngram_counts_dict[ngram] = count
total_ngram_count = 0
counts = 0
weighted_score = 0
for ngram in ngrams(tokens, 13):
count = ngram_counts_dict[ngram]
if frequency == 0 or count <= frequency:
if count != 0:
counts += 1
weighted_score += 1 / count
total_ngram_count += 1
unweighted_score = counts / total_ngram_count
weighted_score = weighted_score / total_ngram_count
unweighted_overlap_metric = OverlapMetric(
metric_score = unweighted_score ,
metric_protocol_spec = MetricProtocolSpec(
partial_overlap_spec = PartialOverlapSpec.jaccard,
frequency_spec = FrequencySpec(
filter_value = frequency,
weighting = False
)
)
)
weighted_overlap_metric = OverlapMetric(
metric_score = weighted_score ,
metric_protocol_spec = MetricProtocolSpec(
partial_overlap_spec = PartialOverlapSpec.jaccard,
frequency_spec = FrequencySpec(
filter_value = frequency,
weighting = True
)
)
)
return unweighted_overlap_metric, weighted_overlap_metric
# Token overlap
def compute_token_overlap(instance_str, overlapping_ngram_counts, tokenizer, frequency = 0):
"""
Compute weighted and unweighted token overlap
If pass in frequency, include only the ngrams with count <= frequency
"""
tokens = tokenizer.tokenize(instance_str)
ngram_counts_dict = defaultdict(int)
# construct a dict of ngram -> count
for ngram, count in overlapping_ngram_counts:
ngram = tuple(ast.literal_eval(ngram))
ngram_counts_dict[ngram] = count
total_token_count = 0
counts = 0
weighted_score = 0
weight = 0
token_budget = 0
for ngram in ngrams(tokens, 13):
curr_count = ngram_counts_dict[ngram]
# either no frequency, or check current count is less than frequency
# or a previous contiguous count (weight != 0) less than frequency
if frequency == 0 or curr_count <= frequency or (weight != 0 and weight <= frequency):
if curr_count != 0:
token_budget = 13
if weight > 0:
weight = min(curr_count, weight)
else:
weight = curr_count
if token_budget > 0:
token_budget -= 1
counts += 1
weighted_score += 1 / weight
else:
weight = 0
total_token_count += 1
for token in ngram[1:]:
if token_budget > 0:
token_budget -= 1
counts += 1
weighted_score += 1 / weight
total_token_count += 1
unweighted_score = counts / total_token_count
weighted_score = weighted_score / total_token_count
unweighted_overlap_metric = OverlapMetric(
metric_score = unweighted_score ,
metric_protocol_spec = MetricProtocolSpec(
partial_overlap_spec = PartialOverlapSpec.token,
frequency_spec = FrequencySpec(
filter_value = frequency,
weighting = False
)
)
)
weighted_overlap_metric = OverlapMetric(
metric_score = weighted_score ,
metric_protocol_spec = MetricProtocolSpec(
partial_overlap_spec = PartialOverlapSpec.token,
frequency_spec = FrequencySpec(
filter_value = frequency,
weighting = True
)
)
)
return unweighted_overlap_metric, weighted_overlap_metric
def compute_and_add_metrics(instance_str, overlapping_ngram_counts, tokenizer, entry_data_overlap_key, entry_overlap_metric_list, frequency = 0):
overlap_metric = compute_binary_overlap(instance_str, overlapping_ngram_counts, tokenizer, frequency)
binary_metric = EntryOverlapMetric(entry_data_overlap_key=entry_data_overlap_key, overlap_metric=overlap_metric)
entry_overlap_metric_list.append(binary_metric)
unweighted_overlap_metric, weighted_overlap_metric = compute_jaccard_overlap(instance_str, overlapping_ngram_counts, tokenizer, frequency)
unweighted_jaccard = EntryOverlapMetric(entry_data_overlap_key=entry_data_overlap_key, overlap_metric=unweighted_overlap_metric)
weighted_jaccard = EntryOverlapMetric(entry_data_overlap_key=entry_data_overlap_key, overlap_metric=weighted_overlap_metric)
entry_overlap_metric_list.append(unweighted_jaccard)
entry_overlap_metric_list.append(weighted_jaccard)
unweighted_overlap_metric, weighted_overlap_metric = compute_token_overlap(instance_str, overlapping_ngram_counts, tokenizer, frequency)
unweighted_token = EntryOverlapMetric(entry_data_overlap_key=entry_data_overlap_key, overlap_metric=unweighted_overlap_metric)
weighted_token = EntryOverlapMetric(entry_data_overlap_key=entry_data_overlap_key, overlap_metric=weighted_overlap_metric)
entry_overlap_metric_list.append(unweighted_token)
entry_overlap_metric_list.append(weighted_token)