Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Wikidata SPARQL queries #138

Open
ammar257ammar opened this issue Mar 11, 2021 · 0 comments
Open

Support for Wikidata SPARQL queries #138

ammar257ammar opened this issue Mar 11, 2021 · 0 comments

Comments

@ammar257ammar
Copy link

Hi all,
I was trying SPARQL.js to parse queries from Wikidata. Wikidata SPARQL syntax allows named subqueries and supports keywords (WITH, AS and INCLUDE) which are coming from the underlying Blazegraph triple store that Wikidata uses.
So, I couldn't parse those kind of queries and I got this error message:

Error: Parse error on line 5:
...?orcid_) AS ?orcid)WITH {  SELECT DIST
----------------------^
Expecting 'VAR', '(', 'WHERE', '{', 'FROM', got 'WITH'
    at Parser.parseError (/.npm-global/lib/node_modules/sparqljs/lib/SparqlParser.js:663:21)
    at Parser.parse (/.npm-global/lib/node_modules/sparqljs/lib/SparqlParser.js:730:22)
    at Parser.parser.parse (/.npm-global/lib/node_modules/sparqljs/sparql.js:31:37)
    at ReadStream.<anonymous> (/.npm-global/lib/node_modules/sparqljs/bin/sparql-to-json:25:65)
    at ReadStream.emit (events.js:326:22)
    at endReadableNT (_stream_readable.js:1241:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  hash: {
    text: 'WITH',
    token: 'WITH',
    line: 5,
    loc: { first_line: 5, last_line: 5, first_column: 28, last_column: 29 },
    expected: [ "'VAR'", "'('", "'WHERE'", "'{'", "'FROM'" ]
  }
}

And the SPARQL query I used is:

SELECT
  (SAMPLE(?number_of_works_) AS ?works)
  (SAMPLE(?wikis_) AS ?wikis)
  ?researcher ?researcherLabel ?researcherDescription
  (SAMPLE(?orcid_) AS ?orcid)
WITH {
  SELECT DISTINCT ?researcher WHERE {
    ?researcher ( wdt:P108 | wdt:P463 | wdt:P1416 ) / wdt:P361* wd:Q19845644 . 
  } 
} AS %researchers
WITH {
  SELECT
    (COUNT(?work) AS ?number_of_works_) ?researcher
  WHERE {
    INCLUDE %researchers
    OPTIONAL { ?work wdt:P50 ?researcher . }

    # No biological pathways; they skew the statistics too much 
    MINUS { ?work wdt:P31 wd:Q4915012 } 
  } 
  GROUP BY ?researcher
} AS %researchers_and_number_of_works
WHERE {
  INCLUDE %researchers_and_number_of_works
  OPTIONAL { ?researcher wdt:P496 ?orcid_ . }
  OPTIONAL { ?researcher wikibase:sitelinks ?wikis_ }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,da,de,es,fr,nl,no,ru,sv,zh" . } 
}
GROUP BY ?researcher ?researcherLabel ?researcherDescription 
ORDER BY DESC(?works)

It would be great if Wikidata queries can be supported. Wikidata has a lot of users and many applications are being developed on top of it like Scholia.

Thanks for all your efforts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant