4

I need to select the entity that have a "taxon rank (P105)" of "species (Q7432)" which have a label that match a literal string such as "Topinambur".

I'm testing the queries on https://query.wikidata.org; this query goes fine and return the entity to me with satisfying response time:

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE {
  ?entity rdfs:label "Topinambur"@de . 
  ?entity wdt:P105 wd:Q7432.
}
LIMIT 100 

The problem here is that my requisite is not to specify the language but the lexical forms of the labels in the underlying dataset ( wikidata) has language tags so i need a way to get Literal Equality for any language.

I tried some possible solution but I didn't find any query that didn't result in the following: TIMEOUT message com.bigdata.bop.engine.QueryTimeoutException: Query deadline is expired

Here the list of what I tried (..and I always get TIMEOUT) :

1) based on this answer I tried:

SELECT * WHERE {
  ?entity rdfs:label ?label FILTER ( str( ?label ) = "Topinambur") . 
  ?entity wdt:P105 wd:Q7432.
}
LIMIT 100

2) based on some other documentation I tried:

SELECT * WHERE {
  ?entity wdt:P105 wd:Q7432.
  ?entity rdfs:label ?label FILTER regex(?label, "^Topinambur")  .  
}
LIMIT 100

3) and

   SELECT * WHERE {
      ?entity wdt:P105 wd:Q7432.
      ?entity rdfs:label ?label .
      FILTER langMatches( lang(?label), "*" )
      FILTER (?label = "Topinambur")
   }
   LIMIT 100

What I'm looking for is a performant solution or some SPARQL syntax the doesn't end up to a TIMEOUT message.

PS: with reference to http://www.rfc-editor.org/rfc/bcp/bcp47.txt I don't understand if language ranges or ```wildcards`` could help in some way.

EDIT

I successfully tested (without falling timeout) a similar query in DbPedia by using virtuoso query editor at: https://dbpedia.org/sparql Default Data Set Name (Graph IRI):http://dbpedia.org

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/> 

SELECT ?resource 
WHERE { 
 ?resource rdfs:label ?label .  FILTER ( str( ?label ) = "Topinambur").
 ?resource rdf:type dbo:Species
 }
LIMIT 100

I am still very interested in understanding the performance problem that I experience on Wikidata and what is the best syntax to use.

7
  • What is giving the timeout? The triple store? Which triple store do you use? Is it Blazegraph or Virtuoso? Oct 25, 2016 at 18:44
  • @AKSW I'm using query.wikidata.org ( just edited my question thanks to your comment ) I will try asap the same on dbpedia virtuoso dbpedia.org/sparql but I don't know now if it's possible because of I don't know if dbpedia has the same data Oct 25, 2016 at 19:05
  • @FrancoRondini, have a look at this answer. It seems something like ?entity wdt:P171+ wd:Q25314 . will be a suitable condition in your case. Jul 18, 2017 at 8:57
  • 1
    @Stanislav Kralin you are correct! I try a couple of query resulting in 8 results in 51294ms and and 2 result in 41284ms (with DISTINCT clause) - both satisfy my requirement although not so fast. If you would to post your answer I will accept it. Thanks for the suggestions. Jul 25, 2017 at 1:14
  • query.wikidata.org/… Jul 25, 2017 at 1:15

3 Answers 3

3

I solved similar problem - want to find entity with label string in any language. I recommend do not use FILTER, because it is too slow. Rather use UNION like this:

SELECT ?entity WHERE {
  ?entity wdt:P105 wd:Q7432.
  { ?entity rdfs:label "Topinambur"@de . }
  UNION { ?entity rdfs:label "Topinambur"@en . }
  UNION { ?entity rdfs:label "Topinambur"@fr . }
}
GROUP BY ?entity
LIMIT 100 

Try it!

This solution is not perfect, because you have to enumater all languages, but is fast and reliable. List of all available wikidata language are here.

2

This answer proposes three options:

  1. Be more specific.
    The ?entity wdt:P171+ wd:Q25314 pattern seems to be sufficiently selective in your case.

  2. Wait until they implement full-text search.

  3. Use Quarry (example query).


Another option is to use Virtuoso full-text search capabilities on wikidata.dbpedia.org:

SELECT ?s WHERE { 
   ?resource rdfs:label ?label .
   ?label bif:contains "'topinambur'" .
   BIND ( IRI ( REPLACE ( STR(?resource),
                          "http://wikidata.dbpedia.org/resource",
                          "http://www.wikidata.org/entity"
                        )
              ) AS ?s
        )
}

Try it!


It seems that even the query below sometime works on wikidata.dbpedia.org without falling into timeout:

PREFIX dbo: <http://dbpedia.org/ontology/>

SELECT ?resource WHERE { 
   ?resource rdfs:label ?label .
   FILTER ( STR(?label) = "Topinambur" ) .
}

Try it!


Two hours ago I've removed this statement on Wikidata:

wd:Q161378 rdfs:label "topinambur"@ru .

I'm not a botanist, but 'topinambur' is definitely not a word in Russian.

1
  • ...actually It was really a strange case that the name ( label ) Topinambur referred 'Dioscorea communis' (Q161378) where in many other languages it is referred to Helianthus tuberosus (Q146190) Jul 25, 2017 at 20:46
0

Working further on from @quick's answer, and showing it for lexemes rather than labels. First identifying relevant language codes:

SELECT (GROUP_CONCAT(?mword; separator=" ") AS ?mwords) {
  BIND(1 AS ?dummy)
  VALUES ?word { "topinambur" }
  {
    SELECT (COUNT(?lexeme) AS ?count) ?language_code {
      ?lexeme dct:language / wdt:P424 ?language_code .
    }
    GROUP BY ?language_code
    HAVING (?count > 100)
    ORDER BY DESC(?count)
  }
  BIND(CONCAT('"', ?word, '"@', ?language_code) AS ?mword)
}
GROUP BY ?dummy

Try it!

Followed by the verbose query

SELECT (COUNT(?lexeme) AS ?count) ?language (GROUP_CONCAT(?word; separator=" ") AS ?words) {
  VALUES ?word { "topinambur"@eo "topinambur"@ko "topinambur"@bfi "topinambur"@nl "topinambur"@uk "topinambur"@cy "topinambur"@pt "topinambur"@zh "topinambur"@br "topinambur"@bg "topinambur"@ms "topinambur"@tg "topinambur"@se "topinambur"@ta "topinambur"@non "topinambur"@it "topinambur"@zh-min-nan "topinambur"@nan "topinambur"@fi "topinambur"@jbo "topinambur"@ml "topinambur"@ja "topinambur"@ku "topinambur"@bn "topinambur"@ar "topinambur"@nb "topinambur"@es "topinambur"@pl "topinambur"@nn "topinambur"@sk "topinambur"@da "topinambur"@de "topinambur"@cs "topinambur"@fr "topinambur"@sv "topinambur"@eu "topinambur"@he "topinambur"@la "topinambur"@en "topinambur"@ru }
  ?lexeme dct:language ?language ;
          ontolex:lexicalForm / ontolex:representation ?word .
}
GROUP BY ?language

Try it!

For querying on labels, do something similar to:

SELECT (COUNT(?item) AS ?count) ?language (GROUP_CONCAT(?word; separator=" ") AS ?words) {
  VALUES ?word { "topinambur"@eo "topinambur"@ko "topinambur"@bfi "topinambur"@nl "topinambur"@uk "topinambur"@cy "topinambur"@pt "topinambur"@zh "topinambur"@br "topinambur"@bg "topinambur"@ms "topinambur"@tg "topinambur"@se "topinambur"@ta "topinambur"@non "topinambur"@it "topinambur"@zh-min-nan "topinambur"@nan "topinambur"@fi "topinambur"@jbo "topinambur"@ml "topinambur"@ja "topinambur"@ku "topinambur"@bn "topinambur"@ar "topinambur"@nb "topinambur"@es "topinambur"@pl "topinambur"@nn "topinambur"@sk "topinambur"@da "topinambur"@de "topinambur"@cs "topinambur"@fr "topinambur"@sv "topinambur"@eu "topinambur"@he "topinambur"@la "topinambur"@en "topinambur"@ru }
  ?item rdfs:label ?word ;
}
GROUP BY ?language

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.