Quickstart

After installing PyClause with either method you should be able to import the clause and c_clause packages.

Run the following code from anywhere, it shows an example of answering the queries “Anna speaks ? (what)” and “? (who) speaks English” after a small KG and ruleset is loaded.

 1from c_clause import QAHandler, Loader
 2from clause import Options
 3
 4# ***Example for Query Answering***
 5
 6# define a knowledge graph
 7# alternatively, specify file path or use arrays + indices
 8data = [
 9   ("anna", "livesIn", "london"),
10   ("anna", "learns", "english"),
11   ("bernd", "speaks", "french")
12]
13# define rules, or specify file path
14rules = [
15   "speaks(X,Y) <= learns(X,Y)",
16   "speaks(X,english) <= livesIn(X,london)",
17   "speaks(X,english) <= speaks(X,A)"
18]
19# define rule stats: num_preds, support
20stats = [
21   [20, 10],
22   [40, 35],
23   [50, 5],
24]
25# define options, handlers and load data
26opts = Options()
27opts.set("qa_handler.aggregation_function", "noisyor")
28
29loader = Loader(options=opts.get("loader"))
30loader.load_data(data)
31loader.load_rules(rules=rules, stats=stats)
32
33qa = QAHandler(options=opts.get("qa_handler"))
34# define query: (anna, speaks, ?); alternatively, use indices
35queries = [("anna", "speaks")]
36qa.calculate_answers(queries=queries, loader=loader, direction="tail")
37# outputs [("english", 0.867 )]
38print(qa.get_answers(as_string=True)[0])
39
40# define query: (?, speaks, english); alternatively, use indices
41queries = [("english", "speaks")]
42qa.calculate_answers(queries=queries, loader=loader, direction="head")
43# outputs [('anna', 0.867), ('bernd', 0.001)]
44print(qa.get_answers(as_string=True)[0])