Rank the row based on the similar text using python?

Viewed 127

How to rank the data frame based on the row value. i.e I have a row that contains text data want to provide the rank based on the similarity?

Input

Expected output

expected output

i have tried with the levistian distance but not sure how can i do for the whole table

def bow(x=None):
    x = x.lower()
    words = x.split(' ')
    words.sort()
    x = ' '.join(words)
    
    exclude = set('{}{}'.format(string.punctuation, string.digits))
    x = ''.join(ch for ch in x if ch not in exclude)
    x = '{} '.format(x.strip())
    return x

#intents = load_intents(export=True)
df['bow'] = df['name'].apply(lambda x: bow(x))

df.sort_values(by='bow',ascending=True,inplace=True)

last_bow = ''
recs = []
for idx,row in df.iterrows():
    
    record = { 
        'name': row['name'],
        'bow': row['bow'],
        'lev_distance': ed.eval(last_bow,row['bow'])
    }
    recs.append(record)
    last_bow = row['bow']

intents = pd.DataFrame(recs,columns=['name','bow','lev_distance'])

l = intents[intents['lev_distance'] <= lev_distance_range]

r = []
for x in l.index.values:
    r.append(x - 1)
    r.append(x)

r = list(set(r))
    
l = intents.iloc[r,:]
1 Answers

Using textdistance, you could try this:

import pandas as pd
import textdistance

df = pd.DataFrame(
    {
        "text": [
            "Rahul dsa",
            "Rasul dsad",
            "Raul ascs",
            "shrez",
            "Indya",
            "Indi",
            "shez",
            "india",
            "kloa",
            "klsnsd",
        ],
    }
)

df = (
    df
    .assign(
        match=df["text"].map(
            lambda x: [
                i
                for i, text in enumerate(df["text"])
                if textdistance.jaro_winkler(x, text) >= 0.9
            ]
        )
    )
    .sort_values(by="match")
    .drop(columns="match")
)


print(df)
# Output
         text
0   Rahul dsa
1  Rasul dsad
2   Raul ascs
3       shrez
6        shez
4       Indya
5        Indi
7       india
8        kloa
9      klsnsd
Related