Reference

c_clause Handlers

class c_clause.Loader
get_entity_index(self: c_clause.Loader) dict[str, int]
get_relation_index(self: c_clause.Loader) dict[str, int]
get_rules(self: c_clause.Loader) list[str]

Returns rules after loading. Returns a list of strings: ‘num_predstsupporttconftrulestring’.

load_data(*args, **kwargs)

Overloaded function.

  1. load_data(self: c_clause.Loader, data: str, filter: str = ‘’, target: str = ‘’) -> None

  2. load_data(self: c_clause.Loader, data: list[Annotated[list[str], FixedSize(3)]], filter: list[Annotated[list[str], FixedSize(3)]] = [], target: list[Annotated[list[str], FixedSize(3)]] = []) -> None

  3. load_data(self: c_clause.Loader, data: list[Annotated[list[int], FixedSize(3)]], filter: list[Annotated[list[int], FixedSize(3)]] = [], target: list[Annotated[list[int], FixedSize(3)]] = []) -> None

load_rules(*args, **kwargs)

Overloaded function.

  1. load_rules(self: c_clause.Loader, rules: str) -> None

  2. load_rules(self: c_clause.Loader, rules: list[str]) -> None

  3. load_rules(self: c_clause.Loader, rules: list[str], stats: list[tuple[int, int]]) -> None

replace_ent_strings(self: c_clause.Loader, new_tokens: dict[str, str]) None
replace_rel_strings(self: c_clause.Loader, new_tokens: dict[str, str]) None
rule_index(self: c_clause.Loader) list[str]
set_entity_index(self: c_clause.Loader, index: list[str]) None
set_options(self: c_clause.Loader, options: dict[str, str]) None
set_relation_index(self: c_clause.Loader, index: list[str]) None
update_rules(self: c_clause.Loader) None

Updates rules according to new options set with loader.set_options(new_options). n If, e.g., in new_options load_b_rules=False then these rules will not be used in rule application when the loader is passed to any handler. It can be used for all the options of the loader. Note, however, the global rule index obtained with loader.get_rules() maintains the same. Each rule keeps its original idx from initially loading the rules.

write_rules(self: c_clause.Loader, path: str) None

Writes rules after loading. Can be used to store subsets, e.g., load rules ignoring B-rules and then write.

class c_clause.QAHandler
calculate_answers(*args, **kwargs)

Overloaded function.

  1. calculate_answers(self: c_clause.QAHandler, queries: list[tuple[int, int]], loader: Loader, direction: str) -> None

  2. calculate_answers(self: c_clause.QAHandler, queries: list[tuple[str, str]], loader: Loader, direction: str) -> None

  3. calculate_answers(self: c_clause.QAHandler, queries: str, loader: Loader, direction: str) -> None

get_answers(self: c_clause.QAHandler, as_string: bool) object
get_rules(self: c_clause.QAHandler, as_string: bool) object
set_options(*args, **kwargs)

Overloaded function.

  1. set_options(self: c_clause.QAHandler, options: dict[str, str]) -> None

  2. set_options(self: c_clause.QAHandler, arg0: dict[str, str]) -> None

write_answers(self: c_clause.QAHandler, path: str, as_string: bool) None
write_rules(self: c_clause.QAHandler, path: str, as_string: bool) None
class c_clause.RankingHandler
calculate_ranking(self: c_clause.RankingHandler, loader: Loader) None
get_ranking(self: c_clause.RankingHandler, direction: str, as_string: bool) object
get_rules(self: c_clause.RankingHandler, direction: str, as_string: bool) object
set_options(self: c_clause.RankingHandler, options: dict[str, str]) None
write_ranking(self: c_clause.RankingHandler, path: str, loader: Loader) None
write_rules(self: c_clause.RankingHandler, path: str, loader: Loader, direction: str, as_string: bool) None
class c_clause.RulesHandler
calculate_predictions(*args, **kwargs)

Overloaded function.

  1. calculate_predictions(self: c_clause.RulesHandler, rules: list[str], loader: Loader) -> None

    Given a list of string rules calculate predictions and rule statistics (num_pred, num_true_pred). Option parameters can specify if predictions are stored or if statistics are stored. If only statistics need to be computed, turn off collect_predictions option for efficiency.

  2. calculate_predictions(self: c_clause.RulesHandler, rules: str, loader: Loader) -> None

    Given a list of rules in a file (list of rules or AnyBURL format) calculate predictions and rule statistics (num_pred, num_true_pred). Option parameters can specify if predictions are stored or if statistics are stored. If only statistics need to be computed, turn off collect_predictions option for efficiency.

get_predictions(self: c_clause.RulesHandler, as_string: bool) object
get_statistics(self: c_clause.RulesHandler) list[Annotated[list[int], FixedSize(2)]]
set_options(self: c_clause.RulesHandler, options: dict[str, str]) None
write_predictions(self: c_clause.RulesHandler, path: str, flat: bool = True, as_string: bool = True) None
write_statistics(self: c_clause.RulesHandler, path: str) None
class c_clause.PredictionHandler
calculate_scores(*args, **kwargs)

Overloaded function.

  1. calculate_scores(self: c_clause.PredictionHandler, triples: str, loader: c_clause.Loader) -> None

    Takes as input np.array/list of idx’s or a list of string/token triples (tuples or lists) or a path to a file containing tab separarated string/token triples. Entities and relation tokens must be known, e.g., they must have be loaded in some triples in the Loader.

  2. calculate_scores(self: c_clause.PredictionHandler, triples: list[Annotated[list[int], FixedSize(3)]], loader: c_clause.Loader) -> None

  3. calculate_scores(self: c_clause.PredictionHandler, triples: list[Annotated[list[str], FixedSize(3)]], loader: c_clause.Loader) -> None

get_explanations(self: c_clause.PredictionHandler, as_string: bool) object
get_scores(self: c_clause.PredictionHandler, as_string: bool) object
set_options(self: c_clause.PredictionHandler, options: dict[str, str]) None
write_explanations(self: c_clause.PredictionHandler, path: str, as_string: bool) None
write_scores(self: c_clause.PredictionHandler, path: str, as_string: bool) None

Rule Learning

class clause.Learner(options)

Learns rules with Amie or AnyBURL.

learn_rules(path_data, path_output)

Starts rule learning.

Parameters:
  • path_data – Path to the KG where rules are learned, e.g., train.txt.

  • path_output – Output file where rules are stored.

clause Utilities

class clause.RuleTranslator(idx_to_ent, idx_to_rel)

Translates rules represented with integer idx’s to string format.

translate_b_rules(relations, directions)

Translates B-rules in idx format to string format.

param: (list[list[int]]) relations: head and body relations param: (list[list[bool]]) directions: provides the direction within an atom. First element must be 1.

translate_c_rules(relations, directions, constants, leftC)

Translates Uc-rules in idx format to string format.

Parameters:
  • relations ((list[list[int]])) – head and body relations

  • directions ((list[list[bool]])) – provides the direction within an atom. First element must be 1.

  • constants ((list[list[int, int]])) – each list contains 2 entity idx’s the first el corresponds to head constant, second to body constant

  • leftC ((list[bool])) – denotes if the left (X) or right (Y) variable of the head is grounded

translate_d_rules(relations, directions, constants, leftC)

Translates Ud-rules from idx format to string format.

Parameters:
  • relations ((list[list[int]])) – head and body relations

  • directions ((list[list[bool]])) – provides the direction within an atom. First element must be 1.

  • constants ((list[int])) – head constants

  • leftC ((list[bool])) – denotes if the left (X) or right (Y) variable of the head is grounded

translate_u_xxc_rules(relations, directions, constants)

Translates u_xxc rules from idx format to string format.

Parameters:
  • relations ((list[list[int,int]])) – head and body relations

  • directions ((list[list[bool, bool]])) – first bool must be true, second is body atom direction

  • constants ((list[int])) – the body constants

translate_u_xxd_rules(relations, directions)

Translates u_xxd rules from idx format to string format.

Parameters:
  • relations ((list[list[int,int]])) – head and body relations

  • directions ((list[list[bool,bool]])) – first bool must be true, second is body atom direction

translate_z_rules(relations, constants, leftC)

Translates zero-rules from idx format to string format.

Parameters:
  • relations ((list[int])) – head relations

  • constants ((list[int])) – head constants

  • leftC ((list[bool])) – denotes if the left (X) or right (Y) variable of the head is grounded

class clause.Options(path=None)
flat(key=None)

Returns a flat dictionary representation (or value) of the dictionary referred to by the given key.

All nested / structured keys below that key are mapped to strings separated by dots ‘.’ . If a key is not specified all options are returned in the flat dictionary representation.

flatS(key=None)

Returns a flat dictionary representation (or value) of the dictionary referred to by the given key, where all values are converted to strings.

All nested / structured keys below that key are mapped to strings separated by dots ‘.’. If a key is not specified all options are returned in the flat dictionary representation.

get(key, as_string=True)

Returns a flat dictionary or a value for a given key. Args: key: The key for which to retrieve the value. as_string (bool): If True, returns the value as a string; otherwise, the type is preserved.

set(param, value)

Overwrites an entry in the options.

They key is specified in e.g. the format ‘learner.mode’. A value can be anything (that has a string representation). It is not possible to add new keys that do not exist in the default options. An exception is the parent key “raw”. Under this key, new key, val pairs can be set. For instance, options.set(“learning.amie.raw.notexistingkey”, val).

write(file_path)

Writes user specified options to yaml file (ignores unchanged default options).

class clause.TripleSet(path, index=None, encod=None)

A triple set is a list of triples with several index structures to support fast graph traversal of the knowledge graph defined by the triple set.

get_1to1_score(rel)
is_known(sub, rel, obj)

Used to check in constant time if a triple (index format) exists within the dataset, i.e. if the triple is known.

is_true(sub_token, rel_token, obj_token)

Used to check in constant time if a triple (token format) exists within the dataset, i.e. if the triple is known.

to_list()

Outputs a list of string striples.

A string triple is a list or tuple with three string elements [“head”, “relation”, “tail”].

write_masked(outpath, replace_non_alpha_numeric=True)
class clause.Ranking(filepath=None, contains_confidences=True, k=100)
add_filter_set(tripleset)
compute_scores(triples, head_direction=True, tail_direction=True)
convert_handler_ranking(head_ranking, tail_ranking, testset, target_triples=None)

This function converts the internal rankings computed by PyClause into a ranking that can be evaluted using the standard evaluation protocol. It is assumed that the rankings of the ranking handler are already filtered against valid and train. The additional filtering against the testset is done in this method.

This function takes as arguments a head and a tail ranking and a testset. It is possible to additionally set a list of traget_triples. If this parameter is not set, then the triples from the testset are used as target_triples. The traget_triples are used as evaluation queries for which the ranking is generated. They are usually a subset or all of the triples of the testset.

The head or the tail ranking can be set to None. In this case empty lists of predictions for the head/tail cases are created within the ranking.

diff(other)

This diff has been written for checking the correctness of the conversion from internal to python ranking. It point to the first difference between self and other (another ranking).

This method might be extended later.

get_confidences_from_line(rline)
get_head_candidates(triple)
get_results_from_line(rline)
get_tail_candidates(triple)
write(filepath)