Skip to contents

emoji_to_text() returns a copy of data with its text column rewritten so that every emoji is replaced by its name or shortcode. This is useful for accessibility (screen readers) and as an NLP normalisation step before tokenising. Detection is grapheme-aware and joins go through emoji_key(), so emoji carrying the U+FE0F variation selector still resolve.

Usage

emoji_to_text(
  data,
  text,
  format = c("name", "shortcode"),
  wrap = ":{x}:",
  presentation = "emoji",
  hidden = c("remove", "keep")
)

Arguments

data

A data frame or tibble containing a text column. Grouped data frames are accepted. The verbs that work a row at a time (adding columns, or keeping and expanding rows) carry the grouping through to their result, as dplyr::mutate() and dplyr::filter() do. The verbs that pool across rows – the counts, the co-occurrence edge lists, the time series – warn that they ignore the grouping and return one corpus-wide answer.

text

The text column to scan, supplied unquoted. Any atomic column is accepted and read as character, so a factor works and a numeric, Date or logical one simply contains no emoji. A list column – or a data-frame column – is refused rather than coerced, because coercing one deparses it and the emoji found would be in the code rather than in your data. What counts as an emoji is the same in every verb; see the Detection section of tidyEmoji for the one case that surprises people, code points that are emoji only when they carry U+FE0F.

format

Output form: "name" (the Unicode name, e.g. "grinning face") or "shortcode" (the canonical GitHub-style alias, e.g. "grinning", wrapped as ":grinning:"). Default "name".

wrap

When format = "shortcode", the wrapper applied to each shortcode, written as a template with {x} standing for the shortcode. Default ":{x}:". Must contain {x}, or every emoji would be replaced by the same literal string. Ignored for format = "name".

Only the default is reversible by text_to_emoji(), which looks for exactly :shortcode:; a wrap that does not produce that token leaves the shortcode in the text as an ordinary word, silently. See emoji_sanitize() for the cases.

presentation

Which code points count as emoji: "emoji" (the default) or "any".

  • "emoji" requires emoji presentation, so U+2764 U+FE0F is an emoji and the bare U+2764 is not. This is the default and it will stay the default: widening it would silently recount every corpus already analysed.

  • "any" also reads the text-presentation spellings Unicode lists for an emoji (the bare heart, U+263A, U+2714, a keycap without its selector), because several keyboards emit them. It never reads the copyright, registered and trade-mark signs (U+00A9, U+00AE, U+2122), which are typography far more often than they are emoji.

"any" only ever adds occurrences: everything "emoji" finds, it finds too, in the same place. emoji_coverage() reports how many it adds and how many of those a lexicon can score, which is the cost of the choice in one number. Every verb that counts, scores or rewrites the emoji in a text column takes this argument, so counts still agree across the package for whichever value you use. The flag verbs do not need it, since a flag has no text-presentation form.

hidden

What to do with hidden payloads, as in emoji_sanitize(): "remove" (the default) deletes the variation selectors and tag characters emoji_hidden() reports, those the rewrite leaves hidden included, and "keep" leaves them, as every version before 0.7.0 did.

Value

data, as a tibble, with the text column rewritten in place (same column name). NA entries stay NA, and emoji with no known name are left in place unchanged. A ZWJ sequence the catalogue does not list, such as grinning face, U+200D, fire, is written as the names of the parts it splits into, in order and separated by spaces ("grinning face fire"), or as their shortcodes (":grinning::fire:"): the parts every other verb counts since 0.7.0. If any part has no name the sequence stays as it is, and so it does when it holds anything that is no part, such as a hair component joined to a face, which naming the parts alone would delete.

A preview of what a screen reader says

format = "name" writes the text a screen reader that reads CLDR names, as NVDA does, will speak. That Preview is the design the sighted writers in Griggio, Gorman and Tigwell (CSCW 2024) preferred to warnings, because it leaves the judgement to them, so the package offers it here rather than in a verb of its own; emoji_a11y_check() reports the facts their taxonomy is built from.

See also

text_to_emoji() for the inverse (emojize); as_emoji_name(), as_emoji_shortcode(), as_emoji() for vector helpers; emoji_a11y_check().

Examples

df <- data.frame(text = "great \U0001f600 love \u2764\ufe0f")
emoji_to_text(df, text, format = "name")
#> # A tibble: 1 × 1
#>   text                              
#>   <chr>                             
#> 1 great grinning face love red heart
emoji_to_text(df, text, format = "shortcode")
#> # A tibble: 1 × 1
#>   text                         
#>   <chr>                        
#> 1 great :grinning: love :heart: