Skip to content

Instantly share code, notes, and snippets.

@ettorerizza
Created December 28, 2017 13:49
Show Gist options
  • Save ettorerizza/112486eef962e72158f57bdddeda2aa0 to your computer and use it in GitHub Desktop.
Save ettorerizza/112486eef962e72158f57bdddeda2aa0 to your computer and use it in GitHub Desktop.
Get the least common subsumer (LCS) between two Wikidata items
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
array = ["Q32815", "Q34627"]
query = {"query": """
SELECT ?classe ?classeLabel WHERE {
wd:%s wdt:P279* ?classe .
wd:%s wdt:P279* ?classe .
filter not exists { ?otherClass wdt:P279 ?classe ; ^wdt:P279* wd:%s, wd:%s . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
""" % (array[0], array[1], array[0], array[1])
}
url = "https://query.wikidata.org/sparql"
r = requests.get(url, params=query)
print(r.text)
soup = BeautifulSoup(r.text, "lxml")
print([x.text for x in soup.find_all("literal")])
""" #OpeRefine version (do not forget to uncomment the sparql query)
import sys
sys.path.append(r'D:\jython2.7.0\Lib\site-packages')
import requests
from bs4 import BeautifulSoup
array = row['record']['cells']['class_id']['value']
query = {"query": """
# SELECT ?classe ?classeLabel WHERE {
# wd:%s wdt:P279* ?classe .
# wd:%s wdt:P279* ?classe .
# filter not exists { ?otherClass wdt:P279 ?classe ; ^wdt:P279* wd:%s, wd:%s . }
# SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
# }
""" % (array[0], array[1], array[0], array[1])
}
url = "https://query.wikidata.org/sparql"
r = requests.get(url, params=query)
print(r.text)
soup = BeautifulSoup(r.text)
return ",".join([x.text for x in soup.find_all("uri")])
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment