File size: 1,606 Bytes
5c17e7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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."