sparql-examples

A set of SPARQL examples that are used in different TGX resources

View the Project on GitHub BiGCAT-UM/sparql-examples

014

rq turtle/ttl

Author aspect: retracted articles per year

Use at

PREFIX p: <http://www.wikidata.org/prop/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://wikidata.org/entity/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
#defaultView:BarChart
PREFIX target: <http://www.wikidata.org/entity/Q97270>

# Inspired from LEGOLAS - http://abel.lis.illinois.edu/legolas/
# Shubhanshu Mishra, Vetle Torvik
select ?year (count(?work) as ?number_of_publications) ?role where {
  {
    select (str(?year_) as ?year) (0 as ?pages) ("_" as ?role) where {
      # default values = 0
      ?year_item wdt:P31 wd:Q577 .
      ?year_item wdt:P585 ?date .
      bind(year(?date) as ?year_)
      {
        select (min(?year_) as ?earliest_year)  (max(?year_) as ?latest_year) where {
          ?work wdt:P50 target: .
          ?work wdt:P577 ?publication_date .
          { ?work wdt:P31 wd:Q45182324 } UNION { ?work wdt:P793 wd:Q7316896 . } UNION { ?work wdt:P5824 [] . }
          bind(year(?publication_date) as ?year_)
        }
      }
      bind(year(now())+1 as ?next_year)
      filter (?year_ >= ?earliest_year && ?year_ <= ?latest_year)
    }
  }
  union {
  {
    select ?work (min(?years) as ?year) (count(?coauthors) as ?number_of_authors) ?author_number where {
      ?work (p:P50|p:P2093) ?author_statement .
      ?author_statement ps:P50 target: .
      optional { ?author_statement pq:P1545 ?author_number . }
      ?work (wdt:P50|wdt:P2093) ?coauthors .
      ?work wdt:P577 ?dates .
      { ?work wdt:P31 wd:Q45182324 } UNION { ?work wdt:P793 wd:Q7316896 . } UNION { ?work wdt:P5824 [] . }
      bind(str(year(?dates)) as ?years) .
    }
    group by ?work ?author_number
  }
  bind(coalesce(if(?number_of_authors = 1,
            'Solo author',
            if(xsd:integer(?author_number) = 1,
               'First author',
               if(xsd:integer(?author_number) = ?number_of_authors,
                  'Last author',
                  'Middle author'))), 'Unknown')
       as ?role)
   }
}
group by ?year ?role
order by ?year

graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
  v13("?author_number")
  v12("?author_statement")
  v14("?coauthors")
  v6("?date")
  v15("?dates")
  v9("?earliest_year")
  v9("?latest_year")
  v9("?next_year")
  v17("?number_of_authors")
  v18("?number_of_publications")
  v10("?pages")
  v8("?publication_date")
  v18("?role"):::projected 
  v7("?work"):::projected 
  v17("?year"):::projected 
  v9("?year_")
  v5("?year_item")
  v16("?years")
  a1((" "))
  a2((" "))
  c9([http://wikidata.org/entity/Q7316896]):::iri 
  c2([http://wikidata.org/entity/Q577]):::iri 
  c5(["wd:Q97270"]):::iri 
  c7([http://wikidata.org/entity/Q45182324]):::iri 
  subgraph union0[" Union "]
  subgraph union0l[" "]
    style union0l fill:#abf,stroke-dasharray: 3 3;
    subgraph union1[" Union "]
    subgraph union1l[" "]
      style union1l fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:P2093"-->  v12
    end
    subgraph union1r[" "]
      style union1r fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:P50"-->  v12
    end
    union1r <== or ==> union1l
    end
    v12 --"p:statement/P50"-->  c5
    subgraph optional0["(optional)"]
    style optional0 fill:#bbf,stroke-dasharray: 5 5;
      v12 -."p:qualifier/P1545".->  v13
    end
    subgraph union2[" Union "]
    subgraph union2l[" "]
      style union2l fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:direct/P2093"-->  v14
    end
    subgraph union2r[" "]
      style union2r fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:direct/P50"-->  v14
    end
    union2r <== or ==> union2l
    end
    v7 --"p:direct/P577"-->  v15
    subgraph union3[" Union "]
    subgraph union3l[" "]
      style union3l fill:#abf,stroke-dasharray: 3 3;
      subgraph union4[" Union "]
      subgraph union4l[" "]
        style union4l fill:#abf,stroke-dasharray: 3 3;
        v7 --"p:direct/P5824"-->  a2
      end
      subgraph union4r[" "]
        style union4r fill:#abf,stroke-dasharray: 3 3;
        v7 --"p:direct/P793"-->  c9
      end
      union4r <== or ==> union4l
      end
    end
    subgraph union3r[" "]
      style union3r fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:direct/P31"-->  c7
    end
    union3r <== or ==> union3l
    end
    bind0[/"str(year-from-dateTime(?dates))"/]
    v15 --o bind0
    bind0 --as--o v16
    bind3[/"min(?years)"/]
    v16 --o bind3
    bind3 --as--o v17
    bind4[/"count(?coauthors)"/]
    v14 --o bind4
    bind4 --as--o v17
    bind5[/"if(?number_of_authors = '1^^xsd:integer','Solo author',if(http://www.w3.org/2001/XMLSchema#integer(?author_number) = '1^^xsd:integer','First author',if(http://www.w3.org/2001/XMLSchema#integer(?author_number) = ?number_of_authors,'Last author','Middle author')))'Unknown'"/]
    v17 --o bind5
    v13 --o bind5
    bind5 --as--o v18
  end
  subgraph union0r[" "]
    style union0r fill:#abf,stroke-dasharray: 3 3;
    f6[["?year_ >= ?earliest_year?year_ <= ?latest_year"]]
    f6 --> v9
    f6 --> v9
    f6 --> v9
    v5 --"p:direct/P31"-->  c2
    v5 --"p:direct/P585"-->  v6
    bind7[/"year-from-dateTime(?date)"/]
    v6 --o bind7
    bind7 --as--o v9
    v7 --"p:direct/P50"-->  c5
    v7 --"p:direct/P577"-->  v8
    subgraph union5[" Union "]
    subgraph union5l[" "]
      style union5l fill:#abf,stroke-dasharray: 3 3;
      subgraph union6[" Union "]
      subgraph union6l[" "]
        style union6l fill:#abf,stroke-dasharray: 3 3;
        v7 --"p:direct/P5824"-->  a1
      end
      subgraph union6r[" "]
        style union6r fill:#abf,stroke-dasharray: 3 3;
        v7 --"p:direct/P793"-->  c9
      end
      union6r <== or ==> union6l
      end
    end
    subgraph union5r[" "]
      style union5r fill:#abf,stroke-dasharray: 3 3;
      v7 --"p:direct/P31"-->  c7
    end
    union5r <== or ==> union5l
    end
    bind8[/"year-from-dateTime(?publication_date)"/]
    v8 --o bind8
    bind8 --as--o v9
    bind11[/"min(?year_)"/]
    v9 --o bind11
    bind11 --as--o v9
    bind12[/"max(?year_)"/]
    v9 --o bind12
    bind12 --as--o v9
    bind13[/"year-from-dateTime(NOW()) + '+1^^xsd:integer'"/]
    bind13 --as--o v9
    bind14[/"str(?year_)"/]
    v9 --o bind14
    bind14 --as--o v17
    bind15[/"'0^^xsd:integer'"/]
    bind15 --as--o v10
    bind16[/"'_'"/]
    bind16 --as--o v18
  end
  union0r <== or ==> union0l
  end
  bind18[/"count(?work)"/]
  v7 --o bind18
  bind18 --as--o v18