Spaces:
Running
Running
import os | |
assets_dir = './assets' | |
character = {} | |
# Step 1: Load character dictionary | |
for i in os.listdir(assets_dir): | |
if i.endswith('.wav'): | |
audio_path = f"{assets_dir}/{i}" | |
image_path= f"{assets_dir}/{i.replace('.wav', '.png')}" | |
sound_name = i.replace('.wav', '').replace('_', ' ').title() | |
character[sound_name] = { | |
'image_path': image_path, | |
'audio_path': audio_path | |
} | |
# Step 2: Define custom priority order | |
priority_names = [ | |
'Bombardiro Crocodilo', | |
'Tralalero Tralala', | |
'Bombombini Gusini', | |
'Tung Tung Tung Tung Tung Tung Tung Tung Tung Sahur', | |
'Brr Brr Patapim', | |
'Cappuccino Assassino', | |
'Lirili Larila', | |
'Trulimero Trulichina', | |
'Boneca Ambalabu', | |
'Chimpanzini Bananini' # Note: Fix spacing issue from original | |
] | |
# Normalize keys to fix any mismatch | |
fixed_character = {} | |
for name, data in character.items(): | |
fixed_name = ' '.join(name.split()) # Removes extra spaces | |
fixed_character[fixed_name] = data | |
# Step 3: Sort with custom priority | |
sorted_keys = ( | |
priority_names + | |
sorted([k for k in fixed_character if k not in priority_names]) | |
) | |
# Step 4: Create the final sorted dictionary | |
sorted_character = {k: fixed_character[k] for k in sorted_keys} | |
import json | |
json_path = 'character.json' | |
with open(json_path, 'w', encoding='utf-8') as f: | |
json.dump(sorted_character, f, ensure_ascii=False, indent=4) | |