How to replace emoji to word in a text?
Question:
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?
Answers:
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'
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))
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?
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'
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))