emoji_ngrams() slides a window of n over each row's emoji, in reading
order (any text between the emoji is ignored), and returns one row per
n-gram occurrence. Repeated emoji are kept: a row containing the same emoji
twice in a row yields a bigram of that emoji with itself. This is the emoji
analogue of tidytext::unnest_tokens(..., token = "ngrams") and feeds
sequence / Markov-style analyses of how emoji chain together.
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.- n
Length of the n-gram window. Default
2(bigrams).- sep
Separator between the glyphs of an n-gram. Default a space.
Value
A tibble with columns .row_number (position of the entry in
data), .position (where the n-gram starts within the row's emoji
sequence) and .emoji_ngram. Rows with fewer than n emoji contribute
nothing. The columns of data are not carried, so a grouping is not
either – join back on .row_number to recover them. Unlike the
corpus-wide verbs this one does not pool your rows, so there is no
per-group answer being silently turned into a global one.
Details
Glyphs are canonicalised through the package's codepoint key, so two spellings of one emoji make one n-gram token rather than two; see Which spelling comes back in tidyEmoji.
See also
emoji_pairs() for order-free co-occurrence;
emoji_extract_unnest() for the underlying one-emoji-per-row form.
Examples
df <- data.frame(text = c("\U0001f602\U0001f60d\U0001f389", "\U0001f602"))
emoji_ngrams(df, text)
#> # A tibble: 2 × 3
#> .row_number .position .emoji_ngram
#> <int> <int> <chr>
#> 1 1 1 😂 😍
#> 2 1 2 😍 🎉
emoji_ngrams(df, text, n = 3)
#> # A tibble: 1 × 3
#> .row_number .position .emoji_ngram
#> <int> <int> <chr>
#> 1 1 1 😂 😍 🎉