emoji_token_cost() measures the size of the emoji in each row: bytes, code
points, grapheme clusters, and an estimate of the tokens they will cost a
byte-level tokeniser. Emoji are several times more expensive than their
visual weight suggests – a single ZWJ family emoji can run to well over a
dozen tokens – which makes them a real line item in a prompt budget.
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()anddplyr::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
factorworks and a numeric,Dateor 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 carryU+FE0F.- tokenizer
Optional function taking a character vector and returning either token counts (a numeric vector of the same length) or a list of token vectors. It is called on the row's emoji, concatenated.
NULL(default) uses the byte heuristic.What it returns is checked, because a wrong answer here is silent otherwise. A count is rounded up, and it has to be finite, not negative and within integer range;
NAis accepted and passed through, for a tokeniser that cannot answer for a row. A data frame is refused rather than read, sincelengths()on one counts its columns rather than its tokens.- presentation
Which code points count as emoji:
"emoji"(the default) or"any"."emoji"requires emoji presentation, soU+2764 U+FE0Fis an emoji and the bareU+2764is 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.
Value
data, as a tibble, with added columns .emoji_n, .emoji_bytes,
.emoji_codepoints, .emoji_graphemes and .emoji_token_estimate,
and, since 0.7.0, .emoji_n_hidden and .emoji_hidden_bytes: the code
points riding invisibly in the row (see emoji_hidden()) and their
UTF-8 bytes. They reach a tokenizer and belong to no emoji, so
.emoji_bytes and the token estimate leave them out, as they always
did; add the two to see the whole bill.
Details
Bytes, code points and graphemes are exact and tidyEmoji can be
authoritative about them. The token count cannot be: it depends on the
tokeniser. Without tokenizer, .emoji_token_estimate is a deliberately
crude heuristic of roughly two UTF-8 bytes per token, which is in the right
range for byte-level BPE vocabularies but is an estimate and should never be
quoted as a bill. Pass your real tokeniser through tokenizer when the
number matters.
.emoji_graphemes is the number of emoji occurrences, since the package's
detection is grapheme-aware: a skin-toned family emoji is one grapheme and
many code points, which is precisely the gap that makes emoji expensive.
See also
emoji_sanitize() for acting on the answer; emoji_ratio() for
the share of the text that is emoji;
vignette("reversible-preprocessing", package = "tidyEmoji") for the
before-and-after token budget on a real corpus.
Examples
family <- paste0("\U0001F468\u200d\U0001F469\u200d",
"\U0001F467\u200d\U0001F466")
df <- data.frame(text = c("hi \U0001f600", family, "plain"))
emoji_token_cost(df, text)
#> # A tibble: 3 × 8
#> text .emoji_n .emoji_bytes .emoji_codepoints .emoji_graphemes
#> <chr> <int> <int> <int> <int>
#> 1 hi 😀 1 4 1 1
#> 2 👨👩👧👦 1 25 7 1
#> 3 plain 0 0 0 0
#> # ℹ 3 more variables: .emoji_token_estimate <int>, .emoji_n_hidden <int>,
#> # .emoji_hidden_bytes <int>