How to replace a string using a dictionary containing multiple values for a key in python

Rahul rajan

I have dictionary with Word and its closest related words.

I want to replace the related words in the string with original word. Currently I am able replace words in the string which has only value per key ,I am not able to replace strings for a Key has multiple values. How can this be done

Example Input

North Indian Restaurant
South India  Hotel
Mexican Restrant
Italian  Hotpot
Cafe Bar
Irish Pub
Maggiee Baar
Jacky Craft Beer
Bristo 1889
Bristo 188
Bristo 188.

How dictionary is made

y= list(word)
words = y
similar = [[item[0] for item in model.wv.most_similar(word) if item[1] > 0.7] for word in words]
similarity_matrix = pd.DataFrame({'Orginal_Word': words, 'Related_Words': similar})
similarity_matrix = similarity_matrix[['Orginal_Word', 'Related_Words']] 

Its 2 columns inside a dataframe with lists

Orginal_Word    Related_Words
[Indian]        [India,Ind,ind.]    
[Restaurant]    [Hotel,Restrant,Hotpot]   
[Pub]           [Bar,Baar, Beer]     
[1888]          [188, 188., 18] 

Dictionary

similarity_matrix.set_index('Orginal_Word')['Related_Words'].to_dict()

{'Indian ': 'India, Ind, ind.',
 'Restaurant': 'Hotel, Restrant, Hotpot',
 'Pub': 'Bar, Baar, Beer'
 '1888': '188, 188., 18'}

Expected Output

North Indian Restaurant
South India  Restaurant
Mexican Restaurant
Italian  Restaurant
Cafe Pub
Irish Pub
Maggiee Pub
Jacky Craft Pub
Bristo 1888
Bristo 1888
Bristo 1888

Any help is appreciated

jezrael

I think you can replace by new dict with regex from this answer:

d = {'Indian': 'India, Ind, ind.',
 'Restaurant': 'Hotel, Restrant, Hotpot',
 'Pub': 'Bar, Baar, Beer',
 '1888': '188, 188., 18'}

d1 = {r'(?<!\S)'+ k.strip() + r'(?!\S)':k1 for k1, v1 in d.items() for k in v1.split(',')}

df['col'] = df['col'].replace(d1, regex=True)
print (df)
                        col
0   North Indian Restaurant
1   South Indian Restaurant
2        Mexican Restaurant
3       Italian  Restaurant
4                  Cafe Pub
5                 Irish Pub
6               Maggiee Pub
7           Jacky Craft Pub
8               Bristo 1888
9               Bristo 1888
10              Bristo 1888

EDIT (Function for the above code):

def replace_words(d, col):
    d1={r'(?<!\S)'+ k.strip() + r'(?!\S)':k1 for k1, v1 in d.items() for k in v1.split(',')}
    df[col] = df[col].replace(d1, regex=True)
    return df[col]

df['col'] = replace_words(d, 'col')

EDIT1:

If get errors like:

regex error- missing ), unterminated subpattern at position 7

is necessary escape regex values in keys:

import re

def replace_words(d, col):
    d1={r'(?<!\S)'+ re.escape(k.strip()) + r'(?!\S)':k1 for k1, v1 in d.items() for k in v1.split(',')}
    df[col] = df[col].replace(d1, regex=True)
    return df[col]

df['col'] = replace_words(d, 'col')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to have multiple values for a key in a python dictionary?

How to add multiple values to a key in a Python dictionary

How to assign multiple values to a key in a dictionary, multiple times using pandas

How to Replace value of string in file with dictionary key-value using python script

How to replace multiple key-value pairs in a dictionary in python?

Python Dictionary, finding a key with multiple values using a value?

how to check and replace list element in dictionary key using python

How to return output as a dictionary with unique key and multiple values in python?

How to add multiple values per key in python dictionary

Python: How to store multiple values for one dictionary key

How to make a dataframe with one key to multiple list values to a dictionary in python?

How to re-save a dictionary key with multiple values in python?

How to access single key from multiple values of a dictionary in Python

How to split string to dictionary using multiple separators as a key

Replace string values in a dataframe by using a dictionary

Replace multiple values in dictionary in Python 3

How to write a dictionary key values into text file using python

Python - dictionary with propositions (replace string values)

How to replace a string inside a python dictionary using regex

Using regex to break string into a dictionary with key and values

Python dictionary with multiple unique values corresponding to a key

Key with multiple values - check if value is in dictionary python

Adding multiple Values to a dictionary key in python?

Python append multiple values to nested dictionary by key

Python dictionary single key with multiple values possible?

Add multiple values to a key in python dictionary

Python: Adding multiple values to a dictionary key

How to append multiple key and value into a nested dictionary using python?

How to create csv file with dictionary key containing string as well dictionary key containing list?