user_12
user_12

Reputation: 2129

How to replace emoji to word in a text?

Imagine I have a text like this?

text = "game is on 🔥 🔥"

How can I convert the emojis in the text to words?

Here what I've tried and this code below converts the emoji to word but how can I replace it in place of emoji in the original text. I couldn't figure it out.

import emot
[emot.emoji(i).get('mean').replace(':','').replace('_',' ').replace('-',' ') for i in text.split()]

Expected Output:

game is on fire fire

I've come across these two python modules Emoji, Emot but I couldn't figure out how to successfully convert the emojis to text and replace it in the text sentence.

Can anyone help?

Upvotes: 6

Views: 12527

Answers (2)

user3064538
user3064538

Reputation:

emoji.demojize takes an optional delimiters=(":", ":") parameter. Change it to ("", "")

import emoji
text = "game is on 🔥 🔥"
emoji.demojize(text, delimiters=("", ""))  # 'game is on fire fire'

You'll need to install it with

pip install emoji

Upvotes: 12

Fiona Victoria
Fiona Victoria

Reputation: 61

To convert emoji to text in a complete pandas data frame column

import emoji
def extract_emojis(s):
    return ''.join((' '+c+' ') if c in emoji.UNICODE_EMOJI['en'] else c for c in s)

tweets_df['text'] = tweets_df['text'].apply(lambda x: extract_emojis(x))
tweets_df['text'] = tweets_df['text'].apply(lambda x: emoji.demojize(x))

Upvotes: 1

Related Questions