<jupyter_start><jupyter_text>How to apply AMIE's rules to a KB  1. Read in the mined rules<jupyter_code>dataset_name: str = 'yago3_10'


amie_data_dir: str = os.path.join(data_dir, dataset_name, "amie_output_dir")
amie_rule_tsv_filename = os.path.join(
    amie_data_dir,
    f"amie_output_rules.tsv"
)


rule_wrapper_list: List[RuleWrapper] = get_amie_rules_from_rule_tsv_file(
    amie_rules_tsv_filename=amie_rule_tsv_filename)<jupyter_output><empty_output><jupyter_text>Select the first rule as an example<jupyter_code>from pprint import pprint

rule_wrapper: RuleWrapper = rule_wrapper_list[0]
pprint(rule_wrapper)<jupyter_output>{'Rule': 'dealswith(?a,?b)  => hasneighbor(?a,?b)', 'Head Coverage': 0.295495495, 'Std Confidence': 0.125960061, 'PCA Confidence': 0.16449348, 'Positive Examples': 164, 'Body size': 1302, 'PCA Body size': 997, 'Functional variable': '?b'}<jupyter_text>Load in a KB<jupyter_code>filename_ground_truth_dataset: str = os.path.join(
    data_dir, dataset_name, 'cleaned_csv', 'train.csv'
)
separator_ground_truth_dataset = "\t"

df_ground_truth: pd.DataFrame = get_df_ground_truth(filename_ground_truth_dataset, separator_ground_truth_dataset)
pandas_kb_wrapper = PandasKnowledgeBaseWrapper.create_from_full_data(df_full_data=df_ground_truth)<jupyter_output><empty_output><jupyter_text>Calculate the prediction cache for that rule on the KB<jupyter_code>o_df_cached_predictions: Optional[pd.DataFrame] = pandas_kb_wrapper.calculate_prediction_cache_for_rule(
    rule=rule_wrapper.rule
)
o_df_cached_predictions
o_df_cached_predictions.shape
df_based_rule_predictions: pd.DataFrame = pandas_kb_wrapper.get_predictions_for_rule(
    rule=rule_wrapper.rule)
df_based_rule_predictions.head()
df_based_rule_predictions.shape<jupyter_output><empty_output><jupyter_text>Rule metrics  CWA-based confidence / standard confidence<jupyter_code>from kbc_pul.rule_metrics.prediction_cache_rule_metrics.rule_cwa_and_pca_confidences_from_cached_predictions import \
    calculate_cwa_confidence_from_df_cache, calculate_pca_confidence_s_to_o_from_df_cache, \
    calculate_pca_confidence_o_to_s_from_df_cache

print(f"Rule: {rule_wrapper.rule}")

cwa_conf: Optional[float] = calculate_cwa_confidence_from_df_cache(o_df_cached_predictions)
print(f"CWA-based conf: {cwa_conf:0.3f}")
pca_conf_s_to_o: Optional[float] = calculate_pca_confidence_s_to_o_from_df_cache(o_df_cached_predictions)
print(f"PCA-based conf (subject-to-object): {pca_conf_s_to_o:0.3f}")
pca_conf_o_to_s: Optional[float] = calculate_pca_confidence_o_to_s_from_df_cache(o_df_cached_predictions)
print(f"PCA-based conf (object-to-subject): {pca_conf_o_to_s:0.3f}")

# # TRUE CONF
# true_conf: float = get_true_confidence_on_observed_data_using_cached_predictions(
#     df_cached_predictions=df_cached_predictions,
#     df_ground_truth_target_relation=df_ground_truth_target_relation,
#
# )
# rule_wrapper.o_true_confidence = true_conf
#
# # TRUE pair-positive confidence ('conf*') S->O
# true_pca_conf_subject_to_object: float = get_true_pca_confidence_on_observed_data_using_cached_predictions(
#     df_cached_predictions=df_cached_predictions,
#     true_entity_str_tuple_set=true_entity_sets.entity_pairs,
#     true_pca_non_target_entity_set=true_entity_sets.pca_subjects,
#     predict_object_entity=True,
# )
# rule_wrapper.o_true_pca_confidence_subject_to_object = true_pca_conf_subject_to_object
#
# # TRUE pair-positive confidence ('conf*') O->S
# true_pca_conf_object_to_subject: float = get_true_pca_confidence_on_observed_data_using_cached_predictions(
#     df_cached_predictions=df_cached_predictions,
#     true_entity_str_tuple_set=true_entity_sets.entity_pairs,
#     true_pca_non_target_entity_set=true_entity_sets.pca_objects,
#     predict_object_entity=False
# )

df_based_rule_predictions.shape

# # TRUE CONF
# true_conf: float = get_true_confidence_on_observed_data_using_cached_predictions(
#     df_cached_predictions=df_cached_predictions,
#     df_ground_truth_target_relation=df_ground_truth_target_relation,
#
# )
# rule_wrapper.o_true_confidence = true_conf
#
# # TRUE pair-positive confidence ('conf*') S->O
# true_pca_conf_subject_to_object: float = get_true_pca_confidence_on_observed_data_using_cached_predictions(
#     df_cached_predictions=df_cached_predictions,
#     true_entity_str_tuple_set=true_entity_sets.entity_pairs,
#     true_pca_non_target_entity_set=true_entity_sets.pca_subjects,
#     predict_object_entity=True,
# )
# rule_wrapper.o_true_pca_confidence_subject_to_object = true_pca_conf_subject_to_object
#
# # TRUE pair-positive confidence ('conf*') O->S
# true_pca_conf_object_to_subject: float = get_true_pca_confidence_on_observed_data_using_cached_predictions(
#     df_cached_predictions=df_cached_predictions,
#     true_entity_str_tuple_set=true_entity_sets.entity_pairs,
#     true_pca_non_target_entity_set=true_entity_sets.pca_objects,
#     predict_object_entity=False
# )

# # TRUE CONF
# true_conf: float = get_true_confidence_on_observed_data_using_cached_predictions(
#     df_cached_predictions=df_cached_predictions,
#     df_ground_truth_target_relation=df_ground_truth_target_relation,
#
# )
# rule_wrapper.o_true_confidence = true_conf
#
# # TRUE pair-positive confidence ('conf*') S->O
# true_pca_conf_subject_to_object: float = get_true_pca_confidence_on_observed_data_using_cached_predictions(
#     df_cached_predictions=df_cached_predictions,
#     true_entity_str_tuple_set=true_entity_sets.entity_pairs,
#     true_pca_non_target_entity_set=true_entity_sets.pca_subjects,
#     predict_object_entity=True,
# )
# rule_wrapper.o_true_pca_confidence_subject_to_object = true_pca_conf_subject_to_object
#
# # TRUE pair-positive confidence ('conf*') O->S
# true_pca_conf_object_to_subject: float = get_true_pca_confidence_on_observed_data_using_cached_predictions(
#     df_cached_predictions=df_cached_predictions,
#     true_entity_str_tuple_set=true_entity_sets.entity_pairs,
#     true_pca_non_target_entity_set=true_entity_sets.pca_objects,
#     predict_object_entity=False
# )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08278472679394339, 'fraction_numerical': 0.008229098090849244, 'mean_word_length': 9.01153212520593, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '576200', 'license': 'Apache-2.0', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2256, 'n_words': 242, 'path': 'notebooks/amie_general/3.how_to_apply_amie_rules_to_a_kb.ipynb', 'repo_name': 'ML-KULeuven/KBC-as-PU-Learning', 'size': '6076'}