1

I am trying to work on my little side project with python (recently started learning about it), and I'm trying to make an emoji converter. What I mean by that, is that when a text contains an emoji, the converter would that word into an emoji. An example would be:

String to translate: Smile! You're too cool.

Translation: ! You're too .

The problem is, I have no slightest idea where to begin, and the things I have tried keep giving me the same error:

UnicodeEncodeError: 'UCS-2' codec can't encode character '\U0001f40d' in position 0: Non-BMP character not supported in Tk

Any suggestions? Thanks in advance

user484263
  • 21
  • 4
  • You can start with the `unicodedata` module. `unicodedata.name(character)` will give you the name of a character. E.g. `unicodedata.name('')` -> `'GRINNING FACE'`. You could group emojis together by emotion and build a text -> emoji translation table. – timgeb Sep 04 '18 at 04:54
  • Also see [this](https://stackoverflow.com/questions/52154875/translate-unicode-emojis-to-ascii-emojis-in-python#comment91260018_52154875) related question. – timgeb Sep 04 '18 at 04:55
  • Possible duplicate of ['UCS-2' codec can't encode characters in position 1050-1050](https://stackoverflow.com/questions/32442608/ucs-2-codec-cant-encode-characters-in-position-1050-1050) – zvone Sep 04 '18 at 06:01

1 Answers1

4

https://pypi.org/project/emoji/

doesn't have much documentation but you can get some idea by exploring on github https://github.com/carpedm20/emoji/search?q=emojize&unscoped_q=emojize

import emoji
import os
from string import punctuation

#example 1
string_to_translate1 = ":smile:! You're too :cool:."
translation1 = emoji.emojize(string_to_translate1,use_aliases=True,delimiters =(':',':'))

#example 2
string_to_translate2 = ":smile:! You're too :sunglasses:."
translation2 = emoji.emojize(string_to_translate2,use_aliases=True,delimiters =(':',':'))

#example 3
def custom_emojizer(user_text):
    temp_word_list = []

    for w in user_text.split():
        word = w
        endwith = ''
        for idx,char in enumerate(word):
            if char in punctuation:
                endwith = word[idx:]
                word = word.strip(char)
                break

        word_emoji = emoji.emojize(':'+word.lower()+':',use_aliases=True,delimiters =(':',':'))

        if ':' in word_emoji:
            temp_word_list.append(w)
        else:
            temp_word_list.append(word_emoji+endwith)

    return ' '.join(temp_word_list)


string_to_translate3 = "Smile! You're too cool."
translation3 = custom_emojizer(string_to_translate3)


with open('test.html','w+',encoding='utf-8-sig') as f:
    f.write(translation1+'<br>')
    f.write(translation2+'<br>')
    f.write(translation3)
os.startfile('test.html')

What are we doing in custom_emojizer:

since in your user_string 'Smile! You're too cool.' you have punctuation at the end of words and word can be in upper case/ title case and don't have delimiters we need to loop over the words in user_string and 1)remove punctuations and 2)convert word to lower() and 3) add delimiters before giving it to emoji.emojize then if word gets successfully converted to emoji we will not have any delimiters in it and all we need to do is append the punctuation if any, else we will just add the original word.

output:

! You're too .
! You're too .
! You're too .
Tanmay jain
  • 814
  • 6
  • 11