<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Basic Examples of using Abydos
We start by importing the phonetic & distance modules of Abydos, along with Pandas.
Step1:  The we load some data into a DataFrame. In this case, we'll load the US Census surnames data ranked by frequency.
Step2:  We can create a dictionary of Soundex values mapping to all the surnames with the same Soundex code. These represent Soundex collisions (or blocking). Getting the basic Soundex value of a string is as simple as calling soundex() on it.
Step3:  Better yet, we can construct a Soundex() object to reuse for encoding multiple names.
Step4:  With this dictionary, we can retrieve all the names that map to the same Soundex value as, for example, the name Williamson.
Step5:  We can build up a DataFrame with some interesting information about these names. First, we'll just collect all the names in a column.
Step6:  To that, let's add a few distance measures.
Step7:  And finally, we'll add a few phonetic encodings.
Step8:  Let's check the row for WILLIAMSON.
Step9:  In addition to their Soundex collision, 7 names have matching first Double Metaphone encodings.
Step10:  28 have matching NYSIIS encodings.
Step11:  And 7 have matching first Alpha-SIS encodings.
Step12:  6 names match in all four of the phonetic algorithms considered here.
<ASSISTANT_TASK:>
Python Code:
from abydos.phonetic import *
from abydos.distance import *
import pandas as pd
Explanation: Basic Examples of using Abydos
We start by importing the phonetic & distance modules of Abydos, along with Pandas.
End of explanation
names = pd.read_csv('../tests/corpora/uscensus2000.csv',
                    comment='#', index_col=1, usecols=(0,1), keep_default_na=False)
names.head()
Explanation: The we load some data into a DataFrame. In this case, we'll load the US Census surnames data ranked by frequency.
End of explanation
soundex('WILLIAMSON')
Explanation: We can create a dictionary of Soundex values mapping to all the surnames with the same Soundex code. These represent Soundex collisions (or blocking). Getting the basic Soundex value of a string is as simple as calling soundex() on it.
End of explanation
sdx = Soundex()
reverse_soundex = {}
for name in names.name:
    encoded = sdx.encode(name)
    if encoded not in reverse_soundex:
        reverse_soundex[encoded] = set()
    reverse_soundex[encoded].add(name)
Explanation: Better yet, we can construct a Soundex() object to reuse for encoding multiple names.
End of explanation
reverse_soundex[soundex('WILLIAMSON')]
Explanation: With this dictionary, we can retrieve all the names that map to the same Soundex value as, for example, the name Williamson.
End of explanation
df = pd.DataFrame(sorted(reverse_soundex[soundex('WILLIAMSON')]), columns=['name'])
df
Explanation: We can build up a DataFrame with some interesting information about these names. First, we'll just collect all the names in a column.
End of explanation
# Levenshtein distance from 'WILLIAMSON'
lev = Levenshtein()
df['Levenshtein'] = df.name.apply(lambda name: lev.dist_abs('WILLIAMSON', name))
# Jaccard similarity on 2-grams
jac = Jaccard()
df['Jaccard'] = df.name.apply(lambda name: jac.sim('WILLIAMSON', name))
# Jaro-Winkler similarity
jw = JaroWinkler()
df['Jaro_Winkler'] = df.name.apply(lambda name: jw.sim('WILLIAMSON', name))
Explanation: To that, let's add a few distance measures.
End of explanation
# Double Metaphone (first code only)
dm = DoubleMetaphone()
df['Double_Metaphone'] = df.name.apply(lambda name: dm.encode(name)[0])
# NYSIIS
nysiis = NYSIIS()
df['NYSIIS'] = df.name.apply(lambda name: nysiis.encode(name))
# Alpha-SIS (first code only)
alphasis = AlphaSIS()
df['Alpha_SIS'] = df.name.apply(lambda name: alphasis.encode(name)[0])
df
Explanation: And finally, we'll add a few phonetic encodings.
End of explanation
df[df.name == 'WILLIAMSON']
Explanation: Let's check the row for WILLIAMSON.
End of explanation
df[df.Double_Metaphone == 'ALMSN']
Explanation: In addition to their Soundex collision, 7 names have matching first Double Metaphone encodings.
End of explanation
df[df.NYSIIS == 'WALANS']
Explanation: 28 have matching NYSIIS encodings.
End of explanation
df[df.Alpha_SIS == '45302000000000']
Explanation: And 7 have matching first Alpha-SIS encodings.
End of explanation
df[(df.Alpha_SIS == '45302000000000') & (df.NYSIIS == 'WALANS') &
   (df.Double_Metaphone == 'ALMSN')]
Explanation: 6 names match in all four of the phonetic algorithms considered here.
End of explanation
<END_TASK>