Upload analyze_audiodescriptions.py
Browse files
page_modules/analyze_audiodescriptions.py
CHANGED
|
@@ -97,83 +97,110 @@ def _file_for_hist_choice(vid_dir: Path, version: str, filename: str, hist_choic
|
|
| 97 |
|
| 98 |
|
| 99 |
def load_eval_values(vid_dir: Path, version: str, eval_content: Optional[str] = None) -> Optional[Dict[str, int]]:
|
| 100 |
-
"""Carga los valores de evaluaci贸n desde eval (DB o CSV) si existe.
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
"""
|
|
|
|
| 109 |
csv_path = vid_dir / version / "eval.csv"
|
| 110 |
|
| 111 |
try:
|
| 112 |
if eval_content is not None:
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
elif csv_path.exists():
|
| 115 |
-
f_obj = open(csv_path,
|
| 116 |
else:
|
| 117 |
return None
|
| 118 |
|
| 119 |
with f_obj as f:
|
| 120 |
-
reader = csv.
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
return None
|
| 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 |
-
val = int(float(row[name]))
|
| 166 |
-
values[key] = max(0, min(7, val))
|
| 167 |
-
break
|
| 168 |
-
except (ValueError, TypeError):
|
| 169 |
-
continue
|
| 170 |
-
|
| 171 |
-
# Si no se encontr贸 valor, usar 7 por defecto
|
| 172 |
if key not in values:
|
| 173 |
values[key] = 7
|
| 174 |
-
|
| 175 |
return values
|
| 176 |
-
|
| 177 |
except Exception:
|
| 178 |
# Si hay cualquier error, simplemente ignorar y devolver None
|
| 179 |
return None
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
def load_eval_values(vid_dir: Path, version: str, eval_content: Optional[str] = None) -> Optional[Dict[str, int]]:
|
| 100 |
+
"""Carga los valores de evaluaci贸n (0-7) desde eval (DB o CSV) si existe.
|
| 101 |
+
|
| 102 |
+
El formato esperado es un CSV con cabecera::
|
| 103 |
+
|
| 104 |
+
Caracteristica,Valoracio (0-7),Justificacio
|
| 105 |
+
Precisi贸 Descriptiva,5,"..."
|
| 106 |
+
Sincronitzaci贸 Temporal,6,"..."
|
| 107 |
+
...
|
| 108 |
+
|
| 109 |
+
Cada fila se mapea a una de las seis dimensiones internas:
|
| 110 |
+
transcripcio, identificacio, localitzacions, activitats, narracions, expressivitat.
|
| 111 |
"""
|
| 112 |
+
|
| 113 |
csv_path = vid_dir / version / "eval.csv"
|
| 114 |
|
| 115 |
try:
|
| 116 |
if eval_content is not None:
|
| 117 |
+
# El contenido de la BD puede venir envuelto en ```; lo limpiamos.
|
| 118 |
+
text = eval_content.strip()
|
| 119 |
+
if text.startswith("```"):
|
| 120 |
+
text = text.lstrip("`").lstrip()
|
| 121 |
+
if text.endswith("```"):
|
| 122 |
+
text = text.rstrip("`").rstrip()
|
| 123 |
+
text = text.replace("\r\n", "\n").replace("\r", "\n")
|
| 124 |
+
f_obj = io.StringIO(text)
|
| 125 |
elif csv_path.exists():
|
| 126 |
+
f_obj = open(csv_path, "r", encoding="utf-8")
|
| 127 |
else:
|
| 128 |
return None
|
| 129 |
|
| 130 |
with f_obj as f:
|
| 131 |
+
reader = csv.reader(f)
|
| 132 |
+
|
| 133 |
+
# Saltar cabecera si la primera fila contiene "Caracteristica"
|
| 134 |
+
first = None
|
| 135 |
+
for first in reader:
|
| 136 |
+
if first and any("caracteristica" in c.lower() for c in first):
|
| 137 |
+
# Es cabecera; pasamos a las filas de datos
|
| 138 |
+
break
|
| 139 |
+
else:
|
| 140 |
+
# No es cabecera; la tratamos como primera fila de datos
|
| 141 |
+
break
|
| 142 |
+
|
| 143 |
+
# Mapeo de texto de caracter铆stica -> clave interna
|
| 144 |
+
def map_feature(name: str) -> Optional[str]:
|
| 145 |
+
name_l = name.strip().lower()
|
| 146 |
+
if "precis" in name_l: # Precisi贸 Descriptiva
|
| 147 |
+
return "transcripcio"
|
| 148 |
+
if "sincronitz" in name_l: # Sincronitzaci贸 Temporal
|
| 149 |
+
return "identificacio"
|
| 150 |
+
if "claredat" in name_l or "concis" in name_l: # Claredat i Concisi贸
|
| 151 |
+
return "localitzacions"
|
| 152 |
+
if "di脿leg" in name_l or "di脿leg/so" in name_l or "di脿leg" in name_l:
|
| 153 |
+
return "activitats" # Inclusi贸 de Di脿leg/So
|
| 154 |
+
if "contextualitz" in name_l:
|
| 155 |
+
return "narracions"
|
| 156 |
+
if "flux" in name_l or "ritme" in name_l:
|
| 157 |
+
return "expressivitat"
|
| 158 |
return None
|
| 159 |
+
|
| 160 |
+
values: Dict[str, int] = {}
|
| 161 |
+
|
| 162 |
+
def process_row(row):
|
| 163 |
+
if not row or all(not c.strip() for c in row):
|
| 164 |
+
return
|
| 165 |
+
# Esperamos al menos dos columnas: Caracteristica, Valoracio (0-7)
|
| 166 |
+
if len(row) < 2:
|
| 167 |
+
return
|
| 168 |
+
feature = row[0].strip()
|
| 169 |
+
key = map_feature(feature)
|
| 170 |
+
if not key:
|
| 171 |
+
return
|
| 172 |
+
raw_val = row[1].strip().strip('"')
|
| 173 |
+
if not raw_val:
|
| 174 |
+
return
|
| 175 |
+
try:
|
| 176 |
+
v = int(float(raw_val))
|
| 177 |
+
except ValueError:
|
| 178 |
+
return
|
| 179 |
+
values[key] = max(0, min(7, v))
|
| 180 |
+
|
| 181 |
+
# Si la primera fila ya era de datos, procesarla
|
| 182 |
+
if first:
|
| 183 |
+
# Comprobar si era cabecera
|
| 184 |
+
if not any("caracteristica" in c.lower() for c in first):
|
| 185 |
+
process_row(first)
|
| 186 |
+
|
| 187 |
+
for row in reader:
|
| 188 |
+
process_row(row)
|
| 189 |
+
|
| 190 |
+
# Rellenar con 7 por defecto cualquier dimensi贸n que falte
|
| 191 |
+
for key in [
|
| 192 |
+
"transcripcio",
|
| 193 |
+
"identificacio",
|
| 194 |
+
"localitzacions",
|
| 195 |
+
"activitats",
|
| 196 |
+
"narracions",
|
| 197 |
+
"expressivitat",
|
| 198 |
+
]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
if key not in values:
|
| 200 |
values[key] = 7
|
| 201 |
+
|
| 202 |
return values
|
| 203 |
+
|
| 204 |
except Exception:
|
| 205 |
# Si hay cualquier error, simplemente ignorar y devolver None
|
| 206 |
return None
|