prince1604 commited on
Commit ·
4ec2569
1
Parent(s): c1d121b
Enhanced alt text analysis: added detection for short, sparse, generic, and filename-like alt text
Browse files- debug_crawler.py +9 -0
- src/analyzer.py +52 -12
debug_crawler.py
CHANGED
|
@@ -1,10 +1,14 @@
|
|
| 1 |
from src.crawler import Crawler
|
|
|
|
| 2 |
import logging
|
|
|
|
| 3 |
|
| 4 |
# Configure logging to see what's happening
|
| 5 |
logging.basicConfig(level=logging.INFO)
|
| 6 |
|
| 7 |
c = Crawler()
|
|
|
|
|
|
|
| 8 |
domain = "https://eminentcoders.com/"
|
| 9 |
print(f"DEBUG: Starting crawl for {domain}")
|
| 10 |
|
|
@@ -13,3 +17,8 @@ site_data, discovered, reason = c.crawl_domain(domain, max_pages=1)
|
|
| 13 |
print(f"DEBUG: Result - Pages Scanned: {len(site_data)}")
|
| 14 |
print(f"DEBUG: Discovered: {discovered}")
|
| 15 |
print(f"DEBUG: Blocked Reason: {reason}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from src.crawler import Crawler
|
| 2 |
+
from src.analyzer import ImageAnalyzer
|
| 3 |
import logging
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
# Configure logging to see what's happening
|
| 7 |
logging.basicConfig(level=logging.INFO)
|
| 8 |
|
| 9 |
c = Crawler()
|
| 10 |
+
analyzer = ImageAnalyzer()
|
| 11 |
+
|
| 12 |
domain = "https://eminentcoders.com/"
|
| 13 |
print(f"DEBUG: Starting crawl for {domain}")
|
| 14 |
|
|
|
|
| 17 |
print(f"DEBUG: Result - Pages Scanned: {len(site_data)}")
|
| 18 |
print(f"DEBUG: Discovered: {discovered}")
|
| 19 |
print(f"DEBUG: Blocked Reason: {reason}")
|
| 20 |
+
|
| 21 |
+
report = analyzer.analyze_site(site_data)
|
| 22 |
+
print(json.dumps(report, indent=2))
|
| 23 |
+
|
| 24 |
+
|
src/analyzer.py
CHANGED
|
@@ -11,32 +11,72 @@ class ImageAnalyzer:
|
|
| 11 |
total_short_alt = 0
|
| 12 |
pages_report = []
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
for page_url, images in site_data.items():
|
| 15 |
missing_images = []
|
| 16 |
-
|
|
|
|
| 17 |
for img in images:
|
| 18 |
total_images += 1
|
| 19 |
alt_text = img.get('alt')
|
| 20 |
|
| 21 |
-
#
|
| 22 |
if not alt_text or alt_text.strip() == "":
|
| 23 |
-
# Double check if 'alt' attribute existed but was empty vs didn't exist?
|
| 24 |
-
# The crawler uses .get('alt', '') so it's '' if missing.
|
| 25 |
-
# User asked for "missing alt text".
|
| 26 |
-
# We will count empty string as missing alt text for SEO purposes.
|
| 27 |
missing_images.append(img['src'])
|
| 28 |
total_missing_alt += 1
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
pages_report.append({
|
| 35 |
"page_url": page_url,
|
| 36 |
"missing_alt_count": len(missing_images),
|
| 37 |
-
"
|
|
|
|
| 38 |
"images_without_alt": list(set(missing_images)),
|
| 39 |
-
"
|
|
|
|
| 40 |
})
|
| 41 |
|
| 42 |
return {
|
|
|
|
| 11 |
total_short_alt = 0
|
| 12 |
pages_report = []
|
| 13 |
|
| 14 |
+
# Compile regex for filename patterns (e.g., DSC_001, img-123)
|
| 15 |
+
import re
|
| 16 |
+
filename_pattern = re.compile(r'^(dsc|img|pic|screenshot)[\-_]?\d+', re.IGNORECASE)
|
| 17 |
+
|
| 18 |
for page_url, images in site_data.items():
|
| 19 |
missing_images = []
|
| 20 |
+
poor_quality_images = []
|
| 21 |
+
|
| 22 |
for img in images:
|
| 23 |
total_images += 1
|
| 24 |
alt_text = img.get('alt')
|
| 25 |
|
| 26 |
+
# 1. Strictly Missing
|
| 27 |
if not alt_text or alt_text.strip() == "":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
missing_images.append(img['src'])
|
| 29 |
total_missing_alt += 1
|
| 30 |
+
continue # Skip further checks if missing
|
| 31 |
+
|
| 32 |
+
cleaned_alt = alt_text.strip()
|
| 33 |
+
lower_alt = cleaned_alt.lower()
|
| 34 |
+
word_count = len(cleaned_alt.split())
|
| 35 |
+
|
| 36 |
+
is_poor = False
|
| 37 |
+
reason = ""
|
| 38 |
+
|
| 39 |
+
# 2. Advanced Poor Quality Checks
|
| 40 |
+
|
| 41 |
+
# A) Too Short / Sparse
|
| 42 |
+
if len(cleaned_alt) < 5:
|
| 43 |
+
is_poor = True
|
| 44 |
+
reason = "Too short (< 5 chars)"
|
| 45 |
+
elif word_count < 2:
|
| 46 |
+
# Single word check - allow exemptions?
|
| 47 |
+
# For now per requirements: word count < 2 is poor
|
| 48 |
+
is_poor = True
|
| 49 |
+
reason = "Too few words (needs >= 2)"
|
| 50 |
+
|
| 51 |
+
# B) Generic / Filler Text (Exact match check)
|
| 52 |
+
# We check if the *entire* text is just a placeholder word
|
| 53 |
+
generic_terms = {'image', 'photo', 'picture', 'logo', 'banner', 'icon', 'thumbnail', 'placeholder', 'img', 'spacer'}
|
| 54 |
+
if not is_poor and lower_alt in generic_terms:
|
| 55 |
+
is_poor = True
|
| 56 |
+
reason = "Generic filler text"
|
| 57 |
+
|
| 58 |
+
# D) File Name / Extension Check
|
| 59 |
+
if not is_poor:
|
| 60 |
+
if any(lower_alt.endswith(ext) for ext in ['.jpg', '.png', '.jpeg', '.webp', '.gif', '.svg']):
|
| 61 |
+
is_poor = True
|
| 62 |
+
reason = "Filename extension detected"
|
| 63 |
+
elif filename_pattern.search(cleaned_alt):
|
| 64 |
+
is_poor = True
|
| 65 |
+
reason = "Filename pattern detected"
|
| 66 |
+
|
| 67 |
+
if is_poor:
|
| 68 |
+
# Store with reason for better reporting if needed later, or just src/alt
|
| 69 |
+
poor_quality_images.append({'src': img['src'], 'alt': alt_text, 'reason': reason})
|
| 70 |
+
total_short_alt += 1 # We keep the metric name 'total_short_alt' for backward compatibility but it logic is advanced
|
| 71 |
|
| 72 |
pages_report.append({
|
| 73 |
"page_url": page_url,
|
| 74 |
"missing_alt_count": len(missing_images),
|
| 75 |
+
"poor_quality_count": len(poor_quality_images), # Renamed for clarity in report (client might need updates if they rely on 'short_alt_count')
|
| 76 |
+
"short_alt_count": len(poor_quality_images), # Kept for backward compatibility
|
| 77 |
"images_without_alt": list(set(missing_images)),
|
| 78 |
+
"images_with_poor_alt": poor_quality_images, # New detailed field
|
| 79 |
+
"images_with_short_alt": poor_quality_images # Kept for backward compatibility
|
| 80 |
})
|
| 81 |
|
| 82 |
return {
|