0

I have prepared a chatbot using tkinter python. I want to display the emoji after every message. How to concatenate string and emoji in chatbot? The problem doesn't arise if I use a normal string. For example, I have tried this -

import codecs
x = 'Hello \\U0001F600 world'
codecs.decode(x, 'unicode_escape')

Output

'Hello  world'

But when I implement the same in a chatbot it's giving - code -

face = '\\U0001F600'
face_decode = codecs.decode(face, 'unicode-escape')
def send():
           msg = EntryBox.get("1.0",'end-1c').strip()
           EntryBox.delete("0.0",END)
           if msg != '':
                   ChatLog.config(state = NORMAL)
                   ChatLog.insert(END, "You: " + msg + '\n\n', )
                   ChatLog.config(font =  ("Arial", 14, 'bold'))

                   res = chatbot_response(msg)
                   ChatLog.insert(END, "Tanya: " + res + face + '\n\n')

Here msg and res are the strings. I am getting the following output

output in the bot

How to overcome this?

papanito
  • 2,349
  • 2
  • 32
  • 60
DHEERAJ
  • 1
  • 2
  • Hello, you have defined the variable `face_decode` but didn't use it, sure that is what you wanted? – Talon Jan 24 '20 at 17:07
  • Also, you can avoid the decoding if you use the `unicodedata` module, you can replace your escape code with `unicodedata.lookup("GRINNING FACE")`. Also makes it easier to understand which unicode character that is at a later time. – Talon Jan 24 '20 at 17:13
  • Okay. I wrote face_decode in place of face. I am getting You:hi Tanya:hi there, this is Tanya. How can I help you?ð\x9f\x98\x80 – DHEERAJ Jan 24 '20 at 17:47
  • how to decode the last encoded figures? – DHEERAJ Jan 24 '20 at 17:53
  • I tried to rebuilt your thing, changing `face` to `face_decode` worked for me, did you change anything else? – Talon Jan 24 '20 at 22:47
  • I have made the following changes as you said------------------------------------ face = unicodedata.lookup("GRINNING FACE") --------- face_decode = codecs.decode(face, 'unicode-escape')---------- res = chatbot_response(msg)----------- ChatLog.insert(END, "Tanya: " + res + face_decode + '\n\n')--------------- (hyphens are just to differentiate the lines) – DHEERAJ Jan 25 '20 at 05:33
  • If you use the unicodedata lookup, the result will already be decoded, you only need the decoding if you write it like this `'\\U0001F600'`. With the lookup, you can skip this step. – Talon Jan 26 '20 at 11:34
  • I acknowledge your time sir, but It's not working either. It's working outside, but it's not inside the bot.. – DHEERAJ Jan 26 '20 at 11:53
  • `root = tk.Tk()| eb = tk.Text(root)| cl = tk.Text(root)| eb.pack()| cl.pack()| face = unicodedata.lookup("GRINNING FACE")| def send():| msg = eb.get("1.0",'end-1c').strip()| eb.delete("0.0", tk.END)| if msg != '':| cl.config(state = tk.NORMAL)| cl.insert(tk.END, "You: " + msg + '\n\n', )| cl.config(font = ("Arial", 14, 'bold'))| res = "placeholder"| cl.insert(tk.END, "Tanya: " + res + face + '\n\n')| button = tk.Button(root, command=send, text="Send")| button.pack()| root.mainloop()| ` – Talon Jan 26 '20 at 20:42
  • replace | with newline and match the indentation to yours, also `import tkinter as tk` and `import unicodedata`. I use python 3.8, that works on my machine – Talon Jan 26 '20 at 20:45

0 Answers0