-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathself-consistency.py
More file actions
334 lines (274 loc) · 10.3 KB
/
Copy pathself-consistency.py
File metadata and controls
334 lines (274 loc) · 10.3 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import json
import argparse
import sys
import re
import os
import logging
import math
from pathlib import Path
from tqdm import tqdm
from collections import defaultdict, deque
from typing import Any, Dict, List, Set, Tuple
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
def setup_arg_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Process Multi-intent Multi-task Self-consistency")
# input dir
parser.add_argument("--input-dir", type=str, required=True, help="Directory containing the deffetent reasoning paths result JSONL files")
# output file
parser.add_argument("--output-file", type=str, required=True, help="Path to the SFL-MTSC result output JSONL file")
# hyperparameters
parser.add_argument("--alpha", type=float, default=0.3, help="Hybrid Jaccard alpha (weight for key-value Jaccard)")
parser.add_argument("--tau", type=float, default=0.55, help="Similarity threshold for clustering")
return parser
def normalize_slot_key(text: str) -> str:
if '-' in text:
return text.split('-', 1)[1].strip()
return text.strip()
# --------------------
# Frame Clustering
# --------------------
## slot processing
def slot_pair(frame) -> Set[Tuple[str, str]]:
slots = frame.get("slots", {}) or {}
if not isinstance(slots, dict):
return set()
return {(str(k), str(v)) for k, v in slots.items()}
def slot_values(frame) -> Set[str]:
slots = frame.get("slots", {}) or {}
if not isinstance(slots, dict):
return set()
return {str(v) for v in slots.values()}
## Jaccard
def jaccard(a: Set[Any], b: Set[Any]) -> float:
if not a and not b:
return 1.0
if not a or not b:
return 0.0
return len(a & b) / len(a | b)
def hybrid_jaccard(fa, fb, alpha=0.3) -> float:
sa = slot_pair(fa)
sb = slot_pair(fb)
va = slot_values(fa)
vb = slot_values(fb)
j_kv = jaccard(sa, sb)
j_val = jaccard(va, vb)
sim_hyb = alpha * j_kv + (1 - alpha) * j_val
return sim_hyb
def cluster_hybrid_jaccard(frames, alpha=0.3, tau=0.55):
# 0) ignore some case
n = len(frames)
if n == 0:
return []
if n == 1:
return [frames[:]]
# 1) Build similarity graph
adj: List[List[int]] = [[] for _ in range(n)]
for i in range(n):
for j in range(i + 1, n):
sim = hybrid_jaccard(frames[i], frames[j], alpha=alpha)
if sim >= tau:
adj[i].append(j)
adj[j].append(i)
# 2) Connected components
visited = [False] * n
cluster: List[List[Dict[str, Any]]] = []
for i in range(n):
if visited[i]:
continue
queue = deque([i])
visited[i] = True
comp = []
while queue:
u = queue.popleft()
comp.append(frames[u])
for v in adj[u]:
if not visited[v]:
visited[v] = True
queue.append(v)
cluster.append(comp)
return cluster
# --------------------
# Self-Consistency Score
# --------------------
def compute_support(cluster: List[Dict]) -> int:
path_indices = set()
for frame in cluster:
p = frame.get("_path_idx")
if p is not None:
path_indices.add(p)
return len(path_indices)
# --------------------
# Filtering
# --------------------
def filter_di_clusters(di_cluster, k):
sup_threshold = math.ceil(k / 2)
retained = defaultdict(list)
for di_key, backet in di_cluster.items():
support = compute_support(backet)
if support >= sup_threshold:
retained[di_key] = backet
return retained
def filter_slot_clusters(slot_clusters, k):
sup_threshold = math.ceil(k / 2)
retained = defaultdict(list)
for di_key, clusters in slot_clusters.items():
for cluster in clusters:
support = compute_support(cluster)
if support >= sup_threshold:
retained[di_key].append(cluster)
return retained
# --------------------
# Re-integration
# --------------------
def voting_value(cluster: List[Dict]):
final_slots = {}
all_keys: Set[str] = set()
for frame in cluster:
slots = frame.get("slots", {}) or {}
if isinstance(slots, dict):
all_keys.update(slots.keys())
for key in all_keys:
value_counts = defaultdict(int)
for frame in cluster:
slots = frame.get("slots", {}) or {}
if isinstance(slots, dict) and key in slots:
value_counts[str(slots[key])] += 1
if not value_counts:
continue
best_value = max(value_counts, key=lambda v: (value_counts[v], [-ord(c) for c in v]))
final_slots[key] = best_value
return final_slots
def voting_key(cluster: List[Dict]):
value_key_counts = defaultdict(lambda: defaultdict(int))
value_support = defaultdict(set)
# 1) Collect (key, value) pairs and count support for each value
for frame_idx, frame in enumerate(cluster):
slots = frame.get("slots", {}) or {}
if not isinstance(slots, dict):
continue
for key, value in slots.items():
value = str(value)
value_key_counts[value][key] += 1
value_support[value].add(frame_idx)
# 2) Filter values by support
sup_threshold = math.ceil(len(cluster) / 2)
retained_values = {v for v, sup in value_support.items() if len(sup) >= sup_threshold}
# 3) Voting key for each retained value
final_slots = {}
for value in retained_values:
key_counts = value_key_counts[value]
best_key = max(key_counts, key=lambda k: (key_counts[k], [-ord(c) for c in k]))
final_slots[best_key] = value
return final_slots
def reintegrate_cluster(di_key: Tuple, cluster: List[Dict]) -> Dict:
domain, intent = di_key
final_slots = voting_key(cluster)
return {
"domain": domain,
"intent": intent,
"slots": final_slots
}
def SFL_MTSC(semantics, alpha=0.3, tau=0.55):
k = len(semantics)
if k == 0:
return []
if k == 1:
return semantics[0]
# 1) Collect Frame Pool
frame_pool = []
for path_idx, semantics_list in enumerate(semantics):
for frame in semantics_list:
if not isinstance(frame, dict):
logging.warning(f"Skipping non-dict frame at path {path_idx}: {type(frame)} = {frame}")
continue
if not frame.get("domain") or not frame.get("intent"):
continue
frame_with_path = dict(frame)
frame_with_path["_path_idx"] = path_idx
frame_pool.append(frame_with_path)
# 2) Clustering
## 2-1) Cluster by (domain, intent)
di_cluster = defaultdict(list)
for frame in frame_pool:
domain = frame.get("domain", "")
intent = frame.get("intent", "")
di_cluster[(domain, intent)].append(frame)
## 2-2) Cluster by slot Jaccard similarity
slot_clusters = {}
for di_key, frames in di_cluster.items():
slot_clusters[di_key] = cluster_hybrid_jaccard(
frames,
alpha=alpha,
tau=tau
)
# 4 + 5) Score & Filter
retained_slot_cluster = filter_slot_clusters(slot_clusters, k=k)
# 6) Re-integration
final_semantics = []
for di_key, clusters in retained_slot_cluster.items():
for cluster in clusters:
representative_frame = reintegrate_cluster(di_key, cluster)
representative_frame.pop("_path_idx", None)
final_semantics.append(representative_frame)
return final_semantics
def process_files(args: argparse.Namespace):
# 1) Prefare file paths
input_dir = Path(args.input_dir)
all_input_files = list(input_dir.glob("*.jsonl"))
if input_dir / "SFL-MTSC.jsonl" in all_input_files:
all_input_files.remove(input_dir / "SFL-MTSC.jsonl")
logging.info(f"Found {len(all_input_files)} JSONL files in the input directory.")
output_file = Path(args.output_file)
output_file.parent.mkdir(parents=True, exist_ok=True)
# 2) Integrate the reasoning paths results for each sample and perform self-consistency voting
sample_results = defaultdict(list)
id_to_query = defaultdict(str)
for input_file in tqdm(all_input_files, desc="Processing input files"):
with open(input_file, 'r', encoding='utf-8') as f:
for line in f:
data = json.loads(line)
sample_id = data.get("id", "")
query = data.get("query", "")
semantics = data.get("semantics", "")
# normalize semantics slot keys
if isinstance(semantics, list):
for frame in semantics:
if "slots" in frame and isinstance(frame["slots"], dict):
normalized_slots = {}
for k, v in frame["slots"].items():
norm_k = normalize_slot_key(k)
normalized_slots[norm_k] = v
frame["slots"] = normalized_slots
if sample_id != "":
sample_results[sample_id].append(semantics)
id_to_query[sample_id] = query
# 3) Perform SFL-MTSC and get final predictions for each sample
logging.info(f"Starting SFL-MTSC processing for {len(sample_results)} samples.")
with open(output_file, 'w', encoding='utf-8') as f_out:
for sample_id, semantics_list in tqdm(sample_results.items(), desc="Performing SFL-MTSC"):
final_semantics = SFL_MTSC(
semantics_list,
alpha=args.alpha,
tau=args.tau,
)
final_pred = {
"id": sample_id,
"query": id_to_query.get(sample_id, ""),
"semantics": final_semantics
}
f_out.write(json.dumps(final_pred, ensure_ascii=False) + "\n")
logging.info(f"SFL-MTSC processing completed. Final results saved to {output_file}.")
def main():
parser = setup_arg_parser()
args = parser.parse_args()
if not args.input_dir.endswith('/'):
args.input_dir += '/'
process_files(args)
if __name__ == "__main__":
main()