library(tm.plugin.webmining) library(purrr) library(tidyverse) library(tidytext) company <- c("Microsoft", "Apple") symbol <- c("MSFT", "AAPL") download_articles <- function(symbol) { WebCorpus(GoogleFinanceSource(paste0("NASDAQ:", symbol))) } stock_articles <- data_frame(company = company, symbol = symbol) %>% mutate(corpus = map(symbol, download_articles)) stock_tokens <- stock_articles %>% unnest(map(corpus, tidy)) %>% unnest_tokens(word, text) %>% select(company, datetimestamp, word, id, heading) library(stringr) stock_tf_idf <- stock_tokens %>% count(company, word) %>% filter(!str_detect(word, "\\d+")) %>% bind_tf_idf(word, company, n) %>% arrange(-tf_idf)
stock_tokens %>% count(word) %>% inner_join(get_sentiments("loughran"), by = "word") %>% group_by(sentiment) %>% top_n(5, n) %>% ungroup() %>% mutate(word = reorder(word, n)) %>% ggplot(aes(word, n)) + geom_col() + coord_flip() + facet_wrap(~ sentiment, scales = "free") + ylab("Frequency of this word in the recent financial articles")
stock_sentiment_count <- stock_tokens %>% inner_join(get_sentiments("loughran"), by = "word") %>% count(sentiment, company) %>% spread(sentiment, n, fill = 0) stock_sentiment_count %>% mutate(score = (positive - negative) / (positive + negative)) %>% mutate(company = reorder(company, score)) %>% ggplot(aes(company, score, fill = score > 0)) + geom_col(show.legend = FALSE) + coord_flip() + labs(x = "Company", y = "Positivity score among 20 recent news articles")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.