convert emoji unicode to TEXT in python
Asked Answered
I

3

6

I have an application made in python (yowsup), I receive text and emoji in my bot... I manage to get the 😀 to convert to "\U0001F600" code...

now i need to convert u"\U0001F600" to :grinning: TEXT or GRINNING FACE

got some source from this page.... http://www.fileformat.info/info/unicode/char/1F600/index.htm

@signals.message_received.connect
def handle(message):
    #message.log() to see message object properties
    #print(message.log())
    params = {}
    params_upload = {}
    zapsend_host = config.config_variables['zapsend_host']
    zapsend_port = config.config_variables['zapsend_port']

    # CASE TEXT-MESSAGE AND NOT GROUP
    if helper.is_text_message(message.message_entity) and helper.isGroupJid(message.conversation) == False:
        #converted here....
        params['msg']  = message.text.encode('unicode_escape') 
        params['number']  = message.conversation
        params['whatsapp']= config.config_variables['user']
        params['media'] = 'text'
        params['caption'] = ''
        params['name'] = message.who_name
        database_helper.sync_contacts(message.conversation, message.who_name)
        database_helper.save_message_receive(params, message)
        print("MSG FROM CLIENT: "+ message.conversation +" => " + params['msg'])
        requests_helper.request_get(zapsend_host, zapsend_port,'zapsend',params)
Ilanailangilang answered 25/11, 2017 at 19:40 Comment(3)
knows to do what?Embattle
I'll rephrase my questionIlanailangilang
Can you show us your code?Avignon
S
2

Try this:

s = u'\U0001f600'
from emoji.unicode_codes import UNICODE_EMOJI

print UNICODE_EMOJI[s]
>:grinning_face:

this assumes you have the module emoji installed

Shifty answered 25/11, 2017 at 19:50 Comment(1)
thanks alot!!!!! now.. any tip to do this in a phrase... like: " I have alot of emojis u"\U0001F600" " and transform to " I have alot of emojis :grinning_face:"Ilanailangilang
D
20

@sealabr check this.

import emoji
print(emoji.demojize('Python is 👍'))
>>Python is :thumbs_up:
Deerstalker answered 24/6, 2018 at 7:50 Comment(0)
S
2

Try this:

s = u'\U0001f600'
from emoji.unicode_codes import UNICODE_EMOJI

print UNICODE_EMOJI[s]
>:grinning_face:

this assumes you have the module emoji installed

Shifty answered 25/11, 2017 at 19:50 Comment(1)
thanks alot!!!!! now.. any tip to do this in a phrase... like: " I have alot of emojis u"\U0001F600" " and transform to " I have alot of emojis :grinning_face:"Ilanailangilang
T
2

This approach will handle both:

  • emojis: 👽😲👌
  • emoticons: :-)) ; 8‑D

Using the emot library and a bit of text manipulation: Using flashtext as it is much faster than regular expression for exact string find and/or replace.

from emot.emo_unicode import UNICODE_EMOJI, UNICODE_EMOJI_ALIAS, EMOTICONS_EMO
from flashtext import KeywordProcessor

## formatting
all_emoji_emoticons = {**EMOTICONS_EMO,**UNICODE_EMOJI_ALIAS, **UNICODE_EMOJI_ALIAS}
all_emoji_emoticons = {k:v.replace(":","").replace("_"," ").strip() for k,v in all_emoji_emoticons.items()}

kp_all_emoji_emoticons = KeywordProcessor()
for k,v in all_emoji_emoticons.items():
    kp_all_emoji_emoticons.add_keyword(k, v)
kp_all_emoji_emoticons.replace_keywords('I am an 👽 hehe :-)). Lets try another one 😲. It seems 👌')

#output==> 'I am an alien hehe Very happy. Lets try another one astonished. It seems ok hand'
Tarpaulin answered 13/2, 2022 at 3:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.