Pie-ton
Pie-ton

Reputation: 562

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

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.

Upvotes: 6

Views: 10716

Answers (2)

awakenedhaki
awakenedhaki

Reputation: 301

This is a rather complicated question given that any given emoji has multiple meanings. The meaning of an emoji will depend on the context of the current text, or preceding text (if thinking about messenger like applications). This is known as semantic meaning.

Edit: The Kaggle kernel works fine for the fire emoji, it fails in other cases. The next example in the kernel is as follows:

text = "Hilarious 😂"
convert_emojis(text)

# 'Hilarious face_with_tears_of_joy'

Upvotes: 1

Aditya Mishra
Aditya Mishra

Reputation: 1885

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

Upvotes: 5

Related Questions