Nabih Bawazir
Nabih Bawazir

Reputation: 7275

How do I make spelling corrector using txt file

Here's my txt file, called it replacer.txt

keyword_origin, keyword_destinantion
topu,topup
atmstrbca,atm bca

Here's what I want

id keyword
1  transfer atmstrbca
2  topu bank
3  topup bank

My expected output

id keyword
1  transfer atm bca
2  topup bank
3  topup bank

What I did is

df['keyword'].str.replace("atmstrbca","atm bca")
df['keyword'].str.replace("topu","topup")

The output is

id keyword
1  transfer atm bca
2  topup bank
3  topupp bank

My Idea is using text replacer.txt to do this since the list is more tahn 100 keyword

Upvotes: 2

Views: 34

Answers (2)

Andy Hayden
Andy Hayden

Reputation: 375865

You can use str.replace with a callable:

In [11]: d = {"atmstrbca": "atm bca", "topu": "topup"}  # all the typos

In [12]: regex = r'\b' + '|'.join(d.keys()) + r'\b'

In [13]: df['keyword'].str.replace(regex, lambda x: d[x.group()], regex=True)
Out[13]:
0    transfer atm bca
1          topup bank
2          topup bank
Name: keyword, dtype: object

You can make the dict from the other DataFrame, e.g. via:

dict(zip(df_replacer.keyword_origin, df_replacer.keyword_destinantion))

Upvotes: 1

jezrael
jezrael

Reputation: 863611

Create dictionary from first file and split values by whitespace and use get for replace:

d = dict(zip(df1.keyword_origin, df1.keyword_destinantion))
#alternative
#d = df1.set_index('keyword_origin')['keyword_destinantion'].to_dict()
df2['keyword'] = df2['keyword'].apply(lambda x: ' '.join([d.get(y, y) for y in x.split()]))
print (df2)
   id           keyword
0   1  transfer atm bca
1   2        topup bank
2   3        topup bank

Upvotes: 1

Related Questions