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 .