Spaces:
Configuration error
Configuration error
Create scoring/structure.py
Browse files- scoring/structure.py +51 -0
scoring/structure.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
|
| 4 |
+
def structure_score(entity: str) -> int:
|
| 5 |
+
"""
|
| 6 |
+
If 'entity' is a URL:
|
| 7 |
+
• JSON-LD <script type="application/ld+json"> present → +40
|
| 8 |
+
• H1 heading contains entity → +30
|
| 9 |
+
• <meta name="description"> contains entity → +20
|
| 10 |
+
• First <p> starts with entity → +10
|
| 11 |
+
Cap at 100.
|
| 12 |
+
Else, return default 60.
|
| 13 |
+
"""
|
| 14 |
+
if not entity.startswith("http"):
|
| 15 |
+
return 60
|
| 16 |
+
|
| 17 |
+
score = 0
|
| 18 |
+
try:
|
| 19 |
+
resp = requests.get(entity, timeout=5)
|
| 20 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
| 21 |
+
|
| 22 |
+
# 1. JSON-LD detection
|
| 23 |
+
if soup.find("script", {"type": "application/ld+json"}):
|
| 24 |
+
score += 40
|
| 25 |
+
|
| 26 |
+
# 2. H1 check
|
| 27 |
+
h1 = soup.find("h1")
|
| 28 |
+
if h1 and entity.lower() in h1.text.lower():
|
| 29 |
+
score += 30
|
| 30 |
+
|
| 31 |
+
# 3. Meta description check
|
| 32 |
+
meta = soup.find("meta", {"name": "description"})
|
| 33 |
+
if meta and entity.lower() in meta.get("content", "").lower():
|
| 34 |
+
score += 20
|
| 35 |
+
|
| 36 |
+
# 4. First paragraph starts with entity
|
| 37 |
+
p = soup.find("p")
|
| 38 |
+
if p and p.text.strip().lower().startswith(entity.lower()):
|
| 39 |
+
score += 10
|
| 40 |
+
|
| 41 |
+
return min(score, 100)
|
| 42 |
+
except Exception:
|
| 43 |
+
return 50
|
| 44 |
+
|
| 45 |
+
def structure_recommendation(entity: str, score: int) -> str:
|
| 46 |
+
if score < 50:
|
| 47 |
+
return (
|
| 48 |
+
"Add JSON-LD Person schema, ensure your H1 includes the entity, "
|
| 49 |
+
"and place a definition-style first sentence at the top of your homepage."
|
| 50 |
+
)
|
| 51 |
+
return "Structure is strong; maintain JSON-LD and clear headings."
|