pyneql.ontology package

Submodules

pyneql.ontology.book module

book is part of the project PyNeQL Author: Valérie Hanoka

class pyneql.ontology.book.Book(title=None, author=None, publisher=None, publication_date=None, gallica_url=None, url=None, query_language=<LanguagesIso6391.English: 'en'>, endpoints=None, class_name=u'Book')

Bases: pyneql.ontology.creative_work.CreativeWork

A semantic representation of a book.

has_gallica_url = None
has_publisher = None
query(strict_mode=False, check_type=False)

Launches the query of the current object to the specified endpoints, given the query options to constraints the types of the RDF triples predicates and retrieve the results. The queries options relies on the dictionaries contained in pyneql/utils/vocabulary.py.

Parameters:check_type – Boolean.

Check the type of the object (e.g: Book, Person, Location,…) directly in the SPARQL queries. If True, the restriction of the object’s type is done in the query. For instance, if the object is a Book, activating type checking will build queries where the object to find (?Book) is constrained by an union of RDF triples checking that ?Book is a Book: […] { ?Book a fabio:Book  } UNION […] UNION { ?Book a schemaorg:Book  } .

Parameters:strict_mode – Boolean.

Check the type of the object’s attributes (e.g: label, first name,…) directly in the SPARQL queries. If True, the predicates of the triplet whose values are instantiated will have their types checked against the allowed types listed in self.voc_attributes.

Example:

We are looking for a Thing whose label is “አዲስ አበባ”.

  • Non strict mode will have its query restrained to elements satisfying

the triplet ?Thing ?has_label "አዲስ አበባ".. The predicate is left undetermined (?has_label is a variable).

  • In strict mode, we are strict about the types of predicates of the triplet.

For the current class, those predicates will be listed in self.voc_attributes['has_label'] and combined in the SPARQL query. Here, for the example, we set ‘has_label’ allowed the RDF predicates ‘rdfs:label’ and u’wdt:P1813’.

>>> print(self.voc_attributes['has_label'])
>>> [u'rdfs:label', u'wdt:P1813']

So in strict_mode, the query will be constrained to:

[…]{ ?Thing rdfs:label "አዲስ አበባ"  } UNION { ?Thing wdt:P1813 "አዲስ አበባ"  }.[…]

pyneql.ontology.person module

personquerybuilder is part of the project PyNeQL Author: Valérie Hanoka

class pyneql.ontology.person.Person(full_name=None, last_name=None, first_name=None, url=None, query_language=<LanguagesIso6391.English: 'en'>, endpoints=None, class_name=u'Person')

Bases: pyneql.ontology.thing.Thing

A semantic representation of a person, retrieved from the Semantic Web.

get_birth_info()

This function returns all information that is available in the linked data about the birth of the person (e.g: date and/or place).

Returns:a dict of information concerning the birth of the person
get_death_info()

This function returns all information that is available in the linked data about the death of the person (e.g: date and/or place).

Returns:a dict of information concerning the death of the person
get_external_ids()

This function returns a curated list of external ids of the Person.

Returns:a dict of Person ids such as VIAF, Wikidata, IDREF, ARK,…
get_gender()

This function returns the gender of the person. We assume that there is only one gender available for a person in the retrieved data.

Returns:
  • ‘F’ if the person is labelled as a woman
  • ‘M’ if the person is labelled as a man
  • ‘MtF’, ‘FtM’, ‘intersex’ or ‘queer’ if the person is transgender or genderqueer.
  • ‘unknown’ if the gender information is unavailable.
get_names()

This function returns all information that is available in the linked data about the name of the person (e.g: birth name, family name, name in the native language,…).

Returns:a dict of information concerning the names of the person.
has_birth_year = None
has_death_year = None
has_first_name = None
has_full_name = None
has_last_name = None
has_url = None

pyneql.ontology.thing module

thing.py is part of the project PyNeQL Author: Valérie Hanoka

class pyneql.ontology.thing.Thing(query_language=<LanguagesIso6391.English: 'en'>, label=None, url=None, endpoints=None, limit=1500, class_name=u'Thing')

Bases: object

A semantic representation of a Thing, retrieved from the Semantic Web.

RE_LABEL = <_sre.SRE_Pattern object>
add_query_endpoint(endpoint)

Any ontology object (here, a Thing) will be constructed through SPARQL queries send to some specified SPARQL endpoints. This function allows to add a single endpoint for the query.

Parameters:endpoint – An endpoint we wish to query for the construction of the current Thing.
add_query_endpoints(endpoints)

This function allows to add a list of endpoints to which the Thing queries will be sent.

Parameters:endpoints – A list of endpoints we wish to query for the construction of the current Thing.
attributes = None
create_triples_for_multiple_element(entity_name, entity_values)

If the variable ‘has_…’ (entity_name) is a list of values instead of a single value, we create an alternate triple. N.B.: If strict mode is enabled for the query, it will be disabled for this particular triples union because this would increase the size of the query too much.

Example:
>>> has_author = ["http://www.example.org/X", "http://www.example.org/Y"]
Will give the query triple:
`{ ?Book has_author <http://www.example.org/X>  } UNION { ?Book has_author <http://www.example.org/Y>  }.`
Parameters:
  • entity_name – name of the ‘has_…’ attribute
  • entity_values – values of the ‘has_…’ attribute
Returns:

create_triples_for_single_element(entity_name, entity_value, strict_mode)

If the variable ‘has_…’ (entity_name) is a single values instead of a list of value, we create a standard query triple.

Parameters:
  • entity_name – name of the ‘has_…’ attribute
  • entity_value – values of the ‘has_…’ attribute
  • strict_mode – is strict_mode (check RDF type) is enabled
Returns:

endpoints = None
find_more_about()

Deepens the search

format_value_for_RDF_compliance(value, default_value)

An element of a RDF triple must have a certain form depending on it type:

  • Integers are given as is
  • URL/URIs must be enclosed in angle brackets.
  • Normal strings must be enclosed in quotation marks
  • If the value is empty or None, then the default value is prefixed with a question mark and becomes a variable.
Parameters:
  • value – The value to be passed to a subject or object RDF slot.
  • default_value – The name of the variable in case the value is empty.
Returns:

A formatted value that will be a legal subject or object of an RDF triple.

get_attributes_with_keyword(keyword)

Returns the attributes whose predicates contains a keyword.

Example:
>>> my_thing.get_attributes_with_keyword('rdfs:')
will return all the attributes whose values' keys contains rdfs:
rdfs:label, rdf:type, subClassOf, rdfs:seeAlso ...
get_external_ids()

Get the external ids of the Thing.

get_uris()

Gets the URIs of the current object.

has_label = None
has_url = None
query(strict_mode=False, check_type=True, limit=1500)

Launches the query of the current object to the specified endpoints, given the query options to constraints the types of the RDF triples predicates and retrieve the results. The queries options relies on the dictionaries contained in pyneql/utils/vocabulary.py.

Parameters:check_type – Boolean.

Check the type of the object (e.g: Book, Person, Location,…) directly in the SPARQL queries. If True, the restriction of the object’s type is done in the query. For instance, if the object is a Book, activating type checking will build queries where the object to find (?Book) is constrained by an union of RDF triples checking that ?Book is a Book: […] { ?Book a fabio:Book  } UNION […] UNION { ?Book a schemaorg:Book  } .

Parameters:strict_mode – Boolean.

Check the type of the object’s attributes (e.g: label, first name,…) directly in the SPARQL queries. If True, the predicates of the triplet whose values are instantiated will have their types checked against the allowed types listed in self.voc_attributes.

Example:

We are looking for a Thing whose label is “አዲስ አበባ”.

  • Non strict mode will have its query restrained to elements satisfying

the triplet ?Thing ?has_label "አዲስ አበባ".. The predicate is left undetermined (?has_label is a variable).

  • In strict mode, we are strict about the types of predicates of the triplet.

For the current class, those predicates will be listed in self.voc_attributes['has_label'] and combined in the SPARQL query. Here, for the example, we set ‘has_label’ allowed the RDF predicates ‘rdfs:label’ and u’wdt:P1813’.

>>> print(self.voc_attributes['has_label'])
>>> [u'rdfs:label', u'wdt:P1813']

So in strict_mode, the query will be constrained to:

[…]{ ?Thing rdfs:label "አዲስ አበባ"  } UNION { ?Thing wdt:P1813 "አዲስ አበባ"  }.[…]

query_builder = None
query_limit = None

Module contents