# Reproducible Workflow
This notebook is intended to be a walkthrough of the paper results with examples that convey the main idea. We show the workflow for the entire paper.
* To load the LUBM Graph we use a public endpoint on [Dydra](http://dydra.com).
However for larger test cases we run our tests on Apache Jena. In case you have a LUBM on your local/public endpoint, you can load it as well. Please note that the LUBM that we use is _materialized_. The inferencing is **RDFS**.

* The DBpedia endpoint is a local endpoint, and if you want to test it, please replace it with either the public endpoint or your own local endpoint

* For any other questions or suggestions or concerns with the procedures, please do contact me kannaa@rpi.edu, I'll be glad to include your suggestions.

# Relaxation
Relaxation is the standard baseline for reformulating SPARQL queries. A lot of related work exists on reformulating SPARQL queries. The ideas are inherently based on *flexible* querying. In this notion, the different conditions in the input query are *loosened* or *relaxed* to give more results. This can also be looked at as *exploratory* querying.

## SPARQL Query Relaxation
Lets have a look at this hierarchy from LUBM on all teaching faculty in a University.
* Employee
* * Faculty
* * * Professor
* * * * VisitingProfessor
* * * * FullProfessor
* * * * Dean
* * * * Chair
* * * * AssociateProfessor
* * * * AssistantProfessor
* * * * PostDoc
* * * * Lecturer

Lets look how relaxation helps in getting more answers. In this case consider the following query on LUBM

```Select ?teacher where {
    ?teacher a Lecturer .
} ```

Lets see the results that we get from LUBM

```python
from rdflib import Graph
g = Graph()
g.parse('lubm_saturated.nt',format="nt")
print("Total triple statements in LUBM is " + str(len(g)))
```

    Total triple statements in LUBM is 144299

```python
from SPARQLWrapper import SPARQLWrapper, JSON
from IPython.display import display
import pandas as pd
import json
import numpy as np
from pandas.io.json import json_normalize

pd.options.display.max_colwidth = 100
pd.options.display.max_rows = 999

#Procedure to execute a SPARQL Query and get a pandas object out of it
def execute_query(sparqlQuery,endpoint):
    sparql = SPARQLWrapper(endpoint)
    sparql.setQuery(sparqlQuery)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    results_df = json_normalize(results["results"]["bindings"])
    return results_df

```

```python
%%time
query  = """

PREFIX  ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#>
Select ?teacher where {
    ?teacher a ub:Lecturer .
}

"""
endpoint  = "https://dydra.com/amarviswanathan/lubm/sparql"
results_df = execute_query(query,endpoint)

#Show the top-5 results
results_df.head()
```

    CPU times: user 8 ms, sys: 0 ns, total: 8 ms
    Wall time: 540 ms

```python
#Total number of lecturers
shape = results_df.shape
print("There are " + str(shape[0]) + " lecturers")
```

    There are 93 lecturers

But if the user is not satisfied with these 93 lecturers and wants to find more of them, a simple way is to relax by moving up in the hierarchy. So we move from Lecturer to Professor. This gives us the following query :

```
Select ?teacher where {
    ?teacher a ub:Professor .
}
```

```python
%%time
relaxed_query  = """

PREFIX  ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#>
Select ?teacher where {
    ?teacher a ub:Professor .
}
"""
results_df = execute_query(relaxed_query,endpoint)

#Show the top-5 results
results_df.head()
```

    CPU times: user 12 ms, sys: 4 ms, total: 16 ms
    Wall time: 703 ms

```python
#Total number of lecturers
shape = results_df.shape
print("There are " + str(shape[0]) + " Professors")
```

    There are 447 Professors

The above result gives us 447 Professors, each of whom may be any type under the hierarchy of **Professor**.

## Instance Query
Let us look at an instance of a **Professor** i.e. **<http://www.Department14.University0.edu/FullProfessor4>** and see what courses this Professor teaches.

```python
%%time
entity_query = """
PREFIX  ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#>
Select ?course where {
   <http://www.Department14.University0.edu/FullProfessor4> ub:teacherOf  ?course .
}
"""

results_df = execute_query(entity_query,endpoint)
results_df = results_df[['course.value']]
# Show all the results
results_df
```

    CPU times: user 4 ms, sys: 4 ms, total: 8 ms
    Wall time: 482 ms

So the **FullProfessor4** teaches `2 Graduate Courses` and `1 Course`.  **If** the user decides to want more answers, an automatic way would be to relax the query. Thus the system wouldrelax this entity value. However the entity has **no hierarchy**. Which means this entity ends up being relaxed to a variable. This is known as `simple relaxation`. The relaxed query then becomes

```Select ?course where {
    ?teacher ub:teacherOf ?course .
} ```

```python
%%time
entity_relaxed_query = """
PREFIX  ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#>
Select ?teacher ?course where {
   ?teacher ub:teacherOf  ?course .
}
"""

relax_df = execute_query(entity_relaxed_query,endpoint)

# Show all the results
relax_df
print("The total courses are " + str(relax_df.shape[0]))

relax_df.head()
```

    The total courses are 1627
    CPU times: user 64 ms, sys: 4 ms, total: 68 ms
    Wall time: 1.31 s

# Motivation
In this case, we end up relaxing the query to find _Anybody who takes any course_. Now this ends up giving `1627 results` and is very _generalized_. While this is logically right, wouldn't it be more beneficial if the system resulted in courses are more similar to what **Professor14** teaches?

# Goal

To address this issue, we present a technique where we utilize the _entity_ statements present in the graph to suggest reformulations. Let us see how this makes sense. Entities have properties(_predicate_) and values (_object_) in the graph. For example the entity **Professor14** has this triple associated with it

| Subject        | Predicate           | Object  |
| ------------- |:-------------:| -----:|
|**Professor14**|teacherOf|GraduateCourse5|

One could easily utilize these values much more effectively to create _triple patterns_ that can be appended back to the original query. This can then be used to suggest reformulations. We call the **predicate** and **object** value pair as a _feature_ or a _fact_. This is because they provide more contextual information about the entity in the RDF or RDFS graph. Since we utilize these features(facts) to create reformulations, we call our method
**Feature based reformulation of entities in triple pattern queries**.

The features provide more _information_ and _context_ about an entity. Let us see how features can be used. To do that we print out the features of **FullProfessor4** from LUBM.

```python
entity_statement_query = """
PREFIX  ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#>
Select ?p ?o where {
   <http://www.Department14.University0.edu/FullProfessor4> ?p ?o .
}
"""

results_df = execute_query(entity_statement_query,endpoint)

# Show all the results
results_df

```

      o.datatype
      o.type
      o.value
      p.type
      p.value

      0
      NaN
      uri
      http://www.w3.org/2000/01/rdf-schema#Resource
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      1
      NaN
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#FullProfessor
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      2
      NaN
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#Faculty
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      3
      NaN
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#Person
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      4
      NaN
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#Professor
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      5
      NaN
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#Employee
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      6
      http://www.w3.org/2001/XMLSchema#string
      literal
      FullProfessor4
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#name

      7
      NaN
      uri
      http://www.Department14.University0.edu/GraduateCourse5
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#teacherOf

      8
      NaN
      uri
      http://www.Department14.University0.edu/Course4
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#teacherOf

      9
      NaN
      uri
      http://www.Department14.University0.edu/GraduateCourse4
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#teacherOf

      10
      NaN
      uri
      http://www.University214.edu
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#undergraduateDegreeFrom

      11
      NaN
      uri
      http://www.University912.edu
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#mastersDegreeFrom

      12
      NaN
      uri
      http://www.University801.edu
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#doctoralDegreeFrom

      13
      NaN
      uri
      http://www.Department14.University0.edu
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#worksFor

      14
      http://www.w3.org/2001/XMLSchema#string
      literal
      FullProfessor4@Department14.University0.edu
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#emailAddress

      15
      http://www.w3.org/2001/XMLSchema#string
      literal
      xxx-xxx-xxxx
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#telephone

      16
      http://www.w3.org/2001/XMLSchema#string
      literal
      Research25
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#researchInterest

      17
      NaN
      uri
      http://www.Department14.University0.edu
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#memberOf

      18
      NaN
      uri
      http://www.University214.edu
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#degreeFrom

      19
      NaN
      uri
      http://www.University912.edu
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#degreeFrom

      20
      NaN
      uri
      http://www.University801.edu
      uri
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#degreeFrom

From the above result we see that the `literal` values don't add more information to the triple except that they are string values for an entity. Morever, they don't have any statements associated with them. So we filter the literal values out first.

```python
results_df = results_df[results_df['o.type'] == 'uri']
results_df = results_df[['p.value','o.value']]
results_df
```

      p.value
      o.value

      0
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.w3.org/2000/01/rdf-schema#Resource

      1
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#FullProfessor

      2
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#Faculty

      3
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#Person

      4
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#Professor

      5
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#Employee

      7
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#teacherOf
      http://www.Department14.University0.edu/GraduateCourse5

      8
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#teacherOf
      http://www.Department14.University0.edu/Course4

      9
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#teacherOf
      http://www.Department14.University0.edu/GraduateCourse4

      10
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#undergraduateDegreeFrom
      http://www.University214.edu

      11
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#mastersDegreeFrom
      http://www.University912.edu

      12
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#doctoralDegreeFrom
      http://www.University801.edu

      13
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#worksFor
      http://www.Department14.University0.edu

      17
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#memberOf
      http://www.Department14.University0.edu

      18
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#degreeFrom
      http://www.University214.edu

      19
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#degreeFrom
      http://www.University912.edu

      20
      http://swat.cse.lehigh.edu/onto/univ-bench.owl#degreeFrom
      http://www.University801.edu

The element in `row 0`, is too generic because it tags anything as a resource. So instead of that lets eyeball some interesting properties and change our initial query. For the sake of this example I pick

| Predicate        | Object           |
| ------------- |:-------------:|
|**mastersDegreeFrom**|University912.edu|
|**memberOf**|Department14.University0.edu|

The above two properties say something about the entity **Professor14** i.e. it says that **Professor14** got his `doctoralDegreeFrom University801.edu` and is `memberOf Department14.University0.edu`. To utilize these features in a query, one just has to convert them to a pattern so that it becomes a valid _triple pattern_. This is shown in the table below :

### Entity Statements

|Entity| Predicate | Value|
|--|--|--|
|**Professor14**| **mastersDegreeFrom**|University912.edu|
|**Professor14**| **memberOf**|Department14.University0.edu|

Replacing the entity **Professor14** with a variable `?x` we get the following :
### Entity Feature Patterns

|Variable|Predicate|Value|
|--|--|--|
|?x|**mastersDegreeFrom**|University912.edu|
|?x|**memberOf**|Department14.University0.edu|

Now let us pick the first pattern and add it back to the original query. Then lets add the second pattern to the original query independently.  This results in a  reformulated queries that looks like :

```
Select ?course where {
   ?x ub:teacherOf  ?course .
   ?x mastersDegreeFrom University912.edu .
}
```

```
Select ?course where {
   ?x ub:teacherOf  ?course .
   ?x memberOf Department14.University0.edu .
}
```

The inital query was :

* Select courses taught by **FullProfessor4**

Adding the two new features the query becomes :
* Select courses taught by ?x who has a mastersDegreeFrom `University912.edu`
* Select courses taught by ?x who is a member of `Department14.Univeristy0.edu` .

Both the above queries are more contextual and give precise answers than the initial relaxation which read as
* Select all courses taught by any teacher

So lets run the reformulation to see the results.

```python
%%time
entity_reformulated_query = """
PREFIX  ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#>
Select ?course where {
   ?x ub:teacherOf ?course .
   ?x ub:mastersDegreeFrom <http://www.University912.edu> .

}
"""

ref_1 = execute_query(entity_reformulated_query,endpoint)

# Show all the results
print("The number of courses now is " + str(ref_1.shape[0]))
ref_1.head()
```

    The number of courses now is 5
    CPU times: user 4 ms, sys: 4 ms, total: 8 ms
    Wall time: 457 ms

```python
%%time
entity_reformulated_query = """
PREFIX  ub: <http://swat.cse.lehigh.edu/onto/univ-bench.owl#>
Select ?course where {
   ?x ub:teacherOf ?course .
   ?x ub:memberOf <http://www.Department14.University0.edu> .

}
"""

ref_2 = execute_query(entity_reformulated_query,endpoint)

# Show all the results
print("The number of courses now is " + str(ref_2.shape[0]))
ref_2.head()
```

    The number of courses now is 97
    CPU times: user 8 ms, sys: 0 ns, total: 8 ms
    Wall time: 591 ms

Now lets find the common results between the two reformulations

```python
overall_ref = pd.merge(ref_1,ref_2,how='inner',on=['course.type','course.value'])
overall_ref
```

      course.type
      course.value

      0
      uri
      http://www.Department14.University0.edu/GraduateCourse5

      1
      uri
      http://www.Department14.University0.edu/Course4

      2
      uri
      http://www.Department14.University0.edu/GraduateCourse4

Lets find the results that are not common between the two reformulations.

```python
merged_df = pd.concat([ref_1,ref_2])
merged_df  = merged_df.drop_duplicates(keep=False)
merged_df.shape[0]
```

    96

So we now have the following comparisons between relaxation and reformulation. There are a total of `96 + 3 = 99 ` unique results out of the reformulation procedure, whereas the relaxation has a total of `1627` results. The `99` results are more related to the `?course` variable of the original query because of using the features.

### Results Comparison
| Original Query        | **Relaxation**           | Ref-1 | Ref-2 | Combined |
| ------------- |:-------------:|:--------:|:-------:|:---------|
|3|1627|5|97|99|

### Time Comparison
 | **Relaxation**           | Ref-1 | Ref-2 |
|:-------------:|:--------:|:-------:|
|1310ms|457ms|591ms|

* Clearly the **relaxation results are higher, whereas the reformulation results are lesser**. This makes this kind of reformulation more precise.
* In addition the time calculation shows that the **resulting reformulations also run in lesser time than the relaxed version of the query**.

This can be visualized as

You can access this visualization at the [jsfiddle](http://jsfiddle.net/N00bsie/hk6vjz4o/)

## Selecting Interesting Features

In the previous sections, we focused on the aspect of showing that an entity reformulation is more **contextual** and **precise** than an _entity relaxation_. However the following question remains :
* **Why do you need to pick subsets of features from the graph?**
* **How do you select relevant _features_ for an entity?**
* **Entities in large graphs have a lot of features. How many do you select?**

## DBpedia Example

Let us take an entity query involving `dbr:Martin_Scorsese`.  Assuming that initially the user is interested in looking at movies made by people similar to `dbr:Martin_Scorsese` and he starts out by making a query about the _movies made by_ `dbr:Martin_Scorsese`. Lets run it on the DBpedia end point to see what answers we get.

```python
dbpedia_query ="""
PREFIX  schema: <http://schema.org/>
PREFIX  dbr:  <http://dbpedia.org/resource/>
PREFIX  umbel-rc: <http://umbel.org/umbel/rc/>
PREFIX  dbc:  <http://dbpedia.org/resource/Category:>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  yago: <http://dbpedia.org/class/yago/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  dbp:  <http://dbpedia.org/property/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  dcterms: <http://purl.org/dc/terms/>
PREFIX  dbpedia-wikidata: <http://wikidata.dbpedia.org/resource/>
PREFIX  dul:  <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  dc:   <http://purl.org/dc/elements/1.1/>
PREFIX prov: <http://www.w3.org/ns/prov#>

SELECT  Distinct ?movie
WHERE
  {

     ?movie dbo:director dbr:Martin_Scorsese .
  }

"""
dbpedia_endpoint = "http://zen.cs.rpi.edu:8890/sparql"
movies_df = execute_query(dbpedia_query,dbpedia_endpoint)
print("The movies directed by dbr:Martin_Scorsese are :")
movies_df
```

    The movies directed by dbr:Martin_Scorsese are :

      movie.type
      movie.value

      0
      uri
      http://dbpedia.org/resource/Made_in_Milan

      1
      uri
      http://dbpedia.org/resource/The_Key_to_Reserva

      2
      uri
      http://dbpedia.org/resource/Mean_Streets

      3
      uri
      http://dbpedia.org/resource/Raging_Bull

      4
      uri
      http://dbpedia.org/resource/Taxi_Driver

      5
      uri
      http://dbpedia.org/resource/Alice_Doesn't_Live_Here_Anymore

      6
      uri
      http://dbpedia.org/resource/The_King_of_Comedy_(1983_film)

      7
      uri
      http://dbpedia.org/resource/Boxcar_Bertha

      8
      uri
      http://dbpedia.org/resource/Who's_That_Knocking_at_My_Door

      9
      uri
      http://dbpedia.org/resource/My_Voyage_to_Italy

      10
      uri
      http://dbpedia.org/resource/Goodfellas

      11
      uri
      http://dbpedia.org/resource/After_Hours_(film)

      12
      uri
      http://dbpedia.org/resource/New_York_Stories

      13
      uri
      http://dbpedia.org/resource/Kundun

      14
      uri
      http://dbpedia.org/resource/No_Direction_Home

      15
      uri
      http://dbpedia.org/resource/Street_Scenes

      16
      uri
      http://dbpedia.org/resource/The_Blues_(film)

      17
      uri
      http://dbpedia.org/resource/The_Last_Temptation_of_Christ_(film)

      18
      uri
      http://dbpedia.org/resource/Vinyl_(TV_series)

      19
      uri
      http://dbpedia.org/resource/Bringing_Out_the_Dead

      20
      uri
      http://dbpedia.org/resource/The_Aviator_(2004_film)

      21
      uri
      http://dbpedia.org/resource/Shutter_Island_(film)

      22
      uri
      http://dbpedia.org/resource/Silence_(2016_film)

      23
      uri
      http://dbpedia.org/resource/Casino_(film)

      24
      uri
      http://dbpedia.org/resource/New_York,_New_York_(film)

      25
      uri
      http://dbpedia.org/resource/The_Departed

      26
      uri
      http://dbpedia.org/resource/A_Personal_Journey_with_Martin_Scorsese_Through_American_Movies

      27
      uri
      http://dbpedia.org/resource/The_Age_of_Innocence_(1993_film)

      28
      uri
      http://dbpedia.org/resource/What's_a_Nice_Girl_Like_You_Doing_in_a_Place_Like_This%3F

      29
      uri
      http://dbpedia.org/resource/Shine_a_Light_(film)

      30
      uri
      http://dbpedia.org/resource/Shine_a_Light_(The_Rolling_Stones_album)

      31
      uri
      http://dbpedia.org/resource/George_Harrison:_Living_in_the_Material_World

      32
      uri
      http://dbpedia.org/resource/A_Letter_to_Elia

      33
      uri
      http://dbpedia.org/resource/Cape_Fear_(1991_film)

      34
      uri
      http://dbpedia.org/resource/Gangs_of_New_York

      35
      uri
      http://dbpedia.org/resource/The_Color_of_Money

      36
      uri
      http://dbpedia.org/resource/The_Last_Waltz

      37
      uri
      http://dbpedia.org/resource/The_Big_Shave

      38
      uri
      http://dbpedia.org/resource/Italianamerican

      39
      uri
      http://dbpedia.org/resource/American_Boy:_A_Profile_of_Steven_Prince

      40
      uri
      http://dbpedia.org/resource/It's_Not_Just_You,_Murray!

      41
      uri
      http://dbpedia.org/resource/Hugo_(film)

      42
      uri
      http://dbpedia.org/resource/Boardwalk_Empire_(episode)

      43
      uri
      http://dbpedia.org/resource/The_Wolf_of_Wall_Street_(2013_film)

      44
      uri
      http://dbpedia.org/resource/Public_Speaking_(film)

```python
print("dbr:Martin_Scorsese has directed a total of " + str(movies_df.shape[0]) + " movies")
```

    dbr:Martin_Scorsese has directed a total of 45 movies

As before lets relaxing `dbr:Martin_Scorsese` will lead to to the following query

```
SELECT  Distinct ?movie
WHERE
  {

     ?movie dbo:director ?y .
  }
```
Let us execute this query on DBpedia to see how many movies we get

```python
%%time
dbpedia_query ="""
PREFIX  schema: <http://schema.org/>
PREFIX  dbr:  <http://dbpedia.org/resource/>
PREFIX  umbel-rc: <http://umbel.org/umbel/rc/>
PREFIX  dbc:  <http://dbpedia.org/resource/Category:>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  yago: <http://dbpedia.org/class/yago/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  dbp:  <http://dbpedia.org/property/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  dcterms: <http://purl.org/dc/terms/>
PREFIX  dbpedia-wikidata: <http://wikidata.dbpedia.org/resource/>
PREFIX  dul:  <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  dc:   <http://purl.org/dc/elements/1.1/>
PREFIX prov: <http://www.w3.org/ns/prov#>

SELECT  Distinct ?movie
WHERE
  {

     ?movie dbo:director ?y .
  }

"""
dbpedia_endpoint = "http://zen.cs.rpi.edu:8890/sparql"
movies_df = execute_query(dbpedia_query,dbpedia_endpoint)
print("The toal movies directed by anybody are : " + str(movies_df.shape[0]))

```

    The toal movies directed by anybody are : 103317
    CPU times: user 2.44 s, sys: 60 ms, total: 2.5 s
    Wall time: 5.01 s

Looking at the results, you would realize that not all the results are movies. For example the entity `http://dbpedia.org/resource/Indianapolis_Art_Center
n` at row index `102808` is not a movie, but rather an organization.

```python
print(movies_df.loc[102808])
```

    movie.type                                                     uri
    movie.value    http://dbpedia.org/resource/Indianapolis_Art_Center
    Name: 102808, dtype: object

This happens because the query is very generalized because of the _relaxation_. To address this we need to execute queries that are contextually similar to the original query.  Following what was discussed earlier, we start by looking at the properties of the entity `dbr:Martin_Scorsese`.

```python
%%time
dbpedia_entity_feature_query ="""
PREFIX  schema: <http://schema.org/>
PREFIX  dbr:  <http://dbpedia.org/resource/>
PREFIX  umbel-rc: <http://umbel.org/umbel/rc/>
PREFIX  dbc:  <http://dbpedia.org/resource/Category:>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  yago: <http://dbpedia.org/class/yago/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  dbp:  <http://dbpedia.org/property/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  dcterms: <http://purl.org/dc/terms/>
PREFIX  dbpedia-wikidata: <http://wikidata.dbpedia.org/resource/>
PREFIX  dul:  <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  dc:   <http://purl.org/dc/elements/1.1/>
PREFIX prov: <http://www.w3.org/ns/prov#>

SELECT ?p ?o
WHERE
  {

     dbr:Martin_Scorsese ?p ?o .
  }
"""
dbpedia_endpoint = "http://zen.cs.rpi.edu:8890/sparql"
entity_features = execute_query(dbpedia_entity_feature_query,dbpedia_endpoint)
display(entity_features)
```

      o.datatype
      o.type
      o.value
      o.xml:lang
      p.type
      p.value

      0
      NaN
      uri
      http://www.w3.org/2002/07/owl#Thing
      NaN
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      1
      NaN
      uri
      http://xmlns.com/foaf/0.1/Person
      NaN
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      2
      NaN
      uri
      http://schema.org/Person
      NaN
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      3
      NaN
      uri
      http://dbpedia.org/ontology/Person
      NaN
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      4
      NaN
      uri
      http://dbpedia.org/ontology/Agent
      NaN
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      5
      NaN
      uri
      http://www.wikidata.org/entity/Q215627
      NaN
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      6
      NaN
      uri
      http://www.wikidata.org/entity/Q5
      NaN
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      7
      NaN
      uri
      http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#Agent
      NaN
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      8
      NaN
      uri
      http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#NaturalPerson
      NaN
      uri
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type

      9
      NaN
      literal
      Martin Scorsese
      en
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      10
      NaN
      literal
      مارتن سكورسيزي
      ar
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      11
      NaN
      literal
      Martin Scorsese
      de
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      12
      NaN
      literal
      Martin Scorsese
      es
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      13
      NaN
      literal
      Martin Scorsese
      fr
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      14
      NaN
      literal
      Martin Scorsese
      it
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      15
      NaN
      literal
      マーティン・スコセッシ
      ja
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      16
      NaN
      literal
      Martin Scorsese
      nl
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      17
      NaN
      literal
      Martin Scorsese
      pl
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      18
      NaN
      literal
      Martin Scorsese
      pt
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      19
      NaN
      literal
      Скорсезе, Мартин
      ru
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      20
      NaN
      literal
      马丁·斯科塞斯
      zh
      uri
      http://www.w3.org/2000/01/rdf-schema#label

      21
      NaN
      literal
      Martin Marcantonio Luciano Scorsese [skɔɹˈsɛzɪ] (* 17. November 1942 in Queens, New York) ist ei...
      de
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      22
      NaN
      literal
      マーティン・スコセッシ（Martin Scorsese, 本名: Martin Marcantonio Luciano Scorsese, 1942年11月17日 - ）は、アメリカ合衆国の映...
      ja
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      23
      NaN
      literal
      Martin Scorsese (Queens (New York City), 17 november 1942) is een Amerikaans filmregisseur van S...
      nl
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      24
      NaN
      literal
      Martin Scorsese (ur. 17 listopada 1942 w Nowym Jorku) – amerykański reżyser, scenarzysta, montaż...
      pl
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      25
      NaN
      literal
      Martin Scorsese (Martin Charles Scorsese, Queens, Nova Iorque, 17 de novembro de 1942) é um cine...
      pt
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      26
      NaN
      literal
      Martin Charles Scorsese (/skɔrˈsɛsi/; born November 17, 1942) is an American director, producer,...
      en
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      27
      NaN
      literal
      مارتن سكورسيزي ولد في 17 نوفمبر 1942 نيويورك في الولايات المتحدة الأمريكية، هو مخرج سينمائي أمري...
      ar
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      28
      NaN
      literal
      Martin Charles Scorsese (Queens, Nueva York, 17 de noviembre de 1942), conocido como Martin Scor...
      es
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      29
      NaN
      literal
      Martin Scorsese est un réalisateur américain, né le 17 novembre 1942 à New York aux États-Unis.D...
      fr
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      30
      NaN
      literal
      Esponente della New Hollywood, è considerato uno dei maggiori e più importanti registi della sto...
      it
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      31
      NaN
      literal
      Ма́ртин Ча́рльз Скорсе́зе (англ. Martin Charles Scorsese, более точное произношение фамилии — Ск...
      ru
      uri
      http://www.w3.org/2000/01/rdf-schema#comment

      32
      NaN
      uri
      http://rdf.freebase.com/ns/m.04sry
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      33
      NaN
      uri
      http://www.wikidata.org/entity/Q41148
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      34
      NaN
      uri
      http://fr.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      35
      NaN
      uri
      http://de.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      36
      NaN
      uri
      http://cs.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      37
      NaN
      uri
      http://el.dbpedia.org/resource/Μάρτιν_Σκορσέζε
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      38
      NaN
      uri
      http://es.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      39
      NaN
      uri
      http://eu.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      40
      NaN
      uri
      http://id.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      41
      NaN
      uri
      http://it.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      42
      NaN
      uri
      http://ja.dbpedia.org/resource/マーティン・スコセッシ
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      43
      NaN
      uri
      http://ko.dbpedia.org/resource/마틴_스코세이지
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      44
      NaN
      uri
      http://nl.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      45
      NaN
      uri
      http://pl.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      46
      NaN
      uri
      http://pt.dbpedia.org/resource/Martin_Scorsese
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      47
      NaN
      uri
      http://wikidata.dbpedia.org/resource/Q41148
      NaN
      uri
      http://www.w3.org/2002/07/owl#sameAs

      48
      NaN
      uri
      http://dbpedia.org/resource/Category:1942_births
      NaN
      uri
      http://purl.org/dc/terms/subject

      49
      NaN
      uri
      http://dbpedia.org/resource/Category:20th-century_American_male_actors
      NaN
      uri
      http://purl.org/dc/terms/subject

      50
      NaN
      uri
      http://dbpedia.org/resource/Category:21st-century_American_male_actors
      NaN
      uri
      http://purl.org/dc/terms/subject

      51
      NaN
      uri
      http://dbpedia.org/resource/Category:American_Roman_Catholics
      NaN
      uri
      http://purl.org/dc/terms/subject

      52
      NaN
      uri
      http://dbpedia.org/resource/Category:American_documentary_filmmakers
      NaN
      uri
      http://purl.org/dc/terms/subject

      53
      NaN
      uri
      http://dbpedia.org/resource/Category:American_film_directors
      NaN
      uri
      http://purl.org/dc/terms/subject

      54
      NaN
      uri
      http://dbpedia.org/resource/Category:American_film_directors_of_Italian_descent
      NaN
      uri
      http://purl.org/dc/terms/subject

      55
      NaN
      uri
      http://dbpedia.org/resource/Category:American_film_editors
      NaN
      uri
      http://purl.org/dc/terms/subject

      56
      NaN
      uri
      http://dbpedia.org/resource/Category:American_film_producers
      NaN
      uri
      http://purl.org/dc/terms/subject

      57
      NaN
      uri
      http://dbpedia.org/resource/Category:American_male_film_actors
      NaN
      uri
      http://purl.org/dc/terms/subject

      58
      NaN
      uri
      http://dbpedia.org/resource/Category:American_male_screenwriters
      NaN
      uri
      http://purl.org/dc/terms/subject

      59
      NaN
      uri
      http://dbpedia.org/resource/Category:American_music_video_directors
      NaN
      uri
      http://purl.org/dc/terms/subject

      60
      NaN
      uri
      http://dbpedia.org/resource/Category:American_people_of_Sicilian_descent
      NaN
      uri
      http://purl.org/dc/terms/subject

      61
      NaN
      uri
      http://dbpedia.org/resource/Category:Best_Director_Academy_Award_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      62
      NaN
      uri
      http://dbpedia.org/resource/Category:Best_Director_BAFTA_Award_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      63
      NaN
      uri
      http://dbpedia.org/resource/Category:Best_Director_Golden_Globe_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      64
      NaN
      uri
      http://dbpedia.org/resource/Category:Cecil_B._DeMille_Award_Golden_Globe_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      65
      NaN
      uri
      http://dbpedia.org/resource/Category:César_Award_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      66
      NaN
      uri
      http://dbpedia.org/resource/Category:David_di_Donatello_Career_Award_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      67
      NaN
      uri
      http://dbpedia.org/resource/Category:Directors_Guild_of_America_Award_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      68
      NaN
      uri
      http://dbpedia.org/resource/Category:English-language_film_directors
      NaN
      uri
      http://purl.org/dc/terms/subject

      69
      NaN
      uri
      http://dbpedia.org/resource/Category:Fellows_of_the_American_Academy_of_Arts_and_Sciences
      NaN
      uri
      http://purl.org/dc/terms/subject

      70
      NaN
      uri
      http://dbpedia.org/resource/Category:Film_directors_from_New_York_City
      NaN
      uri
      http://purl.org/dc/terms/subject

      71
      NaN
      uri
      http://dbpedia.org/resource/Category:Film_theorists
      NaN
      uri
      http://purl.org/dc/terms/subject

      72
      NaN
      uri
      http://dbpedia.org/resource/Category:Grammy_Award_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      73
      NaN
      uri
      http://dbpedia.org/resource/Category:Kennedy_Center_honorees
      NaN
      uri
      http://purl.org/dc/terms/subject

      74
      NaN
      uri
      http://dbpedia.org/resource/Category:Living_people
      NaN
      uri
      http://purl.org/dc/terms/subject

      75
      NaN
      uri
      http://dbpedia.org/resource/Category:Légion_d'honneur_recipients
      NaN
      uri
      http://purl.org/dc/terms/subject

      76
      NaN
      uri
      http://dbpedia.org/resource/Category:Male_actors_from_New_York_City
      NaN
      uri
      http://purl.org/dc/terms/subject

      77
      NaN
      uri
      http://dbpedia.org/resource/Category:Male_actors_of_Italian_descent
      NaN
      uri
      http://purl.org/dc/terms/subject

      78
      NaN
      uri
      http://dbpedia.org/resource/Category:New_York_Democrats
      NaN
      uri
      http://purl.org/dc/terms/subject

      79
      NaN
      uri
      http://dbpedia.org/resource/Category:People_from_Corona,_Queens
      NaN
      uri
      http://purl.org/dc/terms/subject

      80
      NaN
      uri
      http://dbpedia.org/resource/Category:Primetime_Emmy_Award_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      81
      NaN
      uri
      http://dbpedia.org/resource/Category:Tisch_School_of_the_Arts_alumni
      NaN
      uri
      http://purl.org/dc/terms/subject

      82
      NaN
      uri
      http://dbpedia.org/resource/Category:Transcendental_Meditation_practitioners
      NaN
      uri
      http://purl.org/dc/terms/subject

      83
      NaN
      uri
      http://dbpedia.org/resource/Category:Venice_Best_Director_Silver_Lion_winners
      NaN
      uri
      http://purl.org/dc/terms/subject

      84
      NaN
      uri
      http://dbpedia.org/resource/New_York_University
      NaN
      uri
      http://dbpedia.org/property/almaMater

      85
      NaN
      literal
      Scorsese, Martin C.
      en
      uri
      http://dbpedia.org/property/alternativeNames

      86
      http://www.w3.org/2001/XMLSchema#date
      typed-literal
      1942-11-17
      NaN
      uri
      http://dbpedia.org/property/birthDate

      87
      NaN
      literal
      Martin Charles Scorsese
      en
      uri
      http://dbpedia.org/property/birthName

      88
      NaN
      literal
      Queens, New York, U.S.
      en
      uri
      http://dbpedia.org/property/birthPlace

      89
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      3
      NaN
      uri
      http://dbpedia.org/property/children

      90
      NaN
      literal
      Category:Martin Scorsese
      en
      uri
      http://dbpedia.org/property/commons

      91
      http://www.w3.org/2001/XMLSchema#date
      typed-literal
      1942-11-17
      NaN
      uri
      http://dbpedia.org/property/dateOfBirth

      92
      NaN
      literal
      no
      en
      uri
      http://dbpedia.org/property/m

      93
      NaN
      literal
      no
      en
      uri
      http://dbpedia.org/property/mw

      94
      NaN
      literal
      Director, producer, actor, screenwriter, film historian
      en
      uri
      http://dbpedia.org/property/occupation

      95
      NaN
      literal
      Marty
      en
      uri
      http://dbpedia.org/property/otherNames

      96
      NaN
      uri
      http://dbpedia.org/resource/Catherine_Scorsese
      NaN
      uri
      http://dbpedia.org/property/parents

      97
      NaN
      uri
      http://dbpedia.org/resource/Charles_Scorsese
      NaN
      uri
      http://dbpedia.org/property/parents

      98
      NaN
      literal
      Flushing, New York, United States
      en
      uri
      http://dbpedia.org/property/placeOfBirth

      99
      NaN
      literal
      American film director
      en
      uri
      http://dbpedia.org/property/shortDescription

      100
      NaN
      literal
      no
      en
      uri
      http://dbpedia.org/property/species

      101
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1965
      NaN
      uri
      http://dbpedia.org/property/spouse

      102
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1971
      NaN
      uri
      http://dbpedia.org/property/spouse

      103
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1976
      NaN
      uri
      http://dbpedia.org/property/spouse

      104
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1977
      NaN
      uri
      http://dbpedia.org/property/spouse

      105
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1979
      NaN
      uri
      http://dbpedia.org/property/spouse

      106
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1982
      NaN
      uri
      http://dbpedia.org/property/spouse

      107
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1985
      NaN
      uri
      http://dbpedia.org/property/spouse

      108
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1991
      NaN
      uri
      http://dbpedia.org/property/spouse

      109
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1999
      NaN
      uri
      http://dbpedia.org/property/spouse

      110
      NaN
      uri
      http://dbpedia.org/resource/Isabella_Rossellini
      NaN
      uri
      http://dbpedia.org/property/spouse

      111
      NaN
      uri
      http://dbpedia.org/resource/Barbara_De_Fina
      NaN
      uri
      http://dbpedia.org/property/spouse

      112
      NaN
      uri
      http://dbpedia.org/resource/Julia_Cameron
      NaN
      uri
      http://dbpedia.org/property/spouse

      113
      NaN
      literal
      Helen Schermerhorn Morris
      en
      uri
      http://dbpedia.org/property/spouse

      114
      NaN
      literal
      Laraine Marie Brennan
      en
      uri
      http://dbpedia.org/property/spouse

      115
      NaN
      literal
      div.
      en
      uri
      http://dbpedia.org/property/spouse

      116
      NaN
      literal
      no
      en
      uri
      http://dbpedia.org/property/wikt

      117
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      1963
      NaN
      uri
      http://dbpedia.org/property/yearsActive

      118
      NaN
      literal
      no
      en
      uri
      http://dbpedia.org/property/b

      119
      NaN
      literal
      Scorsese at the 2010 Cannes Film Festival
      en
      uri
      http://dbpedia.org/property/caption

      120
      NaN
      literal
      Q41148
      en
      uri
      http://dbpedia.org/property/d

      121
      NaN
      literal
      no
      en
      uri
      http://dbpedia.org/property/n

      122
      NaN
      literal
      Martin Scorsese
      en
      uri
      http://dbpedia.org/property/name

      123
      NaN
      literal
      Scorsese, Martin
      en
      uri
      http://dbpedia.org/property/name

      124
      NaN
      literal
      no
      en
      uri
      http://dbpedia.org/property/s

      125
      NaN
      literal
      Awards for Martin Scorsese
      en
      uri
      http://dbpedia.org/property/title

      126
      NaN
      literal
      no
      en
      uri
      http://dbpedia.org/property/v

      127
      NaN
      literal
      no
      en
      uri
      http://dbpedia.org/property/voy

      128
      NaN
      uri
      http://digitalcontentproducer.com/podcasts/audio/scorsese_departed/
      NaN
      uri
      http://dbpedia.org/ontology/wikiPageExternalLink

      129
      NaN
      uri
      http://realserver.princeton.edu:8080/ramgen/lectures/19990504scorseseTV7200K.rm
      NaN
      uri
      http://dbpedia.org/ontology/wikiPageExternalLink

      130
      NaN
      uri
      http://www.theyshootpictures.com/scorsesemartin.htm
      NaN
      uri
      http://dbpedia.org/ontology/wikiPageExternalLink

      131
      NaN
      uri
      http://www.lib.berkeley.edu/MRC/martinscorsese.html
      NaN
      uri
      http://dbpedia.org/ontology/wikiPageExternalLink

      132
      NaN
      uri
      http://www.vanityfair.com/culture/features/2010/03/proust-scorsese-201003
      NaN
      uri
      http://dbpedia.org/ontology/wikiPageExternalLink

      133
      NaN
      uri
      http://commons.wikimedia.org/wiki/Special:FilePath/Martin_Scorsese_Cannes_2010.jpg
      NaN
      uri
      http://xmlns.com/foaf/0.1/depiction

      134
      NaN
      uri
      http://commons.wikimedia.org/wiki/Special:FilePath/Martin_Scorsese_Cannes_2010.jpg?width=300
      NaN
      uri
      http://dbpedia.org/ontology/thumbnail

      135
      NaN
      literal
      Martin Charles Scorsese (/skɔrˈsɛsi/; born November 17, 1942) is an American director, producer,...
      en
      uri
      http://dbpedia.org/ontology/abstract

      136
      NaN
      literal
      مارتن سكورسيزي ولد في 17 نوفمبر 1942 نيويورك في الولايات المتحدة الأمريكية، هو مخرج سينمائي أمري...
      ar
      uri
      http://dbpedia.org/ontology/abstract

      137
      NaN
      literal
      Martin Marcantonio Luciano Scorsese [skɔɹˈsɛzɪ] (* 17. November 1942 in Queens, New York) ist ei...
      de
      uri
      http://dbpedia.org/ontology/abstract

      138
      NaN
      literal
      Martin Charles Scorsese (Queens, Nueva York, 17 de noviembre de 1942), conocido como Martin Scor...
      es
      uri
      http://dbpedia.org/ontology/abstract

      139
      NaN
      literal
      Martin Scorsese est un réalisateur américain, né le 17 novembre 1942 à New York aux États-Unis.D...
      fr
      uri
      http://dbpedia.org/ontology/abstract

      140
      NaN
      literal
      Esponente della New Hollywood, è considerato uno dei maggiori e più importanti registi della sto...
      it
      uri
      http://dbpedia.org/ontology/abstract

      141
      NaN
      literal
      マーティン・スコセッシ（Martin Scorsese, 本名: Martin Marcantonio Luciano Scorsese, 1942年11月17日 - ）は、アメリカ合衆国の映...
      ja
      uri
      http://dbpedia.org/ontology/abstract

      142
      NaN
      literal
      Martin Scorsese (Queens (New York City), 17 november 1942) is een Amerikaans filmregisseur van S...
      nl
      uri
      http://dbpedia.org/ontology/abstract

      143
      NaN
      literal
      Martin Scorsese (ur. 17 listopada 1942 w Nowym Jorku) – amerykański reżyser, scenarzysta, montaż...
      pl
      uri
      http://dbpedia.org/ontology/abstract

      144
      NaN
      literal
      Martin Scorsese (Martin Charles Scorsese, Queens, Nova Iorque, 17 de novembro de 1942) é um cine...
      pt
      uri
      http://dbpedia.org/ontology/abstract

      145
      NaN
      literal
      Ма́ртин Ча́рльз Скорсе́зе (англ. Martin Charles Scorsese, более точное произношение фамилии — Ск...
      ru
      uri
      http://dbpedia.org/ontology/abstract

      146
      NaN
      uri
      http://en.wikipedia.org/wiki/Martin_Scorsese?oldid=682860997
      NaN
      uri
      http://www.w3.org/ns/prov#wasDerivedFrom

      147
      NaN
      literal
      American film director
      en
      uri
      http://purl.org/dc/elements/1.1/description

      148
      NaN
      literal
      American film director
      NaN
      uri
      http://purl.org/dc/elements/1.1/description

      149
      http://www.w3.org/2001/XMLSchema#gYear
      typed-literal
      1963-01-01T00:00:00-05:00
      NaN
      uri
      http://dbpedia.org/ontology/activeYearsStartYear

      150
      NaN
      literal
      Marty
      en
      uri
      http://dbpedia.org/ontology/alias

      151
      NaN
      literal
      Scorsese, Martin C.
      en
      uri
      http://dbpedia.org/ontology/alias

      152
      http://www.w3.org/2001/XMLSchema#date
      typed-literal
      1942-11-17
      NaN
      uri
      http://dbpedia.org/ontology/birthDate

      153
      NaN
      literal
      Martin Charles Scorsese
      en
      uri
      http://dbpedia.org/ontology/birthName

      154
      http://www.w3.org/2001/XMLSchema#gYear
      typed-literal
      1942-01-01T00:00:00-05:00
      NaN
      uri
      http://dbpedia.org/ontology/birthYear

      155
      NaN
      literal
      Martin
      en
      uri
      http://xmlns.com/foaf/0.1/givenName

      156
      NaN
      literal
      Martin Scorsese
      en
      uri
      http://xmlns.com/foaf/0.1/name

      157
      NaN
      literal
      Scorsese, Martin
      en
      uri
      http://xmlns.com/foaf/0.1/name

      158
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      18995
      NaN
      uri
      http://dbpedia.org/ontology/wikiPageID

      159
      NaN
      literal
      Scorsese
      en
      uri
      http://xmlns.com/foaf/0.1/surname

      160
      http://www.w3.org/2001/XMLSchema#integer
      typed-literal
      682860997
      NaN
      uri
      http://dbpedia.org/ontology/wikiPageRevisionID

      161
      NaN
      uri
      http://dbpedia.org/resource/New_York_University
      NaN
      uri
      http://dbpedia.org/ontology/almaMater

      162
      NaN
      uri
      http://dbpedia.org/resource/Queens
      NaN
      uri
      http://dbpedia.org/ontology/birthPlace

      163
      NaN
      uri
      http://dbpedia.org/resource/Flushing,_Queens
      NaN
      uri
      http://dbpedia.org/ontology/birthPlace

      164
      NaN
      uri
      http://dbpedia.org/resource/New_York
      NaN
      uri
      http://dbpedia.org/ontology/birthPlace

      165
      NaN
      uri
      http://dbpedia.org/resource/Martin_Scorsese__1
      NaN
      uri
      http://dbpedia.org/ontology/occupation

      166
      NaN
      uri
      http://dbpedia.org/resource/Catherine_Scorsese
      NaN
      uri
      http://dbpedia.org/ontology/parent

      167
      NaN
      uri
      http://dbpedia.org/resource/Charles_Scorsese
      NaN
      uri
      http://dbpedia.org/ontology/parent

      168
      NaN
      uri
      http://dbpedia.org/resource/Isabella_Rossellini
      NaN
      uri
      http://dbpedia.org/ontology/spouse

      169
      NaN
      uri
      http://dbpedia.org/resource/Barbara_De_Fina
      NaN
      uri
      http://dbpedia.org/ontology/spouse

      170
      NaN
      uri
      http://dbpedia.org/resource/Julia_Cameron
      NaN
      uri
      http://dbpedia.org/ontology/spouse

      171
      NaN
      uri
      http://en.wikipedia.org/wiki/Martin_Scorsese
      NaN
      uri
      http://xmlns.com/foaf/0.1/isPrimaryTopicOf

    CPU times: user 108 ms, sys: 4 ms, total: 112 ms
    Wall time: 124 ms

There are a total of `172` features/facts for the entity `dbr:Martin_Scorsese`. Now how do we pick features/facts to suggest entity reformulation?

Let us formalize the problem with the following definitions :

## Entity Features Summarization

### Entity Facts
Let $\mathbb{E}$ be the set of entities in the RDF Graph. Then a **fact** or a **feature** $f_i$  of an entity  e $\in \mathbb{E}$, is the $\langle p, o \rangle$ pair from the triple statement $\langle e, p, o \rangle$.

|Entity|Predicate|Object|
|--|--|--|
|**dbr:Martin_Scorsese**|**http://dbpedia.org/property/parents ** | http://dbpedia.org/resource/Charles_Scorsese	|

In the above table for the entity `dbr:Martin_Scorsese` the feature $\langle p, o \rangle$ is

|Predicate|Object|
|--|--|
|**http://dbpedia.org/property/parents** | http://dbpedia.org/resource/Charles_Scorsese	|

### Entity Fact Set
Now entities in large graphs like DBpedia, have a lot of facts associated with them. We can denote this by the fact set of an entity $FS(e)$. The total number for `dbr:Martin_Scorsese` in this setting is `172` as we have seen.

### Optimal Feature Set
While SPARQL queries can handle a lot of feature patterns, anything more than 10, would not be easy to interpret for the user. In fact heuristically most users prefer around `1-3` patterns. This is shown by the study done in in the **L**inked **S**PARQL **Q**ueries dataset. Here is how they look

So to pick a subset of the large feature patterns, we define a **Entity Summary**.

### Entity Summary
Given an entity $e$ from a knowledge graph and a positive integer $k$, we define an entity summary to be $k$ $\leq \mid FS(e) \mid $, entity summary of $e$, $\textit{Summ(e,k)} \subseteq \textit{FS(e)}$ such that $\mid\textit{Summ(e,k)}\mid = k$. For the example `dbr:Martin_Scorsese` we have
* $\mid FS(e) \mid $ = `172`

For $k$ = 2, here the entity summary would look like

| Predicate |Object|
|--|--|
| **http://dbpedia.org/property/parents**    | http://dbpedia.org/resource/Charles_Scorsese	|
| **http://dbpedia.org/ontology/birthPlace** | http://dbpedia.org/resource/Queens		|

Now this entity summary can be converted to an entity summary pattern. The following table shows the entity summary pattern from the summary

|Variable|Predicate|Object|
|--|--|--|
|?e|**http://dbpedia.org/property/parents** | http://dbpedia.org/resource/Charles_Scorsese	|
|?e|**http://dbpedia.org/ontology/birthPlace** | http://dbpedia.org/resource/Queens		|

In plain English, the $k=2$ summary can be stated as _Entity Martin Scorsese who has parents Charles Scorsese and birth place Queens_.

The summary pattern can be stated as _Any entity ?e that has parents Charles Scorsese and birth place Queens_.

### Picking Summary Sets

In the $k=2$ summary pattern for `dbr:Martin_Scorsese` we see that `parent, Charles Scorsese` is too specific, because very few entities can have `Charles Scorsese` as parent. Similarly `birthPlace, Queens` is more generic becaue many entities can have `Queens` as a birth place. Ideally one should be able to pick features for reformulation that
* Is not too specific because that would end up not giving more results. This defeats the purpose of reformulation.
* Is not too generic because that would give up a lot of results and not make it any better than relaxation.

Ideally we want a function for the facts  $\left\lbrace f_1,f_2,\ldots f_k \right\rbrace $ in the fact set $\textit{FS(e)}$ so that they are ranked _ideally_ with a _ranking_ function such that $Rank(f_1) > Rank(f_2) > \ldots Rank(f_k)$. This _ranking function_ should be able to address the concerns of not being too _specifc_ or being too _generic_. We discuss how this can be achieved now.

### Ranking Entity Facts

We introduce two measures that can be combined to produce a _ranking_ to give a summary. While there can be many ranking functions that can be developed, our goal is to first start with a ranking function to solve the entity reformulation problem. We will then explore further how we can design better ranking functions. So the two measures that we define are
####  Specificity :
To pick features that convey _interesting_ information about the entity $e$ from the feature set $FS(e)$. For example `birthPlace, Queens` is not as interesting as `almaMater, New York University`. Borrowing from the world of Information Retrieval, we define _specificity_ of a feature $f_i$ based on **IDF** as follows :

$$ \begin{align}
\mbox{Specificity($f_i$)} = \log {\frac{\mid E \mid }{\mid e \mid \exists p,o:\langle e,p,o \rangle \in R\mid }}
\end{align} $$

Github sometimes doesn't render this so the image is here :
Here $\mid E \mid$ refers to the total number of entity resources in the knowledge graph R.

**Note:** For this paper we utilize only entity resources, so we filter out any feature $f_i$ that has literal values.

Let us now apply this to `dbr:Martin_Scorsese` to see the kind of features we get.

```python
endpoint = "http://zen.cs.rpi.edu:8890/sparql"

def specificity(column):
    return np.log(4641890/(float(column)))

def execute_augmented_query(sparqlQuery,endpoint):
    sparql = SPARQLWrapper(endpoint)
    sparql.setQuery(sparqlQuery)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    results_df = json_normalize(results["results"]["bindings"])
#     print(results_df)
    results_df = results_df[results_df['o.type'] == "uri"]
    results_df = results_df.drop(['o.type','p.type'],axis=1)
    results_df = results_df[['p.value','o.value','countS.value']]
    results_df['countS.value'] = results_df['countS.value'].astype(int)
#     results_df = results_df[results_df['countS.value']]
    return results_df

## This query is used to find the count values for the 'Specificity'.
augmented_query = """
PREFIX  schema: <http://schema.org/>
PREFIX  dbr:  <http://dbpedia.org/resource/>
PREFIX  umbel-rc: <http://umbel.org/umbel/rc/>
PREFIX  dbc:  <http://dbpedia.org/resource/Category:>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  yago: <http://dbpedia.org/class/yago/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  dbp:  <http://dbpedia.org/property/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  dcterms: <http://purl.org/dc/terms/>
PREFIX  dbpedia-wikidata: <http://wikidata.dbpedia.org/resource/>
PREFIX  dul:  <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  dc:   <http://purl.org/dc/elements/1.1/>
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX  dc: <http://purl.org/dc/elements/1.1/>

SELECT  ?p ?o (COUNT(DISTINCT ?s) AS ?countS)
WHERE
  { dbr:Martin_Scorsese
              ?p  ?o .
    ?s        ?p  ?o
    FILTER ( ! strstarts(str(?p), str(dbo:wikiPageWikiLink)) )
    FILTER ( ! strstarts(str(?p), str(dbo:wikiPageID)) )
    FILTER ( ! strstarts(str(?p), str(dbo:wikiPageExternalLink)) )
    FILTER ( ! strstarts(str(?p), str(dbo:wikiPageRevisionID)) )
    FILTER ( ! strstarts(str(?p), str(dbo:wikiPageLength)) )
    FILTER ( ! strstarts(str(?p), str(owl:sameAs)) )
    FILTER ( ! strstarts(str(?p), str(dbo:viafId)) )
    FILTER ( ! strstarts(str(?p), str(dbo:wikiPageOutDegree)) )
    FILTER ( ! strstarts(str(?p), str(schema:comment)) )
    FILTER ( ! strstarts(str(?p), str(dbo:abstract)) )
    FILTER ( ! strstarts(str(?p), str(rdfs:comment)) )
    FILTER ( ! strstarts(str(?p), str(dbo:alias)) )
    FILTER ( ! strstarts(str(?p), str(rdfs:label)) )
    FILTER ( ! strstarts(str(?p), str(dbo:thumbnail)) )
    FILTER ( ! strstarts(str(?p), str(foaf:name)) )
    FILTER ( ! strstarts(str(?p), str(foaf:surname)) )
    FILTER ( ! strstarts(str(?p), str(foaf:depiction)) )
    FILTER ( ! strstarts(str(?p), str(foaf:isPrimaryTopicOf)) )
    FILTER ( ! strstarts(str(?p), str(dbp:hasPhotoCollection)) )
    FILTER ( ! strstarts(str(?p), str(dbp:wordnet_type)) )
    FILTER ( ! strstarts(str(?p), str(prov:wasDerivedFrom)) )
    FILTER ( ! strstarts(str(?p), str(dc:description)) )

  }
GROUP BY ?p ?o
ORDER BY ASC(?countS)
"""
results_df = execute_augmented_query(augmented_query,endpoint)
results_df.reset_index(inplace=True)
results_df['Specificity'] = results_df['countS.value'].apply(specificity)
results_df.sort_values(by=['Specificity'],ascending=False,inplace=True)
display(results_df)
```

      index
      p.value
      o.value
      countS.value
      Specificity

      0
      0
      http://dbpedia.org/ontology/spouse
      http://dbpedia.org/resource/Julia_Cameron
      1
      15.350632

      2
      6
      http://dbpedia.org/ontology/parent
      http://dbpedia.org/resource/Catherine_Scorsese
      1
      15.350632

      3
      7
      http://dbpedia.org/property/spouse
      http://dbpedia.org/resource/Isabella_Rossellini
      1
      15.350632

      4
      9
      http://dbpedia.org/ontology/spouse
      http://dbpedia.org/resource/Isabella_Rossellini
      1
      15.350632

      5
      10
      http://dbpedia.org/property/spouse
      http://dbpedia.org/resource/Julia_Cameron
      1
      15.350632

      6
      11
      http://dbpedia.org/property/spouse
      http://dbpedia.org/resource/Barbara_De_Fina
      1
      15.350632

      7
      12
      http://dbpedia.org/ontology/occupation
      http://dbpedia.org/resource/Martin_Scorsese__1
      1
      15.350632

      8
      15
      http://dbpedia.org/property/parents
      http://dbpedia.org/resource/Charles_Scorsese
      1
      15.350632

      9
      18
      http://dbpedia.org/ontology/spouse
      http://dbpedia.org/resource/Barbara_De_Fina
      1
      15.350632

      10
      21
      http://dbpedia.org/property/parents
      http://dbpedia.org/resource/Catherine_Scorsese
      1
      15.350632

      1
      5
      http://dbpedia.org/ontology/parent
      http://dbpedia.org/resource/Charles_Scorsese
      1
      15.350632

      11
      26
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Venice_Best_Director_Silver_Lion_winners
      19
      12.406193

      12
      29
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:People_from_Corona,_Queens
      24
      12.172578

      13
      31
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Best_Director_BAFTA_Award_winners
      38
      11.713046

      14
      32
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:David_di_Donatello_Career_Award_winners
      45
      11.543970

      16
      34
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Best_Director_Golden_Globe_winners
      52
      11.399388

      15
      33
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_directors_of_Italian_descent
      52
      11.399388

      17
      35
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Cecil_B._DeMille_Award_Golden_Globe_winners
      62
      11.223498

      18
      37
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Best_Director_Academy_Award_winners
      68
      11.131124

      19
      38
      http://dbpedia.org/ontology/birthPlace
      http://dbpedia.org/resource/Flushing,_Queens
      88
      10.873295

      20
      46
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:César_Award_winners
      137
      10.430651

      21
      48
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Film_theorists
      155
      10.307207

      22
      50
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Directors_Guild_of_America_Award_winners
      184
      10.135696

      23
      51
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Kennedy_Center_honorees
      188
      10.114190

      24
      52
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Film_directors_from_New_York_City
      198
      10.062365

      25
      53
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Transcendental_Meditation_practitioners
      212
      9.994046

      26
      54
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_people_of_Sicilian_descent
      267
      9.763384

      27
      55
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_music_video_directors
      372
      9.431738

      28
      57
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Primetime_Emmy_Award_winners
      431
      9.284524

      29
      58
      http://dbpedia.org/property/almaMater
      http://dbpedia.org/resource/New_York_University
      498
      9.140032

      30
      59
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_editors
      503
      9.130042

      31
      60
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:English-language_film_directors
      520
      9.096803

      32
      63
      http://dbpedia.org/ontology/almaMater
      http://dbpedia.org/resource/New_York_University
      581
      8.985881

      33
      64
      http://dbpedia.org/ontology/birthPlace
      http://dbpedia.org/resource/Queens
      595
      8.962071

      34
      65
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_documentary_filmmakers
      649
      8.875199

      35
      66
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Male_actors_of_Italian_descent
      726
      8.763082

      36
      67
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Tisch_School_of_the_Arts_alumni
      817
      8.644993

      37
      68
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Légion_d'honneur_recipients
      885
      8.565045

      38
      74
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Male_actors_from_New_York_City
      1454
      8.068559

      39
      77
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Fellows_of_the_American_Academy_of_Arts_and_Sciences
      1864
      7.820152

      40
      78
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:New_York_Democrats
      1966
      7.766876

      41
      81
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Grammy_Award_winners
      2389
      7.572002

      42
      82
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_Roman_Catholics
      2393
      7.570329

      43
      84
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:21st-century_American_male_actors
      3465
      7.200164

      44
      85
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_producers
      3489
      7.193262

      45
      86
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_male_screenwriters
      3608
      7.159723

      46
      88
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_directors
      4477
      6.943924

      47
      89
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:20th-century_American_male_actors
      6540
      6.564940

      48
      91
      http://dbpedia.org/ontology/birthPlace
      http://dbpedia.org/resource/New_York
      8078
      6.353733

      49
      92
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:1942_births
      8423
      6.311911

      50
      93
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_male_film_actors
      10374
      6.103574

      51
      94
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Living_people
      729176
      1.850962

      52
      95
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://dbpedia.org/ontology/Person
      1587944
      1.072682

      53
      96
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.wikidata.org/entity/Q215627
      1587944
      1.072682

      54
      97
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.wikidata.org/entity/Q5
      1587944
      1.072682

      55
      98
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://schema.org/Person
      1587944
      1.072682

      56
      99
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#NaturalPerson
      1587944
      1.072682

      57
      100
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://xmlns.com/foaf/0.1/Person
      1588593
      1.072273

      58
      101
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://dbpedia.org/ontology/Agent
      1855262
      0.917096

      59
      102
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#Agent
      1855262
      0.917096

      60
      103
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.w3.org/2002/07/owl#Thing
      4995959
      -0.073508

In the above table we see from the column **countS.value** that properties like `http://dbpedia.org/ontology/spouse	` and `http://dbpedia.org/ontology/parents` are very specific and using them to reformulate will not lead us to any new results. For example let us use `http://dbpedia.org/ontology/spouse, http://dbpedia.org/resource/Barbara_De_Fina` to reformulate. The triple pattern for this becomes
`?e http://dbpedia.org/ontology/spouse http://dbpedia.org/resource/Barbara_De_Fina`. Appending this to the original query we	get the following query
```
Select
?movie dbo:director ?e
?e http://dbpedia.org/ontology/spouse http://dbpedia.org/resource/Barbara_De_Fina .
```

The query translates in plain english as _Select all movies that are directed by an entity that has spouse Barbara De Fina_. This kind of a feature is too specific for just `dbr:Martin_Scorsese`. So we would need some other measure to pick features that are related to the entity and give more results, but aren't too generic. In addition we need to remove features that have **countS.value** = 1.

### Popularity

Since it is the value i.e. the $o$ in $\langle p, o \rangle$ that contributes to being selective, we consider the notion of _popularity_, which is inspired by **TF** or is a weighting term. This utilizes the value $o_i$ frequency of a feature $f_i$ which is nothing but the tuple $\langle p_i, o_i \rangle$ and can be defined as :
$$
\begin{align}
\mbox{Popularity($o_i$)}  = \log {\mid t \mid \exists o_i:\langle e,p,o_i \rangle \in R\mid }
\end{align}
$$

If the formula hasn't been rendered here is the image
### Combined Ranking for Features

Now that we have _popularity_ and _specificity_ we combine them to form the combined ranking for a feature $f_i$. So the ranking function for a feature $f_i = \langle p_i, o_i \rangle$  can be defined as :

$$
\begin{align}
\mbox{Rank($f_i$)}  = Specificity(f_i) * Popularity(o_i)
\end{align}
$$
If the formula hasn't been rendered here is the image
Let us apply this and see in real time how this changes the object values.

```python
import math
def nbycount(column):
    return 4641890/(1+float(column))

def execute_count_query(obj):
    if(obj.startswith('http')):
        sparqlQuery = "Select (count(*) as ?countRow)  WHERE { ?s " + "?p  <" + obj + "> }"
    else:
        sparqlQuery = 'Select (count(*) as ?countRow)  WHERE { ?s ' + '?p "' + obj + '"}'

    sparql = SPARQLWrapper("http://zen.cs.rpi.edu:8890/sparql")
    sparql.setQuery(sparqlQuery)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    results_df = json_normalize(results["results"]["bindings"])
    val = float(results_df['countRow.value'])
    return val
results_df = results_df[results_df['countS.value'] > 1]
results_df['Popularity'] = results_df['o.value'].apply(execute_count_query)
results_df['Popularity'] = results_df['Popularity'].apply(math.log)
results_df.sort_values(by=['Popularity'],ascending=True,inplace=True)
display(results_df)
```

      index
      p.value
      o.value
      countS.value
      Specificity
      Popularity

      11
      26
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Venice_Best_Director_Silver_Lion_winners
      19
      12.406193
      2.944439

      12
      29
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:People_from_Corona,_Queens
      24
      12.172578
      3.178054

      13
      31
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Best_Director_BAFTA_Award_winners
      38
      11.713046
      3.637586

      14
      32
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:David_di_Donatello_Career_Award_winners
      45
      11.543970
      3.806662

      15
      33
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_directors_of_Italian_descent
      52
      11.399388
      3.951244

      16
      34
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Best_Director_Golden_Globe_winners
      52
      11.399388
      3.970292

      17
      35
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Cecil_B._DeMille_Award_Golden_Globe_winners
      62
      11.223498
      4.127134

      18
      37
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Best_Director_Academy_Award_winners
      68
      11.131124
      4.234107

      20
      46
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:César_Award_winners
      137
      10.430651
      4.983607

      21
      48
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Film_theorists
      155
      10.307207
      5.043425

      22
      50
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Directors_Guild_of_America_Award_winners
      184
      10.135696
      5.214936

      23
      51
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Kennedy_Center_honorees
      188
      10.114190
      5.247024

      24
      52
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Film_directors_from_New_York_City
      198
      10.062365
      5.288267

      25
      53
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Transcendental_Meditation_practitioners
      212
      9.994046
      5.356586

      26
      54
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_people_of_Sicilian_descent
      267
      9.763384
      5.598422

      27
      55
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_music_video_directors
      372
      9.431738
      5.921578

      19
      38
      http://dbpedia.org/ontology/birthPlace
      http://dbpedia.org/resource/Flushing,_Queens
      88
      10.873295
      6.011267

      28
      57
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Primetime_Emmy_Award_winners
      431
      9.284524
      6.109248

      30
      59
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_editors
      503
      9.130042
      6.222576

      31
      60
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:English-language_film_directors
      520
      9.096803
      6.257668

      34
      65
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_documentary_filmmakers
      649
      8.875199
      6.476972

      35
      66
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Male_actors_of_Italian_descent
      726
      8.763082
      6.587550

      36
      67
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Tisch_School_of_the_Arts_alumni
      817
      8.644993
      6.705639

      37
      68
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Légion_d'honneur_recipients
      885
      8.565045
      6.796824

      38
      74
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Male_actors_from_New_York_City
      1454
      8.068559
      7.282761

      29
      58
      http://dbpedia.org/property/almaMater
      http://dbpedia.org/resource/New_York_University
      498
      9.140032
      7.423568

      32
      63
      http://dbpedia.org/ontology/almaMater
      http://dbpedia.org/resource/New_York_University
      581
      8.985881
      7.423568

      39
      77
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Fellows_of_the_American_Academy_of_Arts_and_Sciences
      1864
      7.820152
      7.531016

      40
      78
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:New_York_Democrats
      1966
      7.766876
      7.584773

      33
      64
      http://dbpedia.org/ontology/birthPlace
      http://dbpedia.org/resource/Queens
      595
      8.962071
      7.755767

      41
      81
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Grammy_Award_winners
      2389
      7.572002
      7.779467

      42
      82
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_Roman_Catholics
      2393
      7.570329
      7.787382

      43
      84
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:21st-century_American_male_actors
      3465
      7.200164
      8.151045

      44
      85
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_producers
      3489
      7.193262
      8.158516

      45
      86
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_male_screenwriters
      3608
      7.159723
      8.191186

      46
      88
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_directors
      4477
      6.943924
      8.408940

      47
      89
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:20th-century_American_male_actors
      6540
      6.564940
      8.786151

      49
      92
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:1942_births
      8423
      6.311911
      9.038721

      50
      93
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_male_film_actors
      10374
      6.103574
      9.247443

      48
      91
      http://dbpedia.org/ontology/birthPlace
      http://dbpedia.org/resource/New_York
      8078
      6.353733
      10.642826

      51
      94
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Living_people
      729176
      1.850962
      13.499699

      53
      96
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.wikidata.org/entity/Q215627
      1587944
      1.072682
      14.277952

      54
      97
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.wikidata.org/entity/Q5
      1587944
      1.072682
      14.277952

      55
      98
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://schema.org/Person
      1587944
      1.072682
      14.277952

      56
      99
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#NaturalPerson
      1587944
      1.072682
      14.277952

      52
      95
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://dbpedia.org/ontology/Person
      1587944
      1.072682
      14.278273

      57
      100
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://xmlns.com/foaf/0.1/Person
      1588593
      1.072273
      14.278386

      59
      102
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#Agent
      1855262
      0.917096
      14.433537

      58
      101
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://dbpedia.org/ontology/Agent
      1855262
      0.917096
      14.433553

      60
      103
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.w3.org/2002/07/owl#Thing
      4995959
      -0.073508
      15.424158

Clearly from the above results, we see that the most popular object values are ranked higher. Let us now use this to determine the combined ranking and then pick features:

```python
results_df['Rank'] = results_df['Specificity'] * results_df['Popularity']
results_df.sort_values(by=['Rank'],ascending=False,inplace=True)
display(results_df)
```

      index
      p.value
      o.value
      countS.value
      Specificity
      Popularity
      Rank

      33
      64
      http://dbpedia.org/ontology/birthPlace
      http://dbpedia.org/resource/Queens
      595
      8.962071
      7.755767
      69.507734

      29
      58
      http://dbpedia.org/property/almaMater
      http://dbpedia.org/resource/New_York_University
      498
      9.140032
      7.423568
      67.851654

      48
      91
      http://dbpedia.org/ontology/birthPlace
      http://dbpedia.org/resource/New_York
      8078
      6.353733
      10.642826
      67.621672

      32
      63
      http://dbpedia.org/ontology/almaMater
      http://dbpedia.org/resource/New_York_University
      581
      8.985881
      7.423568
      66.707306

      19
      38
      http://dbpedia.org/ontology/birthPlace
      http://dbpedia.org/resource/Flushing,_Queens
      88
      10.873295
      6.011267
      65.362283

      42
      82
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_Roman_Catholics
      2393
      7.570329
      7.787382
      58.953045

      40
      78
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:New_York_Democrats
      1966
      7.766876
      7.584773
      58.909991

      41
      81
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Grammy_Award_winners
      2389
      7.572002
      7.779467
      58.906140

      39
      77
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Fellows_of_the_American_Academy_of_Arts_and_Sciences
      1864
      7.820152
      7.531016
      58.893694

      38
      74
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Male_actors_from_New_York_City
      1454
      8.068559
      7.282761
      58.761385

      43
      84
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:21st-century_American_male_actors
      3465
      7.200164
      8.151045
      58.688862

      44
      85
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_producers
      3489
      7.193262
      8.158516
      58.686343

      45
      86
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_male_screenwriters
      3608
      7.159723
      8.191186
      58.646625

      46
      88
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_directors
      4477
      6.943924
      8.408940
      58.391035

      37
      68
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Légion_d'honneur_recipients
      885
      8.565045
      6.796824
      58.215098

      36
      67
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Tisch_School_of_the_Arts_alumni
      817
      8.644993
      6.705639
      57.970204

      35
      66
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Male_actors_of_Italian_descent
      726
      8.763082
      6.587550
      57.727242

      47
      89
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:20th-century_American_male_actors
      6540
      6.564940
      8.786151
      57.680552

      34
      65
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_documentary_filmmakers
      649
      8.875199
      6.476972
      57.484422

      49
      92
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:1942_births
      8423
      6.311911
      9.038721
      57.051603

      31
      60
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:English-language_film_directors
      520
      9.096803
      6.257668
      56.924772

      30
      59
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_editors
      503
      9.130042
      6.222576
      56.812383

      28
      57
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Primetime_Emmy_Award_winners
      431
      9.284524
      6.109248
      56.721456

      50
      93
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_male_film_actors
      10374
      6.103574
      9.247443
      56.442457

      27
      55
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_music_video_directors
      372
      9.431738
      5.921578
      55.850778

      26
      54
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_people_of_Sicilian_descent
      267
      9.763384
      5.598422
      54.659541

      25
      53
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Transcendental_Meditation_practitioners
      212
      9.994046
      5.356586
      53.533969

      24
      52
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Film_directors_from_New_York_City
      198
      10.062365
      5.288267
      53.212474

      23
      51
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Kennedy_Center_honorees
      188
      10.114190
      5.247024
      53.069399

      22
      50
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Directors_Guild_of_America_Award_winners
      184
      10.135696
      5.214936
      52.857006

      21
      48
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Film_theorists
      155
      10.307207
      5.043425
      51.983627

      20
      46
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:César_Award_winners
      137
      10.430651
      4.983607
      51.982263

      18
      37
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Best_Director_Academy_Award_winners
      68
      11.131124
      4.234107
      47.130366

      17
      35
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Cecil_B._DeMille_Award_Golden_Globe_winners
      62
      11.223498
      4.127134
      46.320884

      16
      34
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Best_Director_Golden_Globe_winners
      52
      11.399388
      3.970292
      45.258900

      15
      33
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:American_film_directors_of_Italian_descent
      52
      11.399388
      3.951244
      45.041762

      14
      32
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:David_di_Donatello_Career_Award_winners
      45
      11.543970
      3.806662
      43.943996

      13
      31
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Best_Director_BAFTA_Award_winners
      38
      11.713046
      3.637586
      42.607214

      12
      29
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:People_from_Corona,_Queens
      24
      12.172578
      3.178054
      38.685109

      11
      26
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Venice_Best_Director_Silver_Lion_winners
      19
      12.406193
      2.944439
      36.529279

      51
      94
      http://purl.org/dc/terms/subject
      http://dbpedia.org/resource/Category:Living_people
      729176
      1.850962
      13.499699
      24.987427

      52
      95
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://dbpedia.org/ontology/Person
      1587944
      1.072682
      14.278273
      15.316040

      53
      96
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.wikidata.org/entity/Q215627
      1587944
      1.072682
      14.277952
      15.315695

      54
      97
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.wikidata.org/entity/Q5
      1587944
      1.072682
      14.277952
      15.315695

      55
      98
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://schema.org/Person
      1587944
      1.072682
      14.277952
      15.315695

      56
      99
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#NaturalPerson
      1587944
      1.072682
      14.277952
      15.315695

      57
      100
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://xmlns.com/foaf/0.1/Person
      1588593
      1.072273
      14.278386
      15.310326

      58
      101
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://dbpedia.org/ontology/Agent
      1855262
      0.917096
      14.433553
      13.236949

      59
      102
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#Agent
      1855262
      0.917096
      14.433537
      13.236935

      60
      103
      http://www.w3.org/1999/02/22-rdf-syntax-ns#type
      http://www.w3.org/2002/07/owl#Thing
      4995959
      -0.073508
      15.424158
      -1.133795

Although some properties are repeated, because of the nature of DBpedia, we see that features like `birthPlace, Queens` and `almaMater New_York_University` are more representative of the entities similar to `dbr:Martin_Scorsese`. Let us now pick the top 2 features to create reformulate the original query as :
```
Select Distinct movie where {
?movie dbo:director ?e
?e http://dbpedia.org/ontology/birthPlace http://dbpedia.org/resource/Queens	 .
?e http://dbpedia.org/property/almaMater  http://dbpedia.org/resource/New_York_University .
}
```

Now running it to see the results we get :

```python
%%time
dbpedia_query ="""
PREFIX  schema: <http://schema.org/>
PREFIX  dbr:  <http://dbpedia.org/resource/>
PREFIX  umbel-rc: <http://umbel.org/umbel/rc/>
PREFIX  dbc:  <http://dbpedia.org/resource/Category:>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  yago: <http://dbpedia.org/class/yago/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  dbp:  <http://dbpedia.org/property/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  dcterms: <http://purl.org/dc/terms/>
PREFIX  dbpedia-wikidata: <http://wikidata.dbpedia.org/resource/>
PREFIX  dul:  <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  dc:   <http://purl.org/dc/elements/1.1/>
PREFIX prov: <http://www.w3.org/ns/prov#>

Select Distinct ?movie ?e where {
?movie dbo:director ?e .
?e <http://dbpedia.org/ontology/birthPlace> <http://dbpedia.org/resource/New_York>     .
?e <http://dbpedia.org/property/almaMater>  <http://dbpedia.org/resource/New_York_University> .
}
"""
dbpedia_endpoint = "http://zen.cs.rpi.edu:8890/sparql"
movies_df = execute_query(dbpedia_query,dbpedia_endpoint)
print("The toal movies directed by anybody are : " + str(movies_df.shape[0]))
display(movies_df)
```

    The toal movies directed by anybody are : 53

      e.type
      e.value
      movie.type
      movie.value

      0
      uri
      http://dbpedia.org/resource/Burt_Lancaster
      uri
      http://dbpedia.org/resource/The_Kentuckian

      1
      uri
      http://dbpedia.org/resource/Burt_Lancaster
      uri
      http://dbpedia.org/resource/The_Midnight_Man_(1974_film)

      2
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Made_in_Milan

      3
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Key_to_Reserva

      4
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Mean_Streets

      5
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Raging_Bull

      6
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Taxi_Driver

      7
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Alice_Doesn't_Live_Here_Anymore

      8
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_King_of_Comedy_(1983_film)

      9
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Boxcar_Bertha

      10
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Who's_That_Knocking_at_My_Door

      11
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/My_Voyage_to_Italy

      12
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Goodfellas

      13
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/After_Hours_(film)

      14
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/New_York_Stories

      15
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Kundun

      16
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/No_Direction_Home

      17
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Street_Scenes

      18
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Blues_(film)

      19
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Last_Temptation_of_Christ_(film)

      20
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Vinyl_(TV_series)

      21
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Bringing_Out_the_Dead

      22
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Aviator_(2004_film)

      23
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Shutter_Island_(film)

      24
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Silence_(2016_film)

      25
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Casino_(film)

      26
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/New_York,_New_York_(film)

      27
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Departed

      28
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/A_Personal_Journey_with_Martin_Scorsese_Through_American_Movies

      29
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Age_of_Innocence_(1993_film)

      30
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/What's_a_Nice_Girl_Like_You_Doing_in_a_Place_Like_This%3F

      31
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Shine_a_Light_(film)

      32
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Shine_a_Light_(The_Rolling_Stones_album)

      33
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/George_Harrison:_Living_in_the_Material_World

      34
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/A_Letter_to_Elia

      35
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Cape_Fear_(1991_film)

      36
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Gangs_of_New_York

      37
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Color_of_Money

      38
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Last_Waltz

      39
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Big_Shave

      40
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Italianamerican

      41
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/American_Boy:_A_Profile_of_Steven_Prince

      42
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/It's_Not_Just_You,_Murray!

      43
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Hugo_(film)

      44
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Boardwalk_Empire_(episode)

      45
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Wolf_of_Wall_Street_(2013_film)

      46
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Public_Speaking_(film)

      47
      uri
      http://dbpedia.org/resource/Andrew_Levitas
      uri
      http://dbpedia.org/resource/Lullaby_(2014_film)

      48
      uri
      http://dbpedia.org/resource/Herbert_B._Leonard
      uri
      http://dbpedia.org/resource/Going_Home_(1971_film)

      49
      uri
      http://dbpedia.org/resource/Herbert_B._Leonard
      uri
      http://dbpedia.org/resource/The_Perils_of_Pauline_(1967_film)

      50
      uri
      http://dbpedia.org/resource/Jesse_Dylan
      uri
      http://dbpedia.org/resource/American_Wedding

      51
      uri
      http://dbpedia.org/resource/Jesse_Dylan
      uri
      http://dbpedia.org/resource/How_High

      52
      uri
      http://dbpedia.org/resource/Jesse_Dylan
      uri
      http://dbpedia.org/resource/Kicking_&_Screaming_(2005_film)

    CPU times: user 48 ms, sys: 4 ms, total: 52 ms
    Wall time: 53.4 ms

Now in the above result you see 53 movies which other people have also directed. Thus, you  have other entities like `dbr:Herbert_B.Leonard` and `dbr:Jesse_Dylan` and `dbr:Burt_Lancaster`, who are both directors from `New_York` and are also _film producers_.

Let us also try another query using the other ranked properties i.e.

```
Select Distinct ?movie ?e where {
?movie dbo:director ?e .
?e <http://purl.org/dc/terms/subject> <http://dbpedia.org/resource/Category:American_Roman_Catholics> .
?e <http://purl.org/dc/terms/subject> <http://dbpedia.org/resource/Category:New_York_Democrats> .
}
```

**Note** : We have already used the `birthPlace` and `almaMater`. So we use the other properties to explicitly be diverse in results.

```python
%%time
dbpedia_query ="""
PREFIX  schema: <http://schema.org/>
PREFIX  dbr:  <http://dbpedia.org/resource/>
PREFIX  umbel-rc: <http://umbel.org/umbel/rc/>
PREFIX  dbc:  <http://dbpedia.org/resource/Category:>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  yago: <http://dbpedia.org/class/yago/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  dbo:  <http://dbpedia.org/ontology/>
PREFIX  dbp:  <http://dbpedia.org/property/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  dcterms: <http://purl.org/dc/terms/>
PREFIX  dbpedia-wikidata: <http://wikidata.dbpedia.org/resource/>
PREFIX  dul:  <http://www.ontologydesignpatterns.org/ont/dul/DUL.owl#>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
PREFIX  dc:   <http://purl.org/dc/elements/1.1/>
PREFIX prov: <http://www.w3.org/ns/prov#>

Select Distinct ?movie ?e where {
?movie dbo:director ?e .
?e <http://purl.org/dc/terms/subject> <http://dbpedia.org/resource/Category:American_Roman_Catholics> .
?e <http://purl.org/dc/terms/subject> <http://dbpedia.org/resource/Category:New_York_Democrats> .
}
"""
dbpedia_endpoint = "http://zen.cs.rpi.edu:8890/sparql"
movies_df = execute_query(dbpedia_query,dbpedia_endpoint)
print("The toal movies directed by anybody are : " + str(movies_df.shape[0]))
display(movies_df)
```

    The toal movies directed by anybody are : 49

      e.type
      e.value
      movie.type
      movie.value

      0
      uri
      http://dbpedia.org/resource/James_Cagney
      uri
      http://dbpedia.org/resource/Short_Cut_to_Hell

      1
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Made_in_Milan

      2
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Key_to_Reserva

      3
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Mean_Streets

      4
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Raging_Bull

      5
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Taxi_Driver

      6
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Alice_Doesn't_Live_Here_Anymore

      7
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_King_of_Comedy_(1983_film)

      8
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Boxcar_Bertha

      9
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Who's_That_Knocking_at_My_Door

      10
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/My_Voyage_to_Italy

      11
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Goodfellas

      12
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/After_Hours_(film)

      13
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/New_York_Stories

      14
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Kundun

      15
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/No_Direction_Home

      16
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Street_Scenes

      17
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Blues_(film)

      18
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Last_Temptation_of_Christ_(film)

      19
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Vinyl_(TV_series)

      20
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Bringing_Out_the_Dead

      21
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Aviator_(2004_film)

      22
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Shutter_Island_(film)

      23
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Silence_(2016_film)

      24
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Casino_(film)

      25
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/New_York,_New_York_(film)

      26
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Departed

      27
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/A_Personal_Journey_with_Martin_Scorsese_Through_American_Movies

      28
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Age_of_Innocence_(1993_film)

      29
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/What's_a_Nice_Girl_Like_You_Doing_in_a_Place_Like_This%3F

      30
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Shine_a_Light_(film)

      31
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Shine_a_Light_(The_Rolling_Stones_album)

      32
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/George_Harrison:_Living_in_the_Material_World

      33
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/A_Letter_to_Elia

      34
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Cape_Fear_(1991_film)

      35
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Gangs_of_New_York

      36
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Color_of_Money

      37
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Last_Waltz

      38
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Big_Shave

      39
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Italianamerican

      40
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/American_Boy:_A_Profile_of_Steven_Prince

      41
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/It's_Not_Just_You,_Murray!

      42
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Hugo_(film)

      43
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Boardwalk_Empire_(episode)

      44
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/The_Wolf_of_Wall_Street_(2013_film)

      45
      uri
      http://dbpedia.org/resource/Martin_Scorsese
      uri
      http://dbpedia.org/resource/Public_Speaking_(film)

      46
      uri
      http://dbpedia.org/resource/Robert_De_Niro
      uri
      http://dbpedia.org/resource/The_Good_Shepherd_(film)

      47
      uri
      http://dbpedia.org/resource/Robert_De_Niro
      uri
      http://dbpedia.org/resource/A_Bronx_Tale

      48
      uri
      http://dbpedia.org/resource/Alec_Baldwin
      uri
      http://dbpedia.org/resource/Shortcut_to_Happiness

    CPU times: user 28 ms, sys: 8 ms, total: 36 ms
    Wall time: 37 ms

Now in the above result you see 49 movies which other people have also directed. Thus, you  have other entities like `dbr:Robert_De_Niro` and `dbr:Alec_Baldwin`, who are related to `dbr:Martin_Scorsese`.

So we now have the following comparisons between relaxation and reformulation. There are a total of `45 + 4 + 8 = 57 ` unique results out of the reformulation procedure, whereas the relaxation has a total of `102808` results. The `57` results are more related to the `?movie` variable of the original query because of using the _contextualized_ features.

### Results Comparison
| Original Query        | **Relaxation**           | Ref-1 | Ref-2 | Combined |
| ------------- |:-------------:|:--------:|:-------:|:---------|
|45|102808|49|53|57|

### Time Comparison
 | **Relaxation**           | Ref-1 | Ref-2 |
|:-------------:|:--------:|:-------:|
|5010ms|53.4ms|39ms|

* Clearly the **relaxation results are higher, whereas the reformulation results are lesser**. This makes this kind of reformulation more precise.
* In addition the time calculation shows that the **resulting reformulations also run in lesser time than the relaxed version of the query**.

You can access the [jsfiddle](http://jsfiddle.net/N00bsie/dynpyrLq/3/)

# Conclusions

From the above results, we believe that this technique can be used to _contextualize_ entities and be used to reformulate entities in entity queries instead of relaxation.