-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_linking.py
30 lines (25 loc) · 1.09 KB
/
entity_linking.py
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
import requests
def get_wikipedia_link(entity, entity_type):
"""
Queries the Wikipedia API and retrieves the link for the given entity.
If no link is found, returns None.
"""
# Special handling for specific products like iPhone
if entity_type == "PRODUCT" and "iPhone" in entity:
return "https://en.wikipedia.org/wiki/IPhone"
# Special handling for Apple as an organization (avoid linking to the fruit)
if entity_type == "ORG" and entity.lower() == "apple":
return "https://en.wikipedia.org/wiki/Apple_Inc."
# Wikipedia API search query
url = f"https://en.wikipedia.org/w/api.php?action=opensearch&search={entity}&limit=3&format=json"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
if len(data[3]) > 0:
return data[3][0] # Return the first URL from search results
else:
return None
except requests.exceptions.RequestException as e:
print(f"Error occurred: {e}")
return None