Emojify

This could be broken out into 3 separate pages perhaps as a separate lab combined with the Emoji Deep Dive content or perhaps having this be its own lab and Deep Dive be a second one. --MF, 1/10/23
1.6.3 add an example of what the emojify block does (e.g., in pink box)
- emojify word (sun) reporting (in this folder: emojify-word-sun.png)
- emojify (My cat plays soccer in the sun) reporting (in this folder: emojify-my-cat-plays-soccer-in-the-sun.png) --MF, 8/8/23
In this activity, you'll explore how the emojify block works, add additional emojis to it, and learn how to search a list using find first item 'predicate input slot' in 'list input slot'.
  1. Open your "Texting Time" project, if it isn't open already, and find the emojify word () block in the palette. The emojify block you've used already takes a sentence as input and uses emojify word to process each word.
  2. The emojify word block takes a single word as input. If that word has a matching emoji inside the block's code, it reports that emoji. Otherwise, it just reports the input word. Try the following inputs in the emojify word block:
    1. sun
    2. rain

Using Find First to Make Emojify Word

Like map, the find first block is a higher-order function because it takes a function (the predicate) as input.

The emojify word block uses find first item 'predicate input slot' in 'list input slot' to search for a matching emoji. The first input to find first is a predicate, a hexagon-shaped reporter that reports a true/false value. The second input is a list. The find first block reports the first item in the list that makes the predicate true. For example,
find first item (is ( ) a (number)?) in (list (seven) (apple) (-0.82) (giraffe) (5))

Find first uses the predicate to check each item from the input list. The blank in the predicate is where each item from the list goes as it is checked.

The emojify word block uses find first to search a list of lists (a list with lists inside it).
emojify (word) {
	script variables (word emoji pair)
	set (word emoji pair) to (find first item ((item (1) of 'list input slot') = lowercase (word)) in ('list of 19 two-item lists, each containing a word, such as love, and an emoji, such as ❤️'))
	report (if (is (word emoji pair) a ('list')?) then (item (2) of (word emoji pair)) else (word))
}

  1. Talk with Your Partner Discuss why each of these expressions reports what it does:
    find first item ((item (1) of 'list input slot') = 'car') in ('list of 19 two-item lists') reporting list (car) (🚗) find first item ((item (1) of 'list input slot') = 'dinosaur') in ('list of 19 two-item lists') reporting ''
  2. Click for an explanation.

    The list being searched is a list of lists. These two find first expressions are searching for the first list in the list of lists that has certain word as its first item. When searching for a list with the first item "car", find first found a matching list: list (car) (🚗) When searching for a list with the first item "dinosaur", find first found no matching list.

  3. Talk with Your Partner Without using Snap! to check, discuss:
    1. What will emojify word (cat) report?
    2. What will emojify word (cow) report?
    3. How could you change the code to make emojify word (cow) report an emoji of a cow? (You'll have a chance to add word/emoji pairs soon!)

Using If Then Else to Make Emoji from Pair

The emojify word block also uses emoji from pair 'list input slot' otherwise () to check if there is a match by using if then else. For example, if there is a matching list, it reports the emoji from the list:
emoji from pair (list ('star') ('⭐️')) otherwise ('star') reporting '⭐️'
And if there is no match, it reports the input word that should be reported otherwise:
emoji from pair 'list input slot' otherwise ('star') reporting 'star'

  1. Talk with Your Partner Discuss how the emoji from pair block uses if then else to do this job.
    emoji from pair (pair...) otherwise (word) {
	if (is (pair) a (list)?) then (item (2) of (pair)) else (word)
}

    What does What does IS () A (LIST)? do? do?

    The is () a ()? predicate takes two inputs, the first can be anything, and the second is the name of a type of value (number, text, list, etc.). If the first input is of the type selected in the second input, the block will report true; otherwise, it will report false. In this case, if the pair is a list, it will report true, and if not, it will report false.

    1. What are the possible inputs for the word/emoji pair? (Think about how this block is being used.)
    2. Why do each of the following expressions report what they do? (Think about the if then else expression inside emoji from pair.)
      emoji from pair (list ('star') ('⭐️')) otherwise ('star') reporting '⭐️'
      emoji from pair 'list input slot' otherwise ('star') reporting 'star'

Using Emojify Word to Make Emojify

  1. The emojify () block uses the emojify word block. You can type a whole sentence into the emojify block, and it will replace any words from the word/emoji pair list with their matching emojis. Try it for a few sentences using some of the words with matching emojis in the list.
  2. Talk with Your Partner Right-click on the emojify block and select "Edit". Discuss how this block works.
    emojify (text) {
	report (join (map (join (emojify word ()) ('space')) over (split (text) by ('word'))))
}
  3. Write a paragraph describing how the emojify block works. Include the following:
    • A description of the input and how it is used in the code
    • A review of what emojify word does
    • A description of how join, map, and split blocks are used
    • A description of what is reported
  4. Click for an example of what happens.

    If the input text is "I love rain", that first gets split into the list "I", "love", "rain"; then emojify word gets mapped over each word (joined with a space at the end of each word), resulting in the list "I ", "❤️ ", "☔️ "; and finally, that list is joined into "I ❤️ ☔️ ".

    Here's the same process in a diagram:
    flow chart showing how an input is transformed through a series of functions: 'I love rain' → SPLIT → 'I', 'love', 'rain' → MAP (emojify word) → 'I', '❤️', '☔️' → JOIN → 'I ❤️ ☔️'

  5. Create help text for emojify that describes the input and output.
    You learned how to add help text by attaching a comment to the hat block in Unit 1 Lab 6 Activity 1: Texting Time.
  6. Now Is a Good Time to Save Change the code inside emojify word to make emojify translate a word of your choosing into an emoji of your choosing. Test your work in emojify word first, and then by trying a sentence with that word as input to emojify.
  7. Click for hints about adding more word/emoji pairs to the emojify word block.

    1. Add an input slot for a new pair by clicking the right-facing triangle at the end of the outer list block (the list of lists). You should see a new input slot appear at the end of the list. emojify (word) {
	script variables (word emoji pair)
	set (word emoji pair) to (find first item ((item (1) of 'list input slot') = lowercase (word)) in ('list of 19 two-item lists, each containing a word, such as love, and an emoji, such as ❤️'))
	report (if (is (word emoji pair) a ('list')?) then (item (2) of (word emoji pair)) else (word))
}
    2. Duplicate one of the nested word/emoji pair lists by right-clicking it and selecting "duplicate." Then drag the duplicated list block into the new input slot.
    3. Type the new word and emoji in the input slots of the new list. You can paste emojis from emojifinder.org or emojipedia.org.

  1. Add more word/emoji pairs and try them out!
In this activity, you reviewed the map, join, split, and if blocks, you learned about a new block find first item 'predicate input slot' in 'list input slot' that uses a predicate to check for an item in a list, you added a word/emoji pair to the list, and you interpreted new blocks containing new blocks.