Skip to contents

emoji_context() returns one row per emoji occurrence with a window of the text on either side of it. It is the primitive the context-dependent analyses need: emoji are polysemous, and what a glyph means in a message is decided by its co-text, not by a lexicon.

Usage

emoji_context(
  data,
  text,
  window = 5,
  unit = c("word", "char"),
  keep_text = FALSE
)

Arguments

data

A data frame or tibble containing a text column.

text

The text column to scan, supplied unquoted.

window

Size of the context window on each side, in tokens (unit = "word") or characters (unit = "char"). Default 5.

unit

"word" (default) or "char".

keep_text

If TRUE, also return the row's original text column. Default FALSE.

Value

A tibble with one row per emoji occurrence, in reading order, and columns .row_number (position of the entry in data), .position (the character position at which the emoji starts), .emoji, .emoji_context_left, .emoji_context_right and .emoji_context (the two sides joined by a space – the co-text without the glyph). Rows with no emoji contribute nothing.

Details

Windows are taken from the text with all emoji blanked out, so a neighbouring emoji never lands in a context window and character offsets stay exact. With unit = "word" a token is a maximal run of non-whitespace characters, the same definition emoji_density() uses; with unit = "char" the window is a literal character count after trimming the whitespace next to the emoji.

Tokenisation stops there on purpose. If you need stemming, stopword removal or sentence splitting, pass the result to tokenizers or tidytext rather than expecting this verb to grow a tokeniser.

See also

emoji_collocations() for the corpus-level view; emoji_position() for where emoji sit in a text.

Examples

df <- data.frame(text = c("the coffee was cold \U0001f622 again",
                          "no emoji here"))
emoji_context(df, text, window = 2)
#> # A tibble: 1 × 6
#>   .row_number .position .emoji .emoji_context_left .emoji_context_right
#>         <int>     <int> <chr>  <chr>               <chr>               
#> 1           1        21 😢     was cold            again               
#> # ℹ 1 more variable: .emoji_context <chr>
emoji_context(df, text, window = 6, unit = "char")
#> # A tibble: 1 × 6
#>   .row_number .position .emoji .emoji_context_left .emoji_context_right
#>         <int>     <int> <chr>  <chr>               <chr>               
#> 1           1        21 😢     s cold              again               
#> # ℹ 1 more variable: .emoji_context <chr>