juliasilge / tidytext
1
#' Sentiment lexicon from Bing Liu and collaborators
2
#'
3
#' Lexicon for opinion and sentiment analysis in a tidy data frame. This
4
#' dataset is included in this package with permission of the creators, and
5
#' may be used in research, commercial, etc. contexts with attribution, using
6
#' either the paper or URL below.
7
#'
8
#' This lexicon was first published in:
9
#'
10
#' Minqing Hu and Bing Liu, ``Mining and summarizing customer reviews.'',
11
#' Proceedings of the ACM SIGKDD International Conference on Knowledge Discovery
12
#' & Data Mining (KDD-2004), Seattle, Washington, USA, Aug 22-25, 2004.
13
#'
14
#' Words with non-ASCII characters were removed.
15
#'
16
#' @format A data frame with 6,786 rows and 2 variables:
17
#' \describe{
18
#'  \item{word}{An English word}
19
#'  \item{sentiment}{A sentiment for that word, either positive or negative.}
20
#' }
21
#'
22
#'
23
#' @source \url{https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html}
24
"sentiments"
25

26

27
#' Get a tidy data frame of a single sentiment lexicon
28
#'
29
#' Get specific sentiment lexicons in a tidy format, with one row per word,
30
#' in a form that can be joined with a one-word-per-row dataset.
31
#' The \code{"bing"} option comes from the included \code{\link{sentiments}}
32
#' data frame, and others call the relevant function in the \pkg{textdata}
33
#' package.
34
#'
35
#' @param lexicon The sentiment lexicon to retrieve;
36
#' either "afinn", "bing", "nrc", or "loughran"
37
#'
38
#' @return A tbl_df with a \code{word} column, and either a \code{sentiment}
39
#' column (if \code{lexicon} is not "afinn") or a numeric \code{value} column
40
#' (if \code{lexicon} is "afinn").
41
#'
42
#' @examples
43
#'
44
#' library(dplyr)
45
#'
46
#' get_sentiments("bing")
47
#'
48
#' \dontrun{
49
#' get_sentiments("afinn")
50
#' get_sentiments("nrc")
51
#' }
52
#'
53
#' @export
54
get_sentiments <- function(lexicon = c("bing", "afinn", "loughran", "nrc")) {
55 1
  lexicon <- match.arg(lexicon)
56

57 1
  lexicon_names <- list(afinn    = "AFINN",
58 1
                        loughran = "Loughran-McDonald",
59 1
                        nrc      = "NRC word-emotion association")
60

61 1
  if (lexicon != "bing" && !requireNamespace("textdata", quietly = TRUE)) {
62 0
    msg <- "The textdata package is required to download the {lexicon_names[[lexicon]]} lexicon.\nInstall the textdata package to access this dataset."
63 0
    stop(stringr::str_glue(msg), call. = FALSE)
64
  }
65

66
  switch(
67 1
    lexicon,
68 0
    afinn    = textdata::lexicon_afinn(),
69 0
    nrc      = textdata::lexicon_nrc(),
70 0
    loughran = textdata::lexicon_loughran(),
71 1
    bing     = tidytext::sentiments,
72 0
    stop("Unexpected lexicon", call. = FALSE)
73
  )
74
}

Read our documentation on viewing source code .

Loading