In [135]:
import re
import hashlib
import pandas as pd
import os
import random
import matplotlib.pyplot as plt
import math as math

In [136]:
cwd = os.getcwd()
fileName = 'assets/training/CSV_Database_of_First_Names.csv'
filePath = os.path.join(cwd, fileName)
print(cwd, filePath)
df = pd.read_csv(filePath,header=0)


C:\Users\MDPha\Documents\RPL\SortingHat C:\Users\MDPha\Documents\RPL\SortingHat\assets/training/CSV_Database_of_First_Names.csv

In [137]:
def housing(df):
    numbers = []
    for i in range(0,len(df)):
        m=(re.sub("([a-zA-Z])[a-zA-Z]*?[^a-zA-Z]*?([a-zA-Z]+?)[^a-zA-Z]*?$", r"\1\2", df[df.columns[0]][i])).lower()
        m=re.sub("[^a-zA-Z]","",m)
        hasher = hashlib.sha256()
        hasher.update((m).encode('utf-8'))
        random.seed(hasher.hexdigest(),version=2)
        for j in range(0,4):
            hasher.update(repr(random.random()).encode('utf-8'))
        numbers.append(int(hasher.hexdigest(), 16) % 4)#random.random())
    plt.hist(numbers)
    plt.grid()
    plt.show()
    numbers
    numbers.sort()
    numbers

In [138]:
housing(df)


Testing same method with names of people in RPL


In [139]:
cwd = os.getcwd()
fileName = cwd + '/assets/training/sorting2016_2017.csv'
filePath = os.path.join(cwd, fileName)
print(cwd, filePath)
df = pd.read_csv(filePath,header=0)


C:\Users\MDPha\Documents\RPL\SortingHat C:\Users\MDPha\Documents\RPL\SortingHat/assets/training/sorting2016_2017.csv

In [140]:
names = df[df.columns[0]]
len(names)


Out[140]:
51

In [141]:
housing(df)



In [142]:
def house(df):
    m=(re.sub("([a-zA-Z])[a-zA-Z]*?[^a-zA-Z]*?([a-zA-Z]+?)[^a-zA-Z]*?$", r"\1\2", df)).lower()
    print(m)
    m=re.sub("[^a-zA-Z]","",m)
    print(m)
    hasher = hashlib.sha256()
    hasher.update((m).encode('utf-8'))
    random.seed(hasher.hexdigest(),version=2)
    for j in range(0,4):
        hasher.update(repr(random.random()).encode('utf-8'))
        
    print((['Slytherin', 'Gryffindor', 'Hufflepuff', 'Ravenclaw'])[int(hasher.hexdigest(), 16) % 4])
house("12314124 Dan  Tran ")
house("1231241243 Jackie123512312!!!  First 123123141")
house("Ross Libman")
house("Reilly    893475@#$%^&*()   Jensen")


12314124 dtran
dtran
Ravenclaw
1231241243 jfirst
jfirst
Gryffindor
rlibman
rlibman
Slytherin
rjensen
rjensen
Hufflepuff

In [143]:
house("12314124 Dan  Tran ")
house("1231241243 Jackie123512312!!!  First 123123141")
house("Ross Libman")
house("Reilly    893475@#$%^&*()   Jensen")


12314124 dtran
dtran
Ravenclaw
1231241243 jfirst
jfirst
Gryffindor
rlibman
rlibman
Slytherin
rjensen
rjensen
Hufflepuff

In [ ]: