# Import py_entitymatching package
import py_entitymatching as em
import os
import pandas as pd

"""
Explanation: Introduction
This IPython notebook illustrates how to debug blocker output.
First, we need to import py_entitymatching package and other libraries as follows:
End of explanation
"""


# Get the datasets directory
datasets_dir = em.get_install_path() + os.sep + 'datasets'

# Get the paths of the input tables
path_A = datasets_dir + os.sep + 'person_table_A.csv'
path_B = datasets_dir + os.sep + 'person_table_B.csv'

# Read the CSV files and set 'ID' as the key attribute
A = em.read_csv_metadata(path_A, key='ID')
B = em.read_csv_metadata(path_B, key='ID')

"""
Explanation: Then, read the (sample) input tables for blocking purposes.
End of explanation
"""


# First get features that can be used
feature_table = em.get_features_for_blocking(A, B, validate_inferred_attr_types=False)

# Create rule-based blocker
rb = em.RuleBasedBlocker()
# Add rule : block tuples if name_name_lev(ltuple, rtuple) < 0.8
rb.add_rule(['name_name_lev_sim(ltuple, rtuple) < 0.8'], feature_table)

E = rb.block_tables(A, B, l_output_attrs=['name'], r_output_attrs=['name'])

E

dbg = em.debug_blocker(E, A, B, output_size=5)

dbg

# Create rule-based blocker --- NOTE: we are creating a new blocker !!!
rb = em.RuleBasedBlocker()
# Add rule : block tuples if name_name_lev_sim(ltuple, rtuple) < 0.4
rb.add_rule(['name_name_lev_sim(ltuple, rtuple) < 0.4'], feature_table)

E = rb.block_tables(A, B, l_output_attrs=['name'], r_output_attrs=['name'])

E

dbg = em.debug_blocker(E, A, B, output_size=5)

dbg

"""
Explanation: Debugging Blocker Output
First, block using rule-based blocker
End of explanation
"""


