Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Emoji Dictionary Help
#1
Hello,

So I am trying to replace words from the sentence input by the relevant emoji from this dictionary. In the end i would like to reprint the sentence containing the replaced emojis.
Below is the code I've attempted to write so far, but I cannot get the complete sentence to print properly.
emoji_dict = {
    "happy": "😃",
    "heart": "😍",
    "rotfl": "🤣",
    "smile": "😊",
    "crying": "😭",
    "kiss": "😘",
    "clap": "👏",
    "grin": "😁",
    "fire": "🔥",
    "broken": "💔",
    "think": "🤔",
    "excited": "🤩",
    "boring": "🙄",
    "winking": "😉",
    "ok": "👌",
    "hug": "🤗",
    "cool": "😎",
    "angry": "😠",
    "python": "🐍",
}

sentence = input("Please enter a sentence: ")
words = sentence.split()

print(words)

emoji_words = ("")

for word in words:
    if word in emoji_dict:
        word = emoji_dict[word]
    emoji_words += word

print(emoji_words)
snippsat write May-07-2022, 10:14 AM:
Added code tag in your post,look at BBCode on how to use.
Reply
#2
Please post code in propper tags. Makes it easier to read.
Maybe something like this

words = input('Type something: ').split()
emoji = {'smile': ':)', 'wink': ';)'}

chars = ['.', '!', '?']
for i, word in enumerate(words):
    if word[-1:] in chars and word[:-1] in emoji.keys():
        words[i] = emoji[word[:-1]] + word[-1:]
    else:
        if word in emoji.keys():
            words[i] = emoji[word]

print(' '.join(words))
Output:
Type something: have a smile and a wink. have a :) and a ;).
snippsat and rewainy like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Line 33 change to:
emoji_words += f'{word} '
Or use menator01 code.
Reply
#4
(May-07-2022, 10:17 AM)snippsat Wrote: Line 33 change to:
emoji_words += f'{word} '
Or use menator01 code.

Thank you, but this gives syntax error.
Reply
#5
(May-07-2022, 01:13 PM)rewainy Wrote: Thank you, but this gives syntax error.
Then i guess you use a Python version less than Python 3.6(f-string was new in this release)
Try change to this:
emoji_words += '{} '.format(word)
You should definitely use newer Python version.
A loot will not work if use old version of Python.
rewainy likes this post
Reply
#6
(May-07-2022, 01:46 PM)snippsat Wrote:
(May-07-2022, 01:13 PM)rewainy Wrote: Thank you, but this gives syntax error.
Then i guess you use a Python version less than Python 3.6(f-string was new in this release)
Try change to this:
emoji_words += '{} '.format(word)
You should definitely use newer Python version.
A loot will not work if use old version of Python.

I am sorry, it did work now actually...both methods you sent.

Thank you for your support :)
snippsat likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  fontforge Emoji encoding and colored glyphs pauf28 0 2,167 Dec-22-2020, 10:05 AM
Last Post: pauf28
  Because the emoji appears black and white at the exit ? nerd 3 5,624 Jan-28-2019, 11:34 PM
Last Post: nerd
  I can not install the emoji module nerd 1 4,318 Jan-27-2019, 05:53 AM
Last Post: nerd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020