Home

Awesome

<img src="https://raw.githubusercontent.com/symbolic-kg/PyClause/master/docs/logo/PyClause/proc/PyClause.png" width="171" height="57" style="margin-right: 10px;" alt="PyClause">

PyClause is a library for easy and efficient usage and learning of symbolic knowledge graph rules of the following form.


citizenOf(X,Y) <= bornIn(X,A), locatedIn(A,Y)
speaks(X,english) <= lives(X,london)

PyClause provides a wide range of rule application functionalities such as query answering and triple scoring. Rules from any system can be used as long as their syntax aligns. PyClause also provides wrappers to directly use and configure the rule miners AMIE and AnyBURL from Python.

Table of contents

  1. Docs and Examples
  2. Installation
    1. Install from codebase
    2. Install as a package
  3. Quickstart
  4. Knowledge Graph Completion Results
  5. Run tests
  6. How to cite
  7. Colophon

Documentation and Examples

The documentation of the library, rule syntax, data downloads, and a tutorial about rules can be found here. <br> All usable default options for the configuration files can be found in clause/config-default.yaml. <br> We provide runnable examples for different library features in examples. <br>

Installation

PyClause only uses a few lightweight Python dependencies and runs under Windows and Linux systems. Linux users need to have a c++ (14) compiler installed. Windows users need to have C++ build tools installed. PyClause requires Microsoft Visual C++ 14.0 or newer. <br>

We recommend using a fresh Conda environment with Python 3.7+.

1) Install from codebase

For running our examples and using our data directories or working with the code you can install in editable mode:

git clone https://github.com/symbolic-kg/PyClause
cd PyClause
pip install -e .

2) Install as a package

For using PyClause in your own Python project and as a dependency. When running our examples, you have to set your own correct paths to data and rule files. <br> Install:

pip install git+https://github.com/symbolic-kg/PyClause.git

As dependency:

## in setup.py

setup(
  name             = "MyProject",
  install_requires = ["PyClause @ git+https://github.com/symbolic-kg/PyClause.git#egg=PyClause"],
  python_requires  = ">=3.7"
)

Quickstart

After installing PyClause with any of the two options, run the following code from anywhere.

from c_clause import QAHandler, Loader
from clause import Options

# ***Example for Query Answering***

# define a knowledge graph
# alternatively, specify file path or use arrays + indices
data = [
    ("anna", "livesIn", "london"),
    ("anna", "learns", "english"),
    ("bernd", "speaks", "french")
]
# define rules, or specify file path
rules = [
     "speaks(X,Y) <= learns(X,Y)",
     "speaks(X,english) <= livesIn(X,london)",
     "speaks(X,english) <= speaks(X,A)"
]
# define rule stats: num_preds, support
stats = [
    [20, 10],
    [40, 35],
    [50, 5],
]
# define options, handlers and load data
opts = Options()
opts.set("qa_handler.aggregation_function", "noisyor")

loader = Loader(options=opts.get("loader"))
loader.load_data(data)
loader.load_rules(rules=rules, stats=stats)

qa = QAHandler(options=opts.get("qa_handler"))
# define query: (anna, speaks, ?); alternatively, use indices
queries = [("anna", "speaks")]
qa.calculate_answers(queries=queries, loader=loader, direction="tail")
# outputs [("english", 0.867 )] 
print(qa.get_answers(as_string=True)[0])

# define query: (?, speaks, english); alternatively, use indices
queries = [("english", "speaks")]
qa.calculate_answers(queries=queries, loader=loader, direction="head")
# outputs [('anna', 0.867), ('bernd', 0.001)] 
print(qa.get_answers(as_string=True)[0])

Knowledge Graph Completion Results

In the following tables, we present (filtered) ranking-based evaluation results for the test sets of various datasets calculated with PyClause. We show results for different rule confidence aggregation functions. They are are calculated with the RankingHandler under commit 75daaba. When the aggregation function is learned, better results can be achieved [1][2].

WN18RR

AggregationMRRHits@1Hits@10Config
Maxplus0.49570.45680.5712cfg-maxplus.yaml
Noisy-or top-50.49750.45760.5750cfg-no-top-5.yaml
Tuned0.49460.45600.5689*

wn18rr-ruleset

FB15k-237

AggregationMRRHits@1Hits@10Config
Maxplus0.33180.24660.5055cfg-maxplus.yaml
Noisy-or top-50.34840.26120.5235cfg-no-top-5.yaml
Tuned0.34760.26360.5165*

fb15k-237-ruleset

Yago3-10

AggregationMRRHits@1Hits@10Config
Maxplus0.56750.49950.6944cfg-maxplus.yaml
Noisy-or top-50.56250.48950.6972cfg-no-top-5.yaml
Tuned0.58210.51570.7025*

yago3-10-ruleset

Wikidata5M

AggregationMRRHits@1Hits@10Config
Maxplus0.35350.31270.4328cfg-maxplus.yaml
Noisy-or top-50.35610.31450.4371cfg-no-top-5.yaml
Tuned0.35730.31540.4374*

wikidata5m-ruleset

CoDEx-M

AggregationMRRHits@1Hits@10Config
Maxplus0.31950.24880.4559cfg-maxplus.yaml
Noisy-or top-50.31990.24440.4675cfg-no-top-5.yaml
Tuned0.32400.25560.4587*

codex-m-ruleset

<hr/>

*In this approach, for each relation and query direction (head or tail), we searched on the validation set for the best setting of the num_unseen parameter for the rules and the aggregation function. We searched over num_unseen in {0,1,5,10,25,50,100,500,1000,5000,10000} and aggregation_function in {maxplus, noisyor}. We then applied the best setting on the test sets.

Run tests (for developers, linux)

Download this, unpack, and put the folders into data/ <br> Then, from the base directory run pytest -s.

How to cite

If you use PyClause, please cite <br>

@inproceedings{betz2024pyclause,
  title={PyClause-Simple and Efficient Rule Handling for Knowledge Graphs},
  author={Betz, Patrick and Galarraga, Luis and Ott, Simon and Meilicke, Christian and Suchanek, Fabian M and Stuckenschmidt, Heiner},
  booktitle={IJCAI, demo track},
  year={2024},
  publisher={Ijcai.org}
}

If you use AMIE or AnyBURL from within PyClause, please refer to the publications as given on the respective webpages.

Colophon

Have you spotted it? <br>

<img src="https://raw.githubusercontent.com/symbolic-kg/PyClause/master/docs/logo/PyClause/proc/PyClause-begriffsschrift-transparent.png" width="214" height="75" style="margin-right: 10px;">

The PyClause logo is a reminiscence of Freges Begriffsschrift, which has been published in the year 1879. Even though Frege is not well known in computer science, in his book Begriffsschrift he developed a calculus using an uncommon notation, which is essentially second-order logic with identity (an extension of first-order logic). <br>

The blue lines in the logo are Freges way to express an implication. If we would write A at the end of the first line and B at the end of the second line, this would correspond to a logical rule A <= B, which can again be understood as a clause with a positive and a negative literal. The round dint at the beginning is Freges way to express universal quantification. For more details we refer to the Wikipedia article or the book from 1879 entitled Begriffsschrift.