Spaces:
Configuration error
Configuration error
import requests | |
from bs4 import BeautifulSoup | |
def structure_score(entity: str) -> int: | |
""" | |
If 'entity' is a URL: | |
β’ JSON-LD <script type="application/ld+json"> present β +40 | |
β’ H1 heading contains entity β +30 | |
β’ <meta name="description"> contains entity β +20 | |
β’ First <p> starts with entity β +10 | |
Cap at 100. | |
Else, return default 60. | |
""" | |
if not entity.startswith("http"): | |
return 60 | |
score = 0 | |
try: | |
resp = requests.get(entity, timeout=5) | |
soup = BeautifulSoup(resp.text, "html.parser") | |
# 1. JSON-LD detection | |
if soup.find("script", {"type": "application/ld+json"}): | |
score += 40 | |
# 2. H1 check | |
h1 = soup.find("h1") | |
if h1 and entity.lower() in h1.text.lower(): | |
score += 30 | |
# 3. Meta description check | |
meta = soup.find("meta", {"name": "description"}) | |
if meta and entity.lower() in meta.get("content", "").lower(): | |
score += 20 | |
# 4. First paragraph starts with entity | |
p = soup.find("p") | |
if p and p.text.strip().lower().startswith(entity.lower()): | |
score += 10 | |
return min(score, 100) | |
except Exception: | |
return 50 | |
def structure_recommendation(entity: str, score: int) -> str: | |
if score < 50: | |
return ( | |
"Add JSON-LD Person schema, ensure your H1 includes the entity, " | |
"and place a definition-style first sentence at the top of your homepage." | |
) | |
return "Structure is strong; maintain JSON-LD and clear headings." | |