Spaces:
Sleeping
Sleeping
Download cleaner.py from OncomIA01/pythontest: direct link, hf CLI and curl.
- Browser
- Download file 1.59 kB
-
https://huggingface.co/spaces/OncomIA01/pythontest/resolve/main/cleaner.py
- Command line
-
hf download hf://spaces/OncomIA01/pythontest/cleaner.py
-
curl -L -o cleaner.py https://huggingface.co/spaces/OncomIA01/pythontest/resolve/main/cleaner.py
1.59 kB
| import json | |
| from difflib import SequenceMatcher | |
| def similar(a, b): | |
| return SequenceMatcher(None, a, b).ratio() | |
| def clean_duplicates(): | |
| print("🧹 Iniciando limpieza de preguntas...") | |
| try: | |
| with open("questions.json", "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| except FileNotFoundError: | |
| print("❌ No se encontró questions.json") | |
| return | |
| unique_questions = [] | |
| seen_questions = [] | |
| print(f"📊 Total inicial: {len(data)} preguntas.") | |
| for q in data: | |
| is_duplicate = False | |
| current_q_text = q['question'].strip() | |
| # 1. Comprobar duplicados exactos o muy similares | |
| for seen_q in seen_questions: | |
| similarity = similar(current_q_text, seen_q) | |
| if similarity > 0.85: # Si son más del 85% iguales | |
| is_duplicate = True | |
| print(f" 🗑️ Eliminada duplicada (ID {q['id']}): {current_q_text[:30]}...") | |
| break | |
| if not is_duplicate: | |
| unique_questions.append(q) | |
| seen_questions.append(current_q_text) | |
| # 2. Renumerar IDs | |
| print("\n🔄 Renumerando IDs...") | |
| for index, q in enumerate(unique_questions): | |
| q['id'] = index + 1 | |
| # 3. Guardar | |
| with open("questions.json", "w", encoding="utf-8") as f: | |
| json.dump(unique_questions, f, ensure_ascii=False, indent=2) | |
| print(f"✅ ¡Hecho! Total final: {len(unique_questions)} preguntas únicas.") | |
| if __name__ == "__main__": | |
| clean_duplicates() |