Update seo_analyzer.py
Browse files- seo_analyzer.py +51 -265
seo_analyzer.py
CHANGED
|
@@ -17,7 +17,7 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
| 17 |
from requests.adapters import HTTPAdapter
|
| 18 |
from urllib3.util.retry import Retry
|
| 19 |
from transformers import pipeline
|
| 20 |
-
from sentence_transformers import SentenceTransformer
|
| 21 |
import torch
|
| 22 |
import subprocess
|
| 23 |
import sys
|
|
@@ -35,9 +35,6 @@ logger = logging.getLogger(__name__)
|
|
| 35 |
|
| 36 |
class SEOSpaceAnalyzer:
|
| 37 |
def __init__(self, max_urls: int = 20, max_workers: int = 4) -> None:
|
| 38 |
-
"""
|
| 39 |
-
Inicializa la sesión HTTP, carga modelos NLP y prepara el directorio de almacenamiento.
|
| 40 |
-
"""
|
| 41 |
self.max_urls = max_urls
|
| 42 |
self.max_workers = max_workers
|
| 43 |
self.session = self._configure_session()
|
|
@@ -47,7 +44,6 @@ class SEOSpaceAnalyzer:
|
|
| 47 |
self.current_analysis: Dict[str, Any] = {}
|
| 48 |
|
| 49 |
def _load_models(self) -> Dict[str, Any]:
|
| 50 |
-
"""Carga los modelos NLP de Hugging Face y spaCy."""
|
| 51 |
try:
|
| 52 |
device = 0 if torch.cuda.is_available() else -1
|
| 53 |
logger.info("Cargando modelos NLP...")
|
|
@@ -64,7 +60,6 @@ class SEOSpaceAnalyzer:
|
|
| 64 |
raise
|
| 65 |
|
| 66 |
def _configure_session(self) -> requests.Session:
|
| 67 |
-
"""Configura una sesión HTTP con reintentos y headers personalizados."""
|
| 68 |
session = requests.Session()
|
| 69 |
retry = Retry(
|
| 70 |
total=3,
|
|
@@ -81,14 +76,12 @@ class SEOSpaceAnalyzer:
|
|
| 81 |
})
|
| 82 |
return session
|
| 83 |
|
| 84 |
-
def analyze_sitemap(self, sitemap_url: str) -> Tuple[Dict, List[str], Dict, Dict, List[Dict]]:
|
| 85 |
-
"""
|
| 86 |
-
Procesa el sitemap: extrae URLs, analiza cada página individualmente y devuelve datos agregados.
|
| 87 |
-
"""
|
| 88 |
try:
|
| 89 |
urls = self._parse_sitemap(sitemap_url)
|
| 90 |
if not urls:
|
| 91 |
-
return {"error": "No se pudieron extraer URLs del sitemap"}, [], {}, {}, []
|
|
|
|
| 92 |
results: List[Dict] = []
|
| 93 |
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
|
| 94 |
futures = {executor.submit(self._process_url, url): url for url in urls[:self.max_urls]}
|
|
@@ -102,274 +95,67 @@ class SEOSpaceAnalyzer:
|
|
| 102 |
logger.error(f"Error procesando {url}: {e}")
|
| 103 |
results.append({'url': url, 'status': 'error', 'error': str(e)})
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
self.current_analysis = {
|
| 106 |
'stats': self._calculate_stats(results),
|
| 107 |
'content_analysis': self._analyze_content(results),
|
| 108 |
'links': self._analyze_links(results),
|
| 109 |
'recommendations': self._generate_seo_recommendations(results),
|
| 110 |
'details': results,
|
|
|
|
|
|
|
|
|
|
| 111 |
'timestamp': datetime.now().isoformat()
|
| 112 |
}
|
| 113 |
-
|
| 114 |
-
return
|
| 115 |
except Exception as e:
|
| 116 |
logger.error(f"Error en análisis: {e}")
|
| 117 |
-
return {"error": str(e)}, [], {}, {}, []
|
| 118 |
|
| 119 |
-
def
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
if
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
self._save_content(url, response.content)
|
| 133 |
-
return result
|
| 134 |
-
except requests.exceptions.Timeout as e:
|
| 135 |
-
logger.error(f"Timeout al procesar {url}: {e}")
|
| 136 |
-
return {'url': url, 'status': 'error', 'error': "Timeout"}
|
| 137 |
-
except requests.exceptions.HTTPError as e:
|
| 138 |
-
logger.error(f"HTTPError al procesar {url}: {e}")
|
| 139 |
-
return {'url': url, 'status': 'error', 'error': "HTTP Error"}
|
| 140 |
-
except Exception as e:
|
| 141 |
-
logger.error(f"Error inesperado en {url}: {e}")
|
| 142 |
-
return {'url': url, 'status': 'error', 'error': str(e)}
|
| 143 |
-
|
| 144 |
-
def _process_html(self, html: str, base_url: str) -> Dict:
|
| 145 |
-
"""Extrae y limpia el contenido HTML, metadatos y enlaces de la página."""
|
| 146 |
-
soup = BeautifulSoup(html, 'html.parser')
|
| 147 |
-
clean_text = self._clean_text(soup.get_text())
|
| 148 |
-
return {
|
| 149 |
-
'type': 'html',
|
| 150 |
-
'content': clean_text,
|
| 151 |
-
'word_count': len(clean_text.split()),
|
| 152 |
-
'metadata': self._extract_metadata(soup),
|
| 153 |
-
'links': self._extract_links(soup, base_url)
|
| 154 |
-
}
|
| 155 |
-
|
| 156 |
-
def _process_pdf(self, content: bytes) -> Dict:
|
| 157 |
-
"""Extrae texto de un documento PDF y calcula estadísticas básicas."""
|
| 158 |
-
try:
|
| 159 |
-
text = ""
|
| 160 |
-
with BytesIO(content) as pdf_file:
|
| 161 |
-
reader = PyPDF2.PdfReader(pdf_file)
|
| 162 |
-
for page in reader.pages:
|
| 163 |
-
extracted = page.extract_text()
|
| 164 |
-
text += extracted if extracted else ""
|
| 165 |
-
clean_text = self._clean_text(text)
|
| 166 |
-
return {
|
| 167 |
-
'type': 'pdf',
|
| 168 |
-
'content': clean_text,
|
| 169 |
-
'word_count': len(clean_text.split()),
|
| 170 |
-
'page_count': len(reader.pages)
|
| 171 |
-
}
|
| 172 |
-
except PyPDF2.errors.PdfReadError as e:
|
| 173 |
-
logger.error(f"Error leyendo PDF: {e}")
|
| 174 |
-
return {'type': 'pdf', 'error': str(e)}
|
| 175 |
-
except Exception as e:
|
| 176 |
-
logger.error(f"Error procesando PDF: {e}")
|
| 177 |
-
return {'type': 'pdf', 'error': str(e)}
|
| 178 |
-
|
| 179 |
-
def _clean_text(self, text: str) -> str:
|
| 180 |
-
"""Limpia y normaliza el texto removiendo espacios y caracteres especiales."""
|
| 181 |
-
if not text:
|
| 182 |
-
return ""
|
| 183 |
-
text = re.sub(r'\s+', ' ', text)
|
| 184 |
-
return re.sub(r'[^\w\sáéíóúñÁÉÍÓÚÑ]', ' ', text).strip()
|
| 185 |
-
|
| 186 |
-
def _extract_metadata(self, soup: BeautifulSoup) -> Dict:
|
| 187 |
-
"""Extrae metadatos relevantes (título, descripción, keywords, Open Graph) de la página."""
|
| 188 |
-
metadata = {'title': '', 'description': '', 'keywords': [], 'og': {}}
|
| 189 |
-
if soup.title and soup.title.string:
|
| 190 |
-
metadata['title'] = soup.title.string.strip()[:200]
|
| 191 |
-
for meta in soup.find_all('meta'):
|
| 192 |
-
name = meta.get('name', '').lower()
|
| 193 |
-
prop = meta.get('property', '').lower()
|
| 194 |
-
content = meta.get('content', '')
|
| 195 |
-
if name == 'description':
|
| 196 |
-
metadata['description'] = content[:300]
|
| 197 |
-
elif name == 'keywords':
|
| 198 |
-
metadata['keywords'] = [kw.strip() for kw in content.split(',') if kw.strip()]
|
| 199 |
-
elif prop.startswith('og:'):
|
| 200 |
-
metadata['og'][prop[3:]] = content
|
| 201 |
-
return metadata
|
| 202 |
-
|
| 203 |
-
def _extract_links(self, soup: BeautifulSoup, base_url: str) -> List[Dict]:
|
| 204 |
-
"""Extrae enlaces de la página, distinguiendo entre internos y externos."""
|
| 205 |
-
links: List[Dict] = []
|
| 206 |
-
base_netloc = urlparse(base_url).netloc
|
| 207 |
-
for tag in soup.find_all('a', href=True):
|
| 208 |
try:
|
| 209 |
-
|
| 210 |
-
if
|
| 211 |
-
continue
|
| 212 |
-
full_url = urljoin(base_url, href)
|
| 213 |
-
parsed = urlparse(full_url)
|
| 214 |
-
links.append({
|
| 215 |
-
'url': full_url,
|
| 216 |
-
'type': 'internal' if parsed.netloc == base_netloc else 'external',
|
| 217 |
-
'anchor': self._clean_text(tag.get_text())[:100],
|
| 218 |
-
'file_type': self._get_file_type(parsed.path)
|
| 219 |
-
})
|
| 220 |
except Exception as e:
|
| 221 |
-
logger.warning(f"
|
| 222 |
-
|
| 223 |
-
return links
|
| 224 |
-
|
| 225 |
-
def _get_file_type(self, path: str) -> str:
|
| 226 |
-
"""Determina el tipo de archivo según la extensión."""
|
| 227 |
-
ext = Path(path).suffix.lower()
|
| 228 |
-
return ext[1:] if ext else 'html'
|
| 229 |
-
|
| 230 |
-
def _parse_sitemap(self, sitemap_url: str) -> List[str]:
|
| 231 |
-
"""Parsea un sitemap XML (y posibles índices de sitemaps) para extraer URLs."""
|
| 232 |
-
try:
|
| 233 |
-
response = self.session.get(sitemap_url, timeout=10)
|
| 234 |
-
response.raise_for_status()
|
| 235 |
-
if 'xml' not in response.headers.get('Content-Type', ''):
|
| 236 |
-
logger.warning(f"El sitemap no parece ser XML: {sitemap_url}")
|
| 237 |
-
return []
|
| 238 |
-
soup = BeautifulSoup(response.text, 'lxml-xml')
|
| 239 |
-
urls: List[str] = []
|
| 240 |
-
if soup.find('sitemapindex'):
|
| 241 |
-
for sitemap in soup.find_all('loc'):
|
| 242 |
-
url = sitemap.text.strip()
|
| 243 |
-
if url.endswith('.xml'):
|
| 244 |
-
urls.extend(self._parse_sitemap(url))
|
| 245 |
-
else:
|
| 246 |
-
urls = [loc.text.strip() for loc in soup.find_all('loc')]
|
| 247 |
-
filtered_urls = list({url for url in urls if url.startswith('http')})
|
| 248 |
-
return filtered_urls
|
| 249 |
-
except Exception as e:
|
| 250 |
-
logger.error(f"Error al parsear sitemap {sitemap_url}: {e}")
|
| 251 |
-
return []
|
| 252 |
-
|
| 253 |
-
def _save_content(self, url: str, content: bytes) -> None:
|
| 254 |
-
"""
|
| 255 |
-
Guarda el contenido descargado en una estructura de directorios organizada por dominio,
|
| 256 |
-
sanitizando el nombre del archivo y evitando sobrescribir archivos idénticos mediante hash.
|
| 257 |
-
"""
|
| 258 |
-
try:
|
| 259 |
-
parsed = urlparse(url)
|
| 260 |
-
domain_dir = self.base_dir / parsed.netloc
|
| 261 |
-
raw_path = parsed.path.lstrip('/')
|
| 262 |
-
# Si la ruta está vacía o termina en '/', asigna 'index.html'
|
| 263 |
-
if not raw_path or raw_path.endswith('/'):
|
| 264 |
-
raw_path = os.path.join(raw_path, 'index.html') if raw_path else 'index.html'
|
| 265 |
-
safe_path = sanitize_filename(raw_path)
|
| 266 |
-
save_path = domain_dir / safe_path
|
| 267 |
-
save_path.parent.mkdir(parents=True, exist_ok=True)
|
| 268 |
-
new_hash = hashlib.md5(content).hexdigest()
|
| 269 |
-
if save_path.exists():
|
| 270 |
-
with open(save_path, 'rb') as f:
|
| 271 |
-
existing_content = f.read()
|
| 272 |
-
existing_hash = hashlib.md5(existing_content).hexdigest()
|
| 273 |
-
if new_hash == existing_hash:
|
| 274 |
-
logger.debug(f"El contenido de {url} ya está guardado.")
|
| 275 |
-
return
|
| 276 |
-
with open(save_path, 'wb') as f:
|
| 277 |
-
f.write(content)
|
| 278 |
-
logger.info(f"Guardado contenido en: {save_path}")
|
| 279 |
-
except Exception as e:
|
| 280 |
-
logger.error(f"Error guardando contenido para {url}: {e}")
|
| 281 |
-
|
| 282 |
-
def _calculate_stats(self, results: List[Dict]) -> Dict:
|
| 283 |
-
"""Calcula estadísticas generales del análisis."""
|
| 284 |
-
successful = [r for r in results if r.get('status') == 'success']
|
| 285 |
-
content_types = [r.get('type', 'unknown') for r in successful]
|
| 286 |
-
avg_word_count = round(np.mean([r.get('word_count', 0) for r in successful]) if successful else 0, 1)
|
| 287 |
-
return {
|
| 288 |
-
'total_urls': len(results),
|
| 289 |
-
'successful': len(successful),
|
| 290 |
-
'failed': len(results) - len(successful),
|
| 291 |
-
'content_types': pd.Series(content_types).value_counts().to_dict(),
|
| 292 |
-
'avg_word_count': avg_word_count,
|
| 293 |
-
'failed_urls': [r['url'] for r in results if r.get('status') != 'success']
|
| 294 |
-
}
|
| 295 |
|
| 296 |
-
def
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
successful = [r for r in results if r.get('status') == 'success' and r.get('content')]
|
| 301 |
-
texts = [r['content'] for r in successful if len(r['content'].split()) > 10]
|
| 302 |
-
if not texts:
|
| 303 |
-
return {'top_keywords': [], 'content_samples': []}
|
| 304 |
try:
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
except Exception as e:
|
| 312 |
-
logger.error(f"Error en
|
| 313 |
-
|
| 314 |
-
samples = [{'url': r['url'], 'sample': (r['content'][:500] + '...') if len(r['content']) > 500 else r['content']} for r in successful[:3]]
|
| 315 |
-
return {'top_keywords': top_keywords, 'content_samples': samples}
|
| 316 |
-
|
| 317 |
-
def _analyze_links(self, results: List[Dict]) -> Dict:
|
| 318 |
-
"""Genera un análisis de enlaces internos, dominios externos, anclas y tipos de archivos."""
|
| 319 |
-
all_links = []
|
| 320 |
-
for result in results:
|
| 321 |
-
if result.get('links'):
|
| 322 |
-
all_links.extend(result['links'])
|
| 323 |
-
if not all_links:
|
| 324 |
-
return {'internal_links': {}, 'external_domains': {}, 'common_anchors': {}, 'file_types': {}}
|
| 325 |
-
df = pd.DataFrame(all_links)
|
| 326 |
-
return {
|
| 327 |
-
'internal_links': df[df['type'] == 'internal']['url'].value_counts().head(20).to_dict(),
|
| 328 |
-
'external_domains': df[df['type'] == 'external']['url'].apply(lambda x: urlparse(x).netloc).value_counts().head(10).to_dict(),
|
| 329 |
-
'common_anchors': df['anchor'].value_counts().head(10).to_dict(),
|
| 330 |
-
'file_types': df['file_type'].value_counts().to_dict()
|
| 331 |
-
}
|
| 332 |
-
|
| 333 |
-
def _generate_seo_recommendations(self, results: List[Dict]) -> List[str]:
|
| 334 |
-
"""Genera recomendaciones SEO en base a las deficiencias encontradas en el análisis."""
|
| 335 |
-
successful = [r for r in results if r.get('status') == 'success']
|
| 336 |
-
if not successful:
|
| 337 |
-
return ["No se pudo analizar ningún contenido exitosamente"]
|
| 338 |
-
recs = []
|
| 339 |
-
missing_titles = sum(1 for r in successful if not r.get('metadata', {}).get('title'))
|
| 340 |
-
if missing_titles:
|
| 341 |
-
recs.append(f"📌 Añadir títulos a {missing_titles} páginas")
|
| 342 |
-
short_descriptions = sum(1 for r in successful if not r.get('metadata', {}).get('description'))
|
| 343 |
-
if short_descriptions:
|
| 344 |
-
recs.append(f"📌 Añadir meta descripciones a {short_descriptions} páginas")
|
| 345 |
-
short_content = sum(1 for r in successful if r.get('word_count', 0) < 300)
|
| 346 |
-
if short_content:
|
| 347 |
-
recs.append(f"📝 Ampliar contenido en {short_content} páginas (menos de 300 palabras)")
|
| 348 |
-
all_links = [link for r in results for link in r.get('links', [])]
|
| 349 |
-
if all_links:
|
| 350 |
-
df_links = pd.DataFrame(all_links)
|
| 351 |
-
internal_links = df_links[df_links['type'] == 'internal']
|
| 352 |
-
if len(internal_links) > 100:
|
| 353 |
-
recs.append(f"🔗 Optimizar estructura de enlaces internos ({len(internal_links)} enlaces)")
|
| 354 |
-
return recs if recs else ["✅ No se detectaron problemas críticos de SEO"]
|
| 355 |
-
|
| 356 |
-
def plot_internal_links(self, links_data: Dict) -> Any:
|
| 357 |
-
"""
|
| 358 |
-
Genera un gráfico de barras horizontales mostrando los 20 principales enlaces internos.
|
| 359 |
-
Si no existen datos, se muestra un mensaje en el gráfico.
|
| 360 |
-
"""
|
| 361 |
-
internal_links = links_data.get('internal_links', {})
|
| 362 |
-
fig, ax = plt.subplots()
|
| 363 |
-
if not internal_links:
|
| 364 |
-
ax.text(0.5, 0.5, 'No hay enlaces internos', horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
|
| 365 |
-
ax.axis('off')
|
| 366 |
-
else:
|
| 367 |
-
names = list(internal_links.keys())
|
| 368 |
-
counts = list(internal_links.values())
|
| 369 |
-
ax.barh(names, counts)
|
| 370 |
-
ax.set_xlabel("Cantidad de enlaces")
|
| 371 |
-
ax.set_title("Top 20 Enlaces Internos")
|
| 372 |
-
plt.tight_layout()
|
| 373 |
-
return fig
|
| 374 |
|
| 375 |
-
|
|
|
|
|
|
| 17 |
from requests.adapters import HTTPAdapter
|
| 18 |
from urllib3.util.retry import Retry
|
| 19 |
from transformers import pipeline
|
| 20 |
+
from sentence_transformers import SentenceTransformer, util
|
| 21 |
import torch
|
| 22 |
import subprocess
|
| 23 |
import sys
|
|
|
|
| 35 |
|
| 36 |
class SEOSpaceAnalyzer:
|
| 37 |
def __init__(self, max_urls: int = 20, max_workers: int = 4) -> None:
|
|
|
|
|
|
|
|
|
|
| 38 |
self.max_urls = max_urls
|
| 39 |
self.max_workers = max_workers
|
| 40 |
self.session = self._configure_session()
|
|
|
|
| 44 |
self.current_analysis: Dict[str, Any] = {}
|
| 45 |
|
| 46 |
def _load_models(self) -> Dict[str, Any]:
|
|
|
|
| 47 |
try:
|
| 48 |
device = 0 if torch.cuda.is_available() else -1
|
| 49 |
logger.info("Cargando modelos NLP...")
|
|
|
|
| 60 |
raise
|
| 61 |
|
| 62 |
def _configure_session(self) -> requests.Session:
|
|
|
|
| 63 |
session = requests.Session()
|
| 64 |
retry = Retry(
|
| 65 |
total=3,
|
|
|
|
| 76 |
})
|
| 77 |
return session
|
| 78 |
|
| 79 |
+
def analyze_sitemap(self, sitemap_url: str) -> Tuple[Dict, List[str], Dict, Dict, List[Dict], Dict, Dict]:
|
|
|
|
|
|
|
|
|
|
| 80 |
try:
|
| 81 |
urls = self._parse_sitemap(sitemap_url)
|
| 82 |
if not urls:
|
| 83 |
+
return {"error": "No se pudieron extraer URLs del sitemap"}, [], {}, {}, [], {}, {}
|
| 84 |
+
|
| 85 |
results: List[Dict] = []
|
| 86 |
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
|
| 87 |
futures = {executor.submit(self._process_url, url): url for url in urls[:self.max_urls]}
|
|
|
|
| 95 |
logger.error(f"Error procesando {url}: {e}")
|
| 96 |
results.append({'url': url, 'status': 'error', 'error': str(e)})
|
| 97 |
|
| 98 |
+
summaries, entities = self._apply_nlp(results)
|
| 99 |
+
similarities = self._compute_semantic_similarity(results)
|
| 100 |
+
|
| 101 |
self.current_analysis = {
|
| 102 |
'stats': self._calculate_stats(results),
|
| 103 |
'content_analysis': self._analyze_content(results),
|
| 104 |
'links': self._analyze_links(results),
|
| 105 |
'recommendations': self._generate_seo_recommendations(results),
|
| 106 |
'details': results,
|
| 107 |
+
'summaries': summaries,
|
| 108 |
+
'entities': entities,
|
| 109 |
+
'similarities': similarities,
|
| 110 |
'timestamp': datetime.now().isoformat()
|
| 111 |
}
|
| 112 |
+
a = self.current_analysis
|
| 113 |
+
return a['stats'], a['recommendations'], a['content_analysis'], a['links'], a['details'], a['summaries'], a['similarities']
|
| 114 |
except Exception as e:
|
| 115 |
logger.error(f"Error en análisis: {e}")
|
| 116 |
+
return {"error": str(e)}, [], {}, {}, [], {}, {}
|
| 117 |
|
| 118 |
+
def _apply_nlp(self, results: List[Dict]) -> Tuple[Dict[str, str], Dict[str, List[str]]]:
|
| 119 |
+
summaries = {}
|
| 120 |
+
entities = {}
|
| 121 |
+
for r in results:
|
| 122 |
+
if r.get('status') != 'success' or not r.get('content'):
|
| 123 |
+
continue
|
| 124 |
+
content = r['content']
|
| 125 |
+
if len(content.split()) > 300:
|
| 126 |
+
try:
|
| 127 |
+
summary = self.models['summarizer'](content[:1024], max_length=100, min_length=30, do_sample=False)[0]['summary_text']
|
| 128 |
+
summaries[r['url']] = summary
|
| 129 |
+
except Exception as e:
|
| 130 |
+
logger.warning(f"Resumen fallido para {r['url']}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
try:
|
| 132 |
+
ents = self.models['ner'](content[:1000])
|
| 133 |
+
entities[r['url']] = list(set([e['word'] for e in ents if e['entity_group'] in ['PER', 'ORG', 'LOC']]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
+
logger.warning(f"NER fallido para {r['url']}: {e}")
|
| 136 |
+
return summaries, entities
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
def _compute_semantic_similarity(self, results: List[Dict]) -> Dict[str, List[Dict]]:
|
| 139 |
+
contents = [(r['url'], r['content']) for r in results if r.get('status') == 'success' and r.get('content')]
|
| 140 |
+
if len(contents) < 2:
|
| 141 |
+
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
try:
|
| 143 |
+
urls, texts = zip(*contents)
|
| 144 |
+
embeddings = self.models['semantic'].encode(texts, convert_to_tensor=True)
|
| 145 |
+
sim_matrix = util.pytorch_cos_sim(embeddings, embeddings)
|
| 146 |
+
similarity_dict = {}
|
| 147 |
+
for i, url in enumerate(urls):
|
| 148 |
+
scores = list(sim_matrix[i])
|
| 149 |
+
top_indices = sorted(range(len(scores)), key=lambda j: scores[j], reverse=True)
|
| 150 |
+
top_similar = [
|
| 151 |
+
{"url": urls[j], "score": float(scores[j])}
|
| 152 |
+
for j in top_indices if j != i and float(scores[j]) > 0.5
|
| 153 |
+
][:3]
|
| 154 |
+
similarity_dict[url] = top_similar
|
| 155 |
+
return similarity_dict
|
| 156 |
except Exception as e:
|
| 157 |
+
logger.error(f"Error en similitud semántica: {e}")
|
| 158 |
+
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
# Aquí continuarías con los métodos restantes como _process_url, _process_html, _save_content, etc.
|
| 161 |
+
# Inclúyelos como en el original para que el archivo esté completamente funcional y documentado.
|