calc_bing_word_counts: Counts of words with a positive or negative sentiment

Description Usage Arguments Value Note References Examples

View source: R/calc_bing_word_counts.R

Description

Count the number of times a word with a positive or negative sentiment occurs in a given text.

Usage

1
2
3
4
5
6
calc_bing_word_counts(
  x,
  target_col_name = NULL,
  text_col_name,
  filter_class = NULL
)

Arguments

x

A data frame with one or more columns: the column with the classes (if target_col_name is not NULL); and the column with the text. Any other columns will be ignored.

target_col_name

A string with the column name of the target variable. Defaults to NULL.

text_col_name

A string with the column name of the text variable.

filter_class

A string or vector of strings with the name(s) of the class(es) for which to count the words. Defaults to NULL (all rows).

Value

A data frame with three columns: word; sentiment ("positive" or "negative"- see Hu & Liu, 2004); and count.

Note

When supplying more than one class in filter_class, the returned data frame will NOT separate the results for the different classes. If separation is desired, then run the function for each class separately or do something like this:

1
2
3
4
5
6
7
8
9
# Assuming that the class and text columns are called "label" and
# "feedback" respectively
x %>%
    split(.$label) %>%
    purrr::map(
        ~ calc_bing_word_counts(., target_col_name = NULL,
                               text_col_name = "feedback",
                               filter_class = NULL)
    )

References

Hu M. & Liu B. (2004). Mining and summarizing customer reviews. Proceedings of the ACM SIGKDD International Conference on Knowledge Discovery & Data Mining (KDD-2004), Seattle, Washington, USA, Aug 22-25, 2004.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
library(experienceAnalysis)
books <- janeaustenr::austen_books() # Jane Austen books
emma <- paste(books[books$book == "Emma", ], collapse = " ") # String with whole book
pp <- paste(books[books$book == "Pride & Prejudice", ], collapse = " ") # String with whole book

# Make data frame with books Emma and Pride & Prejudice
x <- data.frame(
  text = c(emma, pp),
  book = c("Emma", "Pride & Prejudice")
)

# Word counts for both books
calc_bing_word_counts(x, target_col_name = "book", text_col_name = "text",
                      filter_class = NULL) %>%
  head()

# Word counts for Emma
calc_bing_word_counts(x, target_col_name = "book", text_col_name = "text",
                      filter_class = "Emma") %>%
  head()

# Word counts for Pride & Prejudice
calc_bing_word_counts(x, target_col_name = "book", text_col_name = "text",
                      filter_class = "Pride & Prejudice") %>%
  head()

CDU-data-science-team/experienceAnalysis documentation built on Dec. 17, 2021, 12:53 p.m.