File size: 1,134 Bytes
4ec3826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests

def corpus_score(entity: str) -> int:
    """
    1. Attempt to fetch a Wikipedia page:
       URL = https://en.wikipedia.org/wiki/{entity.replace(' ', '_')}
       If status_code == 200, count occurrences of 'entity' on that page.
       Score = min(hits * 10, 100).

    2. Otherwise (404 or exception), fallback heuristic:
       score = min(len(entity) * 2, 100).
    """
    wiki_url = f"https://en.wikipedia.org/wiki/{entity.replace(' ', '_')}"
    try:
        resp = requests.get(wiki_url, timeout=5)
        if resp.status_code == 200:
            text = resp.text.lower()
            hits = text.count(entity.lower())
            return min(hits * 10, 100)
    except Exception:
        pass

    return min(len(entity) * 2, 100)

def corpus_recommendation(entity: str, score: int) -> str:
    if score < 50:
        return (
            "No dedicated Wikipedia page found or low mention count. "
            "Consider creating a Wikipedia page or getting mentions on authoritative .edu/.gov sites."
        )
    return "Corpus presence is good. Consider maintaining and expanding authoritative mentions."