Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert strings to emoji in python

I have a collection of twits and i want to check emojis in them, but it looks like the writing procedure for the collection converted all emojis in string for example '😊' is ':-)' in text and '😃' is ':D' and so on with all emojis. If we try to check unicode codepoints for them we get ':-)'.encode('utf-8') equals to b':-)' in the same time '😊'.encode('utf-8') equals to 'b'\xf0\x9f\x98\x8a and equality check fails. Using utf-16 : ':-)'.encode('utf-16') equals to b'\xff\xfe:\x00-\x00)\x00' and '😊'.encode('utf-16') is b'\xff\xfe=\xd8\n\xde' . So is there any way to convert text representations such as ':-)' back to emoji '😊'.

like image 700
UGeorge Avatar asked Aug 02 '26 00:08

UGeorge


1 Answers

Use a dictionary to convert any text emoticon back to emoji e.g. as follows:

>>> dict_emo = { ':-)'  : b'\xf0\x9f\x98\x8a',
...              ':)'   : b'\xf0\x9f\x98\x8a',
...              '=)'   : b'\xf0\x9f\x98\x8a',  # Smile or happy
...              ':-D'  : b'\xf0\x9f\x98\x83',
...              ':D'   : b'\xf0\x9f\x98\x83',
...              '=D'   : b'\xf0\x9f\x98\x83',  # Big smile
...              '>:-(' : b'\xF0\x9F\x98\xA0',
...              '>:-o' : b'\xF0\x9F\x98\xA0'   # Angry face
...              }
>>> print( dict_emo[':)'].decode('utf-8'))
😊
>>> print( dict_emo['>:-('].decode('utf-8'))
😠
>>> print( dict_emo[':-D'].decode('utf-8'))
😃
>>>
>>>
>>> dict_emot= { ':-)'  : b'\xf0\x9f\x98\x8a'.decode('utf-8'),
...              ':)'   : b'\xf0\x9f\x98\x8a'.decode('utf-8'),
...              '=)'   : b'\xf0\x9f\x98\x8a'.decode('utf-8'),  # Smile or happy
...              ':-D'  : b'\xf0\x9f\x98\x83'.decode('utf-8'),
...              ':D'   : b'\xf0\x9f\x98\x83'.decode('utf-8'),
...              '=D'   : b'\xf0\x9f\x98\x83'.decode('utf-8'),  # Big smile
...              '>:-(' : b'\xF0\x9F\x98\xA0'.decode('utf-8'),
...              '>:-o' : b'\xF0\x9F\x98\xA0'.decode('utf-8')   # Angry face
...              }
>>> print( dict_emot[':)'] )
😊
>>> print( dict_emot['>:-o'] )
😠
>>> print( dict_emot['=D'] )
😃
>>>

Unfortunately, there are at least two tasks remaining:

  • Text Smiley Faces and Their Meaning are neither stable nor definitive, see also Common examples of emoticons (Computer Definition) and List of emoticons - although there are some attempts to create a resource of all the text smileys and emoticons in the world;
  • Natural Language Processing: What is an algorithmic way to find all smileys in a text? and how-to eliminate (false) embedded text emoticons like :-) smile in :-)) double chin.
like image 152
JosefZ Avatar answered Aug 03 '26 15:08

JosefZ