Emoji co-occurrence counts, with an optional diagonal
Source:R/emoji-relate.R
emoji_cooccurrence.Rdemoji_cooccurrence() is emoji_pairs() under the name the matrix form
goes by, with one argument added and two taken away.
Usage
emoji_cooccurrence(
data,
text,
doc_id = NULL,
diagonal = FALSE,
sort = TRUE,
presentation = "emoji"
)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.- doc_id
Optional unquoted column identifying documents. Rows sharing a value are treated as one document. Default: each row is a document.
The result has a row per pair, so it grows with the square of the distinct emoji in a document: a day or a conversation is cheap, and pooling a whole corpus under one id is not. 800 distinct emoji in one document is 319,600 pairs and a few seconds; all 3963 would be 7.9 million.
- diagonal
If
TRUE, include oneitem1 == item2row per emoji with its document frequency. DefaultFALSE.- sort
If
TRUE(default), sort by descendingmeasure(by descendingnfor"count"; ties broken byn, thenitem1,item2, so the order is deterministic).FALSEsorts byitem1thenitem2instead, which is still a fixed order, computed in the C locale, not the order the pairs happened to be counted in.- 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.
Details
Added: diagonal = TRUE also returns the item1 == item2 rows, whose n
is the number of documents containing that emoji (the diagonal of the
co-occurrence matrix, i.e. its document frequency).
Taken away: there is no directed here. A co-occurrence matrix is
symmetric, so an ordered pair has no meaning on it and the diagonal this
verb exists to add would not either. Use emoji_pairs() when you want
directed = TRUE; the off-diagonal rows the two verbs return are otherwise
identical. Nor is there a measure: an association measure scores two
emoji against each other, and a diagonal cell has only one. Use
emoji_pairs() for the association-weighted edges.
Glyphs are canonicalised through the package's codepoint key, so two spellings of one emoji make one row rather than two; see Which spelling comes back in tidyEmoji.
Examples
df <- data.frame(text = c("\U0001f602\U0001f60d", "\U0001f602"))
emoji_cooccurrence(df, text, diagonal = TRUE)
#> # A tibble: 3 × 3
#> item1 item2 n
#> <chr> <chr> <int>
#> 1 😂 😂 2
#> 2 😂 😍 1
#> 3 😍 😍 1