Skip to content

Commit

Permalink
refactors search_predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Mar 7, 2024
1 parent e0f79de commit cdddf9d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ez_wikidata/__init__.py
@@ -1 +1 @@
__version__ = "0.1.5"
__version__ = "0.2.0"
14 changes: 7 additions & 7 deletions ez_wikidata/trulytabular.py
Expand Up @@ -28,7 +28,7 @@ def __init__(
itemQid,
propertyLabels: list = [],
propertyIds: list = [],
subclassPredicate="wdt:P31",
search_predicate="wdt:P31",
where: str = None,
endpointConf=None,
lang="en",
Expand All @@ -41,7 +41,7 @@ def __init__(
itemQid(str): wikidata id of the type to analyze
propertyLabels(list): a list of labels of properties to be considered
propertyIds(list): a list of ids of properties to be considered
subclassPredicate(str): the subclass Predicate to use
search_predicate(str): the search predicate to use e.g. instanceof / subclass of
where(str): extra where clause for instance selection (if any)
endpoint(str): the url of the SPARQL endpoint to be used
"""
Expand All @@ -53,7 +53,7 @@ def __init__(
self.wpm=WikidataPropertyManager.get_instance(endpoint_url=endpointConf.endpoint,lang=lang)
self.sparql = SPARQL(endpointConf.endpoint, method=self.endpointConf.method)
self.sparql.debug = self.debug
self.subclassPredicate = subclassPredicate
self.search_predicate = search_predicate
self.where = f"\n {where}" if where is not None else ""
self.lang = lang
self.item = WikidataItem(
Expand Down Expand Up @@ -88,7 +88,7 @@ def count(self):
WHERE
{{
# instance of {self.item.qlabel}
?item {self.subclassPredicate} wd:{self.item.qid}.{self.where}
?item {self.search_predicate} wd:{self.item.qid}.{self.where}
}}"""
try:
count = self.sparql.getValue(query, "count")
Expand Down Expand Up @@ -196,7 +196,7 @@ def generateSparqlQuery(
sparqlQuery += f"""
WHERE {{
# instanceof {item.qid}:{item.qlabel}
?{item.itemVarname} {self.subclassPredicate} wd:{item.qid}.
?{item.itemVarname} {self.search_predicate} wd:{item.qid}.
# label
?{item.itemVarname} rdfs:label ?{item.labelVarname}.
FILTER (LANG(?{item.labelVarname}) = "{lang}").
Expand Down Expand Up @@ -254,7 +254,7 @@ def mostFrequentPropertiesQuery(self, whereClause: str = None, minCount: int = 0
whereClause(str): an extra WhereClause to use
"""
if whereClause is None:
whereClause = f"?item {self.subclassPredicate} wd:{self.itemQid}"
whereClause = f"?item {self.search_predicate} wd:{self.itemQid}"
if self.endpointConf.database != "qlever":
whereClause += ";?p ?id"
whereClause += "."
Expand Down Expand Up @@ -322,7 +322,7 @@ def noneTabularQuery(self, wdProperty: WikidataProperty, asFrequency: bool = Tru
WHERE
{{
# instance of {self.item.qlabel}
?item {self.subclassPredicate} wd:{self.itemQid}.{self.where}
?item {self.search_predicate} wd:{self.itemQid}.{self.where}
?item rdfs:label ?itemLabel.
FILTER (LANG(?itemLabel) = "{self.lang}").
# {propertyLabel}
Expand Down
2 changes: 1 addition & 1 deletion ez_wikidata/version.py
Expand Up @@ -17,7 +17,7 @@ class Version(object):
name = "py_ez_wikidata"
version = ez_wikidata.__version__
date = "2024-03-01"
updated = "2024-03-06"
updated = "2024-03-07"
description = "Mapping for Wikidata allows creation of wikidata entries from dicts"

authors = "Tim Holzheim, Wolfgang Fahl"
Expand Down
18 changes: 9 additions & 9 deletions tests/testTrulyTabular.py
Expand Up @@ -389,7 +389,7 @@ def testGenerateSparqlQuery(self):
{
"naive": True,
"qid": "Q2020153", # academic conference
"subclassPredicate": "wdt:P31",
"search_predicate": "wdt:P31",
"propertyIdMap": {
"P1813": ["label"],
"P17": ["label"],
Expand All @@ -400,7 +400,7 @@ def testGenerateSparqlQuery(self):
{
"naive": False,
"qid": "Q2020153", # academic conference
"subclassPredicate": "wdt:P31",
"search_predicate": "wdt:P31",
"propertyIdMap": {
"P1813": ["sample"],
"P17": ["sample"],
Expand All @@ -411,7 +411,7 @@ def testGenerateSparqlQuery(self):
{
"naive": False,
"qid": "Q2020153", # academic conference
"subclassPredicate": "wdt:P31",
"search_predicate": "wdt:P31",
"propertyIdMap": {
"P1813": ["count", "list"],
"P17": ["sample", "ignore"],
Expand All @@ -427,7 +427,7 @@ def testGenerateSparqlQuery(self):
{
"naive": False,
"qid": "Q1667921", # novel series
"subclassPredicate": "wdt:P31",
"search_predicate": "wdt:P31",
"propertyIdMap": {
"P50": ["sample", "ignore"], # author
"P136": ["sample", "ignore"], # genre
Expand All @@ -438,7 +438,7 @@ def testGenerateSparqlQuery(self):
{
"naive": False,
"qid": "Q1667921", # novel series
"subclassPredicate": "wdt:P31",
"search_predicate": "wdt:P31",
"propertyIdMap": {
"P50": ["sample", "ignore", "label"], # author
"P136": ["sample", "ignore", "label"], # genre
Expand All @@ -448,7 +448,7 @@ def testGenerateSparqlQuery(self):
},
{
"naive": False,
"subclassPredicate": "wdt:P279*/wdt:P31*",
"search_predicate": "wdt:P279*/wdt:P31*",
"qid": "Q8063", # rock
"propertyIdMap": {
"P18": ["sample"], # image
Expand All @@ -464,14 +464,14 @@ def testGenerateSparqlQuery(self):
qid = config["qid"]
naive = config["naive"]
propertyIdMap = config["propertyIdMap"]
subclassPredicate = config["subclassPredicate"]
search_predicate = config["search_predicate"]
expectedList = config["expected"]

# create a truly tabular analysis
tt = TrulyTabular(
qid,
propertyIds=list(propertyIdMap.keys()),
subclassPredicate=subclassPredicate,
search_predicate=search_predicate,
)
varname = tt.item.itemVarname
# generate a SPARQL Query
Expand All @@ -481,7 +481,7 @@ def testGenerateSparqlQuery(self):
pprint(config)
print(f"{sparqlQuery}")
# all queries should have basic graph patterns for the subclass
self.assertTrue(f"?{varname} {subclassPredicate} wd:{qid}." in sparqlQuery)
self.assertTrue(f"?{varname} {search_predicate} wd:{qid}." in sparqlQuery)
# and for the properties
for pid in propertyIdMap.keys():
self.assertTrue(f"?{varname} wdt:{pid}" in sparqlQuery)
Expand Down

0 comments on commit cdddf9d

Please sign in to comment.