~dricottone/afinn-demo

474a8fc4f9dc6961f36bf3ad8c5ae5370847695a — Dominic Ricottone 1 year, 22 days ago 866aea9
R demo
2 files changed, 37 insertions(+), 0 deletions(-)

A r/Makefile
A r/main.r
A r/Makefile => r/Makefile +11 -0
@@ 0,0 1,11 @@
.PHONY: install
install:
	sudo pacman -S r-tidyr r-tidytext r-textdata
	@echo "Interactively run R and call the following commands:"
	@echo "> library(tidytext)"
	@echo "> get_sentiments(\"afinn\")"

.PHONY: run
run:
	Rscript main.r


A r/main.r => r/main.r +26 -0
@@ 0,0 1,26 @@
#!/usr/bin/env Rscript

library(tidytext) # for data, unnest_tokens
data(stop_words)

library(janeaustenr) # for austen_books

library(dplyr) # for group_by, anti_join, inner_join, count, mutate

library(tidyr) # for pivot_wider

tidy_books <- austen_books() %>%
  group_by(book) %>%
  unnest_tokens(word, text) %>%
  anti_join(stop_words)


get_sentiments("afinn")

sentiment <- tidy_books %>%
  inner_join(get_sentiments("afinn")) %>%
  summarise(sentiment = sum(value)) %>%
  mutate(method = "AFINN")

sentiment