简体   繁体   English

有没有办法参数化 sparql 过滤条件?

[英]is there a way to parameterize a sparql filter condition?

I am trying to find out what is the best way to parameterize a filter condition with a float value taken as input.我正在尝试找出以浮点值作为输入来参数化过滤条件的最佳方法。

count=25.67
FILTER(?price < count)

instead of:代替:

FILTER ( ?price < 25.67 )

The value of "count" will be taken as input. “count”的值将作为输入。

I would like to know the syntax for including the object count in the FILTER command.我想知道在 FILTER 命令中包含 object 计数的语法。

Thank you in advance.先感谢您。

There is no magic SPARQL syntax to do this: like SQL, you have to make a SPARQL query out of a string, at least in Python.没有神奇的 SPARQL 语法可以做到这一点:就像 SQL 一样,您必须使用字符串进行 SPARQL 查询,至少在 Python 中是这样。 Therefore, any user input you want has to make the string that you then send as the query.因此,您想要的任何用户输入都必须将字符串作为查询发送。 Say you wanted to get all prices greater than price_to_be_less_than , you would do this in RDFlib Python:假设您想获得所有价格高于price_to_be_less_than ,您可以在RDFlib Python 中执行此操作:

def get_prices(g, price_to_be_less_than):
    q = """
        SELECT ?item 
        WHERE {{
            ?item ex:hasPrice ?price .

            FILTER (?price < {})
        }}
        """.format(price_to_be_less_than)

       items = []
       for r in g.query(q):
           items.append(r["item"])

       return items

I use .format() , not f-strings (personal taste), so I then have to use double braces, "{{", in the query我使用.format() ,而不是 f-strings (个人口味),所以我必须在查询中使用双括号“{{”

Here's an example of setQuery use with a parameter added via `.format():下面是一个使用setQuery的示例,其中使用了通过 `.format() 添加的参数:

from SPARQLWrapper import SPARQLWrapper, JSON


def query_dbpedia(my_uri)
    sparql = SPARQLWrapper("http://dbpedia.org/sparql")
    q = """
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

        SELECT ?label
        WHERE {{
            {} rdfs:label ?label .
        }}
    """.format(my_uri)
    sparql.setQuery(q)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()

    results_to_return = []
    for result in results["results"]["bindings"]:
        results_to_return.append(result["label"]["value"])

    return results_to_return

print(query_dbpedia("http://dbpedia.org/resource/Asturias"))

(adapted from SPARQLWrapper documentation at https://sparqlwrapper.readthedocs.io/en/latest/main.html ) (改编自 https ://sparqlwrapper.readthedocs.io/en/latest/main.html的 SPARQLWrapper 文档)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pytest:以 DRY 方式参数化夹具 - pytest: parameterize fixtures in a DRY way SQLAlchemy,有没有办法在关系条件上设置过滤器 - SQLAlchemy, is there any way to set filter on relationship condition 具有Unicode字符的SPARQL正则表达式过滤器 - SPARQL regex FILTER with unicode characters dict作为参数化python类的方法的替代方法是什么? - What is the alternative to dict as way to parameterize python class? Pythonic方法参数化一个函数(没有lambda) - Pythonic way to parameterize a function (without lambda) 有没有办法在条件中使用多种数据类型过滤 Python 中的列? - Is there a way to filter columns in Python using multiple data types in condition? 如何过滤 Dataframe 以使 Pyspark 中的某个条件返回 True 或 False? - How to filter a Dataframe in such a way to return a True or False to a certain condition in Pyspark? 在SPARQL过滤器中引用OWL类 - Refering OWL Class In SPARQL Filter Stament 有没有办法在同一个测试中参数化具有多种数据类型的 pytest? - Is there a way to parameterize a pytest with multiple data types in a same test? 在MySQL + Python中使用AND运算符对子字符串查询进行参数化的安全方法 - Safe way to parameterize substring query with AND operator in MySQL + Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM