kif-lib 0.13.0
A knowledge integration framework based on Wikidata
License: Apache-2.0
Requires Python: >=3.9

![KIF](docs/img/kif-b.svg#gh-light-mode-only)
![KIF](docs/img/kif-w.svg#gh-dark-mode-only)

---

# Knowledge Integration Framework

KIF is a Python framework for knowledge integration from [IBM Research](https://research.ibm.com/).

It is based on [Wikidata](https://www.wikidata.org/) and licensed under the open-source
[Apache-2.0
license](https://raw.githubusercontent.com/IBM/kif/refs/heads/main/LICENSE).

First time here? Check out the [tutorial](https://ibm.github.io/kif/tutorial/).

Looking for the sources? See the [GitHub repository](https://github.com/IBM/kif).

[![Supported Python Versions](https://img.shields.io/pypi/pyversions/kif_lib)](https://pypi.org/project/kif_lib/) [![PyPI version](https://badge.fury.io/py/kif_lib.svg)](https://badge.fury.io/py/kif_lib) [![Downloads](https://pepy.tech/badge/kif_lib/month)](https://pepy.tech/project/kif_lib)

---

## Hello world!

Install KIF using pip:

```shell
$ pip install kif-lib
```

Use KIF to query [Wikidata](https://www.wikidata.org/):

```pycon
>>> from kif_lib import Store
>>> from kif_lib.vocabulary import wd
>>> kb = Store('wikidata')
>>> next(kb.filter(subject=wd.Alan_Turing, property=wd.doctoral_advisor))
Statement(Item(IRI('http://www.wikidata.org/entity/Q7251')), ValueSnak(...))
```

Or, via KIF CLI (the command-line interface):

```shell
$ pip install kif-lib[cli]    # KIF CLI is an optional dependency
$ kif filter --subject=wd.Alan_Turing --property=wd.doctoral_advisor
```

> (**Statement** (**Item** [Alan Turing](http://www.wikidata.org/entity/Q7251)) (**ValueSnak** (**Property** [doctoral advisor](http://www.wikidata.org/entity/P184)) (**Item** [Alonzo Church](http://www.wikidata.org/entity/Q92741))))

KIF can also be used query other knowledge sources.  Here is a similar query over [DBpedia](https://www.dbpedia.org/) (notice the `-s dbpedia` switch):

```shell
$ kif filter -s dbpedia --subject=db.Alan_Turing --property=wd.doctoral_advisor
```

> (**Statement** (**Item** [dbr:Alan_Turing](http://dbpedia.org/resource/Alan_Turing)) (**ValueSnak** (**Property** [dbo:doctoralAdvisor](http://dbpedia.org/ontology/doctoralAdvisor)) (**Item** [dbr:Alonzo_Church](http://dbpedia.org/resource/Alonzo_Church))))

The result is a stream of Wikidata-like statements containing DBpedia entities.

## KIF in a nutshell

KIF is a knowledge integration framework based on [Wikidata](https://www.wikidata.org/).  The idea behind it is to use Wikidata to standardize the syntax and (whenever possible) the vocabulary of the integrated knowledge sources.  Users can then query the sources through filter patterns described in terms of the [Wikidata data model](https://www.wikidata.org/wiki/Wikidata:Data_model).

The integration done by KIF is *virtual* in the sense that syntax and vocabulary translations happen dynamically (at query time) and are guided by user-provided mappings.  KIF comes with built-in mappings for [Wikidata](https://www.wikidata.org/), [DBpedia](https://www.dbpedia.org/), [FactGrid](https://database.factgrid.de/), [PubChem](https://pubchem.ncbi.nlm.nih.gov/), and [UniProt](https://www.uniprot.org/), among others.  New mappings can be added programmatically.

### Highlights

* KIF allows one to query knowledge sources as if they were Wikidata.

* KIF queries are written as simple, high-level filters using entities of the [Wikidata data model](https://www.wikidata.org/wiki/Wikidata:Data_model), such as items, properties, quantities, snaks, statements, etc.

* KIF can be used to query Wikidata itself or other knowledge sources, provided proper mappings are given.

* KIF can run queries over local RDF data using [RDFLib](https://rdflib.readthedocs.io/en/stable/), [Apache Jena](https://jena.apache.org/), [QLever](https://qlever.dev/), or [RDFox](https://www.oxfordsemantic.tech/rdfox).

* KIF has full support for Python's [asyncio](https://docs.python.org/3/library/asyncio.html).  KIF async API can be used run queries asynchronously, without blocking waiting on their results.


## Installation

To install KIF, use:

```shell
$ pip install kif-lib
```

To include KIF CLI, use:

```shell
$ pip install kif-lib[cli]
```

To include all extras, use:

```shell
$ pip install kif-lib[extra]
```

## Documentation

KIF documentation is available at [https://ibm.github.io/kif/](https://ibm.github.io/kif/).

For a primer on KIF, see the [tutorial](https://ibm.github.io/kif/tutorial/).

## Dependencies

Required:

* [httpx](https://www.python-httpx.org/) - HTTP support.
* [lark](https://lark-parser.readthedocs.io/en/stable/) - Parsing.
* [more_itertools](https://more-itertools.readthedocs.io/en/stable/index.html) - Extra itertools.
* [networkx](https://networkx.org/) - Graph algorithms.
* [rdflib](https://rdflib.readthedocs.io/en/stable/) - RDF support.
* [typing-extensions](https://typing-extensions.readthedocs.io/en/latest/) - Typing backports.

KIF CLI (optional):

* [click](https://click.palletsprojects.com/en/stable/) - Option parsing. *(Optional, with `kif-lib[cli]`)*
* [rich](https://rich.readthedocs.io/en/latest/introduction.html) - Rich terminal support.  *(Optional, with `kif-lib[cli]`)*

Extra (optional):

* [graphviz](https://graphviz.readthedocs.io/en/stable/manual.html) - Graph drawing. *(Optional, with `kif-lib[extra]`)*
* [jpype1](https://jpype.readthedocs.io/en/latest/) - Java support. *(Optional, with `kif-lib[extra]`)*
* [pandas](https://pandas.pydata.org/) - CSV/DataFrame support. *(Optional, with `kif-lib[extra]`)*
* [psutil](https://psutil.readthedocs.io/en/latest/) - Process information. *(Optional, with `kif-lib[extra]`)*

## Citation

Guilherme Lima, João M. B. Rodrigues, Marcelo Machado, Elton Soares, Sandro R. Fiorini, Raphael Thiago, Leonardo G. Azevedo, Viviane T. da Silva, Renato Cerqueira.  2024.  ["KIF: A Wikidata-Based Framework for Integrating Heterogeneous Knowledge Sources"](https://arxiv.org/abs/2403.10304), arXiv:2403.10304, 2024.