Tutorial VI:
Covers SPARQL Extensions
for Social Media Analytics


Text Search

Show triples containing a text pattern. The bif:search_excerpt is used to format a short excerpt of the matching literal in search-engine style.

This query requires V6 or higher.

SELECT ?s
       ?p
       ( bif:search_excerpt
         ( bif:vector
           ( 'web', 'semantic' ) ,
           ?o
         )
       )
WHERE
  {
    ?s ?p ?o .
    FILTER
      (
        bif:contains
          ( ?o, '"semantic web"' )
      )
  }
LIMIT 10

## See LOD live results.

http://b3s.openlinksw.com/b3s/search.vsp?q=1

Text Search: Query Result

Text Search

Graphs With Text

What sources talk the most about a given subject? Show the top N graphs containing triples with the given text pattern. Sort by descending triple count.

This query requires V6 or higher.

SELECT ?g
       COUNT (*)
WHERE
  {
    GRAPH ?g
      {
        ?s ?p ?o .
        FILTER
          (
            bif:contains
              ( ?o, 'dakar AND paris' )
          )
      }
  }
GROUP BY ?g
ORDER BY DESC 2
LIMIT 50

## See LOD live results.

http://b3s.openlinksw.com/b3s/search.vsp?q=2

Graphs With Text: Query Result

Graphs With Text

Types of Things With Text

What types of objects contain a text pattern? Find matches, get the type. Group by type, order by count.

This query requires V6 or higher.

SELECT ?tp
       COUNT (*)
WHERE
  {
    GRAPH ?g
      {
        ?s ?p ?o .
        ?s a ?tp
        FILTER
          (
            bif:contains
              ( ?o, '"Paris Hilton"' )
          )
      }
  }
GROUP BY ?tp
ORDER BY DESC 2
LIMIT 10

## See LOD live results.

http://b3s.openlinksw.com/b3s/search.vsp?q=3

Types of Things With Text: Query Result

Graphs With Text

People With Shared Interests

Given a person, find people with the most interests in common with this person. Show the person, the number of shared interests, and the total number of interests.

This query requires V6 or higher.

SELECT ?p
       ?n
       ((
          SELECT COUNT (*)
          WHERE
            {
              ?p   foaf:interest  ?i  .
              ?ps  foaf:interest  ?i
            }
       ))
       ((
          SELECT COUNT (*)
          WHERE
            {
              ?p  foaf:interest  ?i
            }
       ))
WHERE
  {
    ?ps  foaf:nick  "brisch"@en  .
    {
      SELECT DISTINCT ?p
                      ?psi
      WHERE
        {
          ?p    foaf:interest  ?i  .
          ?psi  foaf:interest  ?i
        }
    }  .
    FILTER
      ( ?ps  =  ?psi )
    ?p  foaf:nick  ?n
  }
ORDER BY DESC 3
LIMIT 50

## See LOD live results.

http://b3s.openlinksw.com/b3s/search.vsp?q=8

People With Shared Interests: Query Result

People With Shared Interests

Interests Around

What else are people interested in X interested in? What else do Harry Potter fans like?

This query requires V6 or higher.

SELECT ?i2
       COUNT (*)
WHERE
  {
    ?p  foaf:interest  <http://dbpedia.org/resource/Semantics>  .
    ?p  foaf:interest  ?i2
  }
GROUP BY ?i2
ORDER BY DESC 2
LIMIT 20

## See LOD live results.

http://b3s.openlinksw.com/b3s/search.vsp?q=4

Interests Around: Query Result

Interests Around

Top 100 Authors by Text

Who writes the most about a topic? Show for each author the number of works mentioning the topic and total number of works. For all documents and posts, we have extracted named entities; the entity count shows the entities which occur in the works of each author. There are statistics about named entities occurring together; these are used to display a list of related entities.

This query requires V6 or higher.

SELECT ?auth
       ?cnt
       ((
          SELECT COUNT (DISTINCT ?xx)
          WHERE
            {
              ?xx  dc:creator  ?auth
            }
       ))
WHERE
  {
    {
      SELECT ?auth
             COUNT (DISTINCT ?d) AS ?cnt
      WHERE
        {
          ?d  dc:creator  ?auth  .
          ?d  ?p          ?o
          FILTER
            (
              bif:contains
                ( ?o, 'web AND semantic' )
              &&
              isIRI ( ?auth )
            )
        }
      GROUP BY ?auth
      ORDER BY DESC 2
      LIMIT 100
    }
  }

## See LOD live results.

http://b3s.openlinksw.com/b3s/search.vsp?q=5

Top 100 Authors by Text: Query Result

Top 100 Authors by Text

Social Connections a la LinkedIn

Show the people a person directly or indirectly knows. Sort by distance and count of connections of the known person.

This query requires V6 or higher.

SELECT ?o
       ?dist
       ((
          SELECT COUNT (*)
          WHERE
            {
              ?o  foaf:knows  ?xx
            }
       ))
WHERE
  {
    {
      SELECT ?s ?o
      WHERE
        {
          ?s  foaf:knows  ?o
        }
    }
    OPTION (
             TRANSITIVE    ,
             T_DISTINCT    ,
             T_IN ( ?s )   ,
             T_OUT ( ?o )  ,
             T_MIN ( 1 )   ,
             T_MAX ( 4 )   ,
             T_STEP ( 'step_no' ) AS ?dist
           ) .
    FILTER
      (
        ?s  =  <http://www.w3.org/People/Berners-Lee/card#i>
      )
  }
ORDER BY ?dist
         DESC 3
LIMIT 50

## See LOD live results.

http://b3s.openlinksw.com/b3s/search.vsp?q=6

Social Connections a la LinkedIn: Query Result

Social Connections a la LinkedIn

Connection Between

Given two people, find what chain of acquaintances links them together. For each step in the chain, show the person linked to, the graph linking this person to the previous person, the number of the step, and the number of the path. Note that there may be many paths through which the people are linked.

This query requires V6 or higher.

SELECT ?link
       ?g
       ?step
       ?path
WHERE
  {
    {
      SELECT ?s
             ?o
             ?g
      WHERE
        {
          GRAPH ?g
            {
              ?s  foaf:knows  ?o
            }
        }
    }
  OPTION
    (
      TRANSITIVE                     ,
      T_DISTINCT                     ,
      T_IN ( ?s )                    ,
      T_OUT ( ?o )                   ,
      T_NO_CYCLES                    ,
      T_SHORTEST_ONLY                ,
      T_STEP ( ?s ) AS ?link         ,
      T_STEP ( 'path_id' ) AS ?path  ,
      T_STEP ( 'step_no' ) AS ?step  ,
      T_DIRECTION 3
    ) .
  FILTER
    (
      ?s  =  <http://www.w3.org/People/Berners-Lee/card#i>
      &&
      ?o  =  <http://myopenlink.net/dataspace/person/kidehen#this>
    )
  }
LIMIT 20

## See LOD live results.

http://b3s.openlinksw.com/b3s/search.vsp?q=7

Connection Between: Query Result

Connection Between

Cloud Around Person

This query requires V6 or higher.

# Show names of things surrounding a person. These
# may be interests, classes of things, other people, and
# so forth. For each link shows the count of occurrences
# largest count first.
SELECT ?s
       ?lbl
       COUNT (*)
WHERE
  {
    ?s   ?p2                                    ?o2   .
    ?o2  <http://yago-knowledge.org/resource/linksTo> ?lbl  .
    ?s   foaf:nick                              ?o    .
    FILTER
      (
        bif:contains
          ( ?o, '"King"' )
      )
  }
GROUP BY ?s
          ?lbl
ORDER BY DESC 3
LIMIT 10

## See LOD live results.

http://b3s.openlinksw.com/b3s/search.vsp?q=9

Cloud Around Person: Query Result

Cloud Around Person

Extract part of literal as variable for numeric comparison

This example shows how to extract a part of a literal as a variable for use in a numeric comparison.

Suppose there are the following triples inserted:

SQL>sparql
insert into graph <http://b3s-demo.openlinksw.com/label> { <:a>
                                                           <:p>
                                                           "123 abc" };
callret-0
VARCHAR
_________________________________________________________
Insert into <http://b3s-demo.openlinksw.com/label>, 1 triples -- done

1 Rows. -- 30 msec.

SQL>sparql
insert into graph <http://b3s-demo.openlinksw.com/label> { <:a>
                                                           <:p>
                                                           "234 abc" };
callret-0
VARCHAR
_________________________________________________________
Insert into <http://b3s-demo.openlinksw.com/label>, 1 triples -- done
1 Rows. -- 0 msec.

In order to extract the numeric part, and then do a numeric ( >.<,= ), you can use atoi (), atol or atof in the filter:

SELECT *
FROM <http://b3s-demo.openlinksw.com/label>
WHERE
  {
    ?s ?p ?o .
    FILTER (bif:atoi (?o) > 130)
  }

## See LOD live results.

Extract part of literal as variable for numeric comparison: Query Result

Extract part of literal as variable for numeric comparison
OpenLink
SIOC
RDF
Data Spaces
SPARQL
Query
Services
Graph
URI
Class
Container
SubClass
Entities
Web Services
Atom
SOAP
Moveable
Type
XML
RPC
FOAF
SKOS
ontology
Post
topic
tags
People
Network
Construction
Comment
CONSTRUCT
Endpoint
Data Set
Protocol
Data Web
Web Clients
URL
Social
Connections
LinkedIn
knows
Sort
distance
count
person
paths
linked
OWL
Annotea