BYU logo Computer Science

Emojis (dictionaries) Transcript

Start visual description. The instructor’s screen is shared where he shows how to prepare and write code for a particular program. He demonstrates the steps as he describes each aspect. The instructor can be seen in the top right-hand corner in a small box. End visual description.

[00:00:00] Instructor: Hi! Let’s go through the world needs more emojis using dictionaries. So you’re given a dictionary that will map words to emojis, and we want to replace all instances of the word with its corresponding emoji. And let’s ignore case. So give a dictionary, it has dog, cat, tree, bird. Each map to a corresponding symbol.

[00:00:24] Then the phrase like my dog has fleas should become my dog face has fleas, right? How do you do this? Well, let’s start with a file. Let’s go through emojis here first. So here I’ve got an emoji dictionary, mapping dog to symbols. And we’re going to run add emojis. We’re going to pass in the phrase that comes from the command line and the emojis dictionary that we have here.

[00:00:53] And now up here, we just need to replace each word that’s corresponding symbol. So when we have this idea of a string that we want to replace words with, we’ve seen that pattern before, right? We want to split the string into its individual words, operate on those words, and then join them back together.

[00:01:12] So I can say words equals text dot split. Write new words, this is a list, and at some point, I’m going to return the new words joined together. And now in the middle is where we need to do this transformation. Where some words change and some words don’t. Well, that’s the transform pattern.

[00:01:36] So we can set that up as well, you know, for word in words. And then at some point we’ll pin something to new words and that depends on what the word is. Now, here’s the thing. We’re going to have lots of words coming through here. My dog has fleas are each individually going to come into our for loop.

[00:02:03] And if the word is in our dictionary, if we have a symbol for that word, we want to use the symbol. If we don’t have a symbol for that word, then we just want to use the word as it is, right? Like my or has or fleas.

[00:02:19] So how do we know if we have a symbol or not? Well, that’s using the in operator. If word is in emojis. Now, that means I know I have a corresponding emoji for that word. So let’s replace the word with its emoji. So we’re going to look up that word’s value in the emojis dictionary and then we’re going to replace the word with that value instead. And then whatever the word is at this point, we want to add that to append and then join them all together and send it out. So I can come here to terminal.

[00:03:04] So I’ve got the emojis dot py right here. Python emojis dot py. And then a phrase. The dogs chased the cats who watched the birds. Two dogs chased the cats who watched the birds. Right. Why didn’t two cats and birds? Because we only have cat and bird.

[00:03:32] Let’s use a different phrase. The dogs chased the cat who watched the bird in the tree yesterday. This fun. All right. The dogs chased the cat who watched the bird in the tree yesterday. That’s mapping.

[00:03:44] How was this again that it worked here? We split the words, we created a new list of words, and now for each word, if we had an alternate for that word, then we used it alternate and appended it to the list and rejoined it. So simple example of how you can take a dictionary that maps from keys to values and use that to perform a substitution of a key into its value.