Spaces:
Runtime error
Runtime error
File size: 6,345 Bytes
56ab964 | 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 | """
Metadata Manager for UPB Programs
Provides fast access to program information without retrieval
"""
import json
from pathlib import Path
from typing import List, Dict, Optional
class MetadataManager:
"""Manages program metadata for quick lookups and comprehensive queries"""
def __init__(self, metadata_path: Optional[str] = None):
if metadata_path is None:
metadata_path = Path(__file__).parent / "programs_index.json"
with open(metadata_path, 'r', encoding='utf-8') as f:
self.data = json.load(f)
self.programs = self.data['programs']
self.metadata = self.data['metadata']
def get_all_programs(self) -> List[Dict]:
"""Get list of all programs"""
return self.programs
def get_program_names(self) -> List[str]:
"""Get just the program names"""
return [p['name'] for p in self.programs]
def get_total_count(self) -> int:
"""Get total number of programs"""
return self.metadata['total_programs']
def get_abet_programs(self) -> List[str]:
"""Get programs with ABET accreditation"""
return self.metadata.get('abet_accredited', [])
def get_alta_calidad_programs(self) -> List[str]:
"""Get programs with Alta Calidad accreditation"""
return self.metadata.get('alta_calidad_accredited', [])
def search_by_keyword(self, keyword: str) -> List[Dict]:
"""Find programs matching a keyword"""
keyword_lower = keyword.lower()
matches = []
for program in self.programs:
# Check in name
if keyword_lower in program['name'].lower():
matches.append(program)
continue
# Check in keywords
for kw in program.get('keywords', []):
if keyword_lower in kw.lower():
matches.append(program)
break
return matches
def get_program_by_file(self, filename: str) -> Optional[Dict]:
"""Get program info by filename"""
for program in self.programs:
if program['file'] == filename:
return program
return None
def is_accreditation_query(self, query: str) -> bool:
"""Detect if query is about accreditations"""
accreditation_keywords = [
'acreditación', 'acreditacion', 'abet', 'alta calidad',
'acreditad', 'certificación', 'certificacion'
]
query_lower = query.lower()
return any(keyword in query_lower for keyword in accreditation_keywords)
def is_comprehensive_query(self, query: str) -> bool:
"""Detect if query is asking for all programs"""
query_lower = query.lower()
# First check if it's an accreditation query (higher priority)
accreditation_keywords = [
'acreditación', 'acreditacion', 'abet', 'alta calidad',
'acreditad', 'certificación', 'certificacion'
]
if any(keyword in query_lower for keyword in accreditation_keywords):
return False
comprehensive_keywords = [
'todas', 'todos', 'cuántas', 'cuantas', 'lista',
'listar', 'enumera', 'qué ingenierías', 'que ingenierias',
'qué programas', 'que programas', 'catálogo', 'catalogo'
]
return any(keyword in query_lower for keyword in comprehensive_keywords)
query_lower = query.lower()
return any(keyword in query_lower for keyword in accreditation_keywords)
def format_programs_list(self) -> str:
"""Format all programs as a numbered list"""
programs_list = "\n".join([
f"{i+1}. {program['name']}"
for i, program in enumerate(self.programs)
])
return f"La UPB ofrece {self.get_total_count()} programas de ingeniería:\n\n{programs_list}"
def format_abet_programs(self) -> str:
"""Format ABET accredited programs"""
programs = self.get_abet_programs()
if not programs:
return "No hay información sobre programas con acreditación ABET."
programs_list = "\n".join([f"- {p}" for p in programs])
return f"Programas con acreditación ABET:\n{programs_list}"
def format_alta_calidad_programs(self) -> str:
"""Format Alta Calidad accredited programs"""
programs = self.get_alta_calidad_programs()
if not programs:
return "No hay información sobre programas con acreditación de Alta Calidad."
programs_list = "\n".join([f"- {p}" for p in programs])
return f"Programas con acreditación de Alta Calidad:\n{programs_list}"
def test_metadata_manager():
"""Test the metadata manager"""
print("=" * 80)
print("Testing MetadataManager")
print("=" * 80)
manager = MetadataManager()
print(f"\n✓ Total programs: {manager.get_total_count()}")
print(f"\n✓ All program names:")
for name in manager.get_program_names():
print(f" - {name}")
print(f"\n✓ ABET accredited programs:")
for name in manager.get_abet_programs():
print(f" - {name}")
print(f"\n✓ Search 'sistemas':")
matches = manager.search_by_keyword('sistemas')
for match in matches:
print(f" - {match['name']}")
print(f"\n✓ Is comprehensive query?")
test_queries = [
"¿Cuántas ingenierías hay?",
"Lista todas las ingenierías",
"¿Qué es ingeniería de sistemas?"
]
for q in test_queries:
result = manager.is_comprehensive_query(q)
print(f" '{q}' -> {result}")
print(f"\n✓ Is accreditation query?")
test_queries = [
"¿Qué programas tienen ABET?",
"Acreditación de alta calidad",
"¿Cuánto dura la carrera?"
]
for q in test_queries:
result = manager.is_accreditation_query(q)
print(f" '{q}' -> {result}")
print(f"\n✓ Formatted programs list:")
print(manager.format_programs_list())
print(f"\n✓ Formatted ABET programs:")
print(manager.format_abet_programs())
if __name__ == "__main__":
test_metadata_manager() |