Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert emojis/emoticons to their meanings in python?

Tags:

I am trying to clean up tweets to analyze their sentiments. I want to turn emojis to what they mean.

For instance, I want my code to convert

'I ❤ New York' 
'Python is 👍'

to

'I love New York' 
'Python is cool'

I have seen packages such as emoji but they turn the emoji's to what they represent, not what they mean. for instance, they turn my tweets to :

print(emoji.demojize('Python is 👍'))
'Python is :thumbs_up:'

print(emoji.demojize('I ❤ New York'))
'I :heart: New York'

since "heart" or "thumbs_up" do not carry a positive or negative meaning in textblob, this kind of conversion is useless. But if "❤" is converted to "love", the results of sentiment analysis will improve drastically.

like image 230
Pie-ton Avatar asked Sep 01 '19 09:09

Pie-ton


1 Answers

Referring this kaggle kernel here

def convert_emojis(text):
    for emot in UNICODE_EMO:
        text = re.sub(r'('+emot+')', "_".join(UNICODE_EMO[emot].replace(",","").replace(":","").split()), text)
    return text

text = "game is on 🔥"
convert_emojis(text)

Gives the output 'game is on fire'. You can find a dictionary mapping from emojis to words here.

Hope this helps

like image 189
Aditya Mishra Avatar answered Nov 15 '22 05:11

Aditya Mishra