<jupyter_start><jupyter_text>Introduction Blocking is typically done to reduce the number of tuple pairs considered for matching. There are several blocking methods proposed. The *py_entitymatching* package supports a subset of such blocking methods (ref to what is supported). One such supported blocker is attribute equivalence blocker. This IPython notebook illustrates how to perform blocking using attribute equivalence blocker. First, we need to import *py_entitymatching* package and other libraries as follows:<jupyter_code>%load_ext autotime
# Import py_entitymatching package
import py_entitymatching as em
import os
import pandas as pd<jupyter_output><empty_output><jupyter_text>Then, read the input tablse from the datasets directory<jupyter_code># 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')
A.head()
B.head()<jupyter_output><empty_output><jupyter_text>Different Ways to Block Using Attribute Equivalence BlockerOnce the tables are read, we can do blocking using attribute equivalence blocker. There are three different ways to do attribute equivalence blocking:1. Block two tables to produce a candidate set of tuple pairs.2. Block a candidate set of tuple pairs to typically produce a reduced candidate set of tuple pairs.3. Block two tuples to check if a tuple pair would get blocked.  Block Tables to Produce a Candidate Set of Tuple Pairs<jupyter_code># Instantiate attribute equivalence blocker object
ab = em.AttrEquivalenceBlocker()<jupyter_output><empty_output><jupyter_text>For the given two tables, we will assume that two persons with different `zipcode` values do not refer to the same real world person. So, we apply attribute equivalence blocking on `zipcode`. That is, we block all the tuple pairs that have different zipcodes.<jupyter_code># Use block_tables to apply blocking over two input tables.
C1 = ab.block_tables(A, B, 
                    l_block_attr='zipcode', r_block_attr='zipcode', 
                    l_output_attrs=['name', 'birth_year', 'zipcode'],
                    r_output_attrs=['name', 'birth_year', 'zipcode'],
                    l_output_prefix='l_', r_output_prefix='r_')
# Display the candidate set of tuple pairs
C1.head()<jupyter_output><empty_output><jupyter_text>Note that the tuple pairs in the candidate set have the same zipcode. The attributes included in the candidate set are based on l_output_attrs and r_output_attrs mentioned in block_tables command (the key columns are included by default). Specifically, the list of attributes mentioned in l_output_attrs are picked from table A and the list of attributes mentioned in r_output_attrs are picked from table B. The attributes in the candidate set are prefixed based on l_output_prefix and r_ouptut_prefix parameter values mentioned in block_tables command.<jupyter_code># Show the metadata of C1
em.show_properties(C1)
id(A), id(B)<jupyter_output><empty_output><jupyter_text>Note that the metadata of C1 includes key, foreign key to the left and right tables (i.e A and B) and pointers to left and right tables.  Handling Missing Values If the input tuples have missing values in the blocking attribute, then they are ignored by default. This is because, including all possible tuple pairs with missing values can significantly increase the size of the candidate set. But if you want to include them, then you can set `allow_missing` paramater to be True.<jupyter_code># Introduce some missing values
A1 = em.read_csv_metadata(path_A, key='ID')
A1.ix[0, 'zipcode'] = pd.np.NaN
A1.ix[0, 'birth_year'] = pd.np.NaN
A1
# Use block_tables to apply blocking over two input tables.
C2 = ab.block_tables(A1, B, 
                    l_block_attr='zipcode', r_block_attr='zipcode', 
                    l_output_attrs=['name', 'birth_year', 'zipcode'],
                    r_output_attrs=['name', 'birth_year', 'zipcode'],
                    l_output_prefix='l_', r_output_prefix='r_', 
                    allow_missing=True) # setting allow_missing parameter to True
len(C1), len(C2)
C2<jupyter_output><empty_output><jupyter_text>The candidate set C2 includes all possible tuple pairs with missing values.  Block a Candidate Set of Tuple Pairs In the above, we see that the candidate set produced after blocking over input tables include tuple pairs that have different birth years. We will assume that two persons with different birth years cannot refer to the same person. So, we block the candidate set of tuple pairs on `birth_year`. That is, we block all the tuple pairs that have different birth years.<jupyter_code># Instantiate Attr. Equivalence Blocker
ab = em.AttrEquivalenceBlocker()
# Use block_tables to apply blocking over two input tables.
C3 = ab.block_candset(C1, l_block_attr='birth_year', r_block_attr='birth_year')
C3.head()<jupyter_output><empty_output><jupyter_text>Note that, the tuple pairs in the resulting candidate set have the same birth year. The attributes included in the resulting candidate set are based on the input candidate set (i.e the same attributes are retained).<jupyter_code># Show the metadata of C1
em.show_properties(C3)
id(A), id(B)<jupyter_output><empty_output><jupyter_text>As we saw earlier the metadata of C3 includes the same metadata as C1. That is, it includes key, foreign key to the left and right tables (i.e A and B) and pointers to left and right tables.  Handling Missing Values If the tuple pairs included in the candidate set have missing values in the blocking attribute, then they are ignored by default. This is because, including all possible tuple pairs with missing values can significantly increase the size of the candidate set. But if you want to include them, then you can set `allow_missing` paramater to be True.<jupyter_code># Display C2 (got by blocking over A1 and B)
C2
em.show_properties(C2)
em.show_properties(A1)<jupyter_output>id: 4565680928
key: ID<jupyter_text>We see that `A1` is the left table to `C2`.<jupyter_code>A1.head()
C4 = ab.block_candset(C2, l_block_attr='birth_year', r_block_attr='birth_year', allow_missing=False)
C4
# Set allow_missing to True
C5 = ab.block_candset(C2, l_block_attr='birth_year', r_block_attr='birth_year', allow_missing=True) 
len(C4), len(C5)
C5<jupyter_output><empty_output><jupyter_text>Block Two tuples To Check If a Tuple Pair Would Get Blocked We can apply attribute equivalence blocking to a tuple pair to check if it is going to get blocked. For example, we can check if the first tuple from A and B will get blocked if we block on `zipcode`.<jupyter_code># Display the first tuple from table A
A.ix[[0]]
# Display the first tuple from table B
B.ix[[0]]
# Instantiate Attr. Equivalence Blocker
ab = em.AttrEquivalenceBlocker()

# Apply blocking to a tuple pair from the input tables on zipcode and get blocking status
status = ab.block_tuples(A.ix[0], B.ix[0], l_block_attr='zipcode', r_block_attr='zipcode')

# Print the blocking status
print(status)<jupyter_output>False
{'fraction_non_alphanumeric': 0.06904170118751726, 'fraction_numerical': 0.007870753935376968, 'mean_word_length': 5.030807660283098, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '362822', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2119, 'n_tokens_neox': 2044, 'n_words': 891, 'path': 'notebooks/guides/step_wise_em_guides/Performing Blocking Using Built-In Blockers (Attr. Equivalence Blocker).ipynb', 'repo_name': 'kvpradap/py_entitymatching', 'size': '7242'}