| import json |
| from collections import Counter |
|
|
| print('Loading SPARQL updates...') |
| with open('data/external/templama_2023_2026_updates_clean.json') as f: |
| updates = json.load(f)['updates'] |
| print(f' {len(updates)} changed entities') |
|
|
| print('Loading Dynamic-TempLAMA templates...') |
| dyn = {} |
| with open('data/external/temporal-robustness/data/dynamic-templama/dataset_from_2019-1-1_to_2022-12-31_per_quarter/test.jsonl') as f: |
| for line in f: |
| if not line.strip(): continue |
| d = json.loads(line) |
| eid = d['id'].split('_')[0] |
| key = eid + '_' + d['relation'] |
| if key not in dyn: |
| dyn[key] = {'template': d['query'], 'answers': {}} |
| ans = d['answer'] |
| if isinstance(ans, dict): name = ans.get('name', '') |
| elif isinstance(ans, list) and ans: name = ans[0].get('name', '') |
| else: name = str(ans) |
| dyn[key]['answers'][d['date']] = name |
| print(f' {len(dyn)} entity-relation pairs') |
|
|
| YEARS = [2023, 2024, 2025, 2026] |
| CUTOFFS = {'llama2': '2022-09-01', 'mistral': '2023-12-01', 'llama31': '2023-12-01', 'qwen25': '2023-12-31', 'gemma2': '2024-06-01'} |
|
|
| print('Generating samples...') |
| samples = [] |
| sid = 0 |
| skipped = 0 |
|
|
| for uid, u in updates.items(): |
| dd = u.get('drift_date', '')[:10] |
| try: dy = int(dd[:4]) |
| except: skipped += 1; continue |
| for year in YEARS: |
| if year >= dy: exp, drifted, model_ans = u['new_answer'], True, u['old_answer'] |
| else: exp, drifted, model_ans = u['old_answer'], False, '' |
| tz = 'pre_cutoff' if year < 2023 else ('near_cutoff' if year == 2023 else 'post_cutoff') |
| query = 'In ' + str(year) + ', ' + u['query_template'].replace('_X_', '___') |
| s = {'sample_id': 'sparql_' + str(sid).zfill(6), 'query': query, 'expected_answer': exp, 'year': year, 'temporal_zone': tz, 'is_drifted_query': drifted, 'model_likely_answer': model_ans, 'language': 'en', 'entity': u['entity_id'], 'relation': u['relation_name'], 'knowledge_type': 'entity_role', 'category': 'unknown_drift' if drifted else 'known_drift', 'source': 'sparql_extension', 'parent_id': u['entity_id'], 'drift_date': u.get('drift_date', '')} |
| for m, c in CUTOFFS.items(): s['is_drifted_' + m] = (dd > c) and (year >= dy) |
| samples.append(s) |
| sid += 1 |
|
|
| with open('data/external/sparql_extension_samples.json', 'w') as f: |
| json.dump({'metadata': {'total': len(samples), 'skipped': skipped}, 'samples': samples}, f, indent=2, ensure_ascii=False) |
|
|
| print(f'Total: {len(samples)} samples, Skipped: {skipped}') |
| for label, fn in [('Categories', 'category'), ('Relations', 'relation')]: |
| print(f'{label}:') |
| for k, n in Counter(s[fn] for s in samples).most_common(): print(f' {k}: {n}') |
| print('Years:') |
| for y, n in sorted(Counter(s['year'] for s in samples).items()): print(f' {y}: {n}') |
| print('Per-model unknown_drift:') |
| for m in CUTOFFS: print(f' {m}: {sum(1 for s in samples if s.get("is_drifted_" + m, False))}') |
| print(f'Differential: {sum(1 for s in samples if len(set(s.get("is_drifted_" + m, False) for m in CUTOFFS)) > 1)}') |
| print('Saved to: data/external/sparql_extension_samples.json') |
|
|