Skip to content

Instantly share code, notes, and snippets.

@sebastianrothbucher
Last active January 30, 2018 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianrothbucher/eb14e4223bbf4873973ae6992bad6d03 to your computer and use it in GitHub Desktop.
Save sebastianrothbucher/eb14e4223bbf4873973ae6992bad6d03 to your computer and use it in GitHub Desktop.
Song sentiments in R - improved and compared
library(tidytext)
library(tm)
stp <- stopwords()
dfAll <- data.frame(sent=c(), cnt=c(), song=c())
s <- c('love', 'nationalanthem', 'outofthewoods', 'power')
for(f in s){
natL <- readLines(paste(f, '.txt', sep=""))
nat <- tolower(unlist(strsplit(natL, "\\s|[,?\\.()]")))
df <- data.frame(table(nat))
dfF <- df[(df$nat!='' & is.na(match(df$nat, stp))),]
dfM <- merge(dfF, sentiments, by.x=c('nat'), by.y=c('word'))
dfMA <- aggregate(Freq~nat+sentiment, data=dfM, FUN=max) # !!!
dfAgg <- aggregate(Freq~sentiment, data=dfMA, FUN=sum)
colnames(dfAgg) <- c('sent', 'cnt')
dfAgg$song <- f
dfAll <- rbind(dfAll, dfAgg)
}
dfDim <- data.frame(sent=unique(dfAll$sent))
for(f in s){
dfSlc <- dfAll[dfAll$song==f, c('sent', 'cnt')]
colnames(dfSlc) <- c('sent', f)
dfDim <- merge(dfDim, dfSlc, by=c('sent'), all=TRUE)
}
rownames(dfDim) <- dfDim$sent
dfDimM <- as.matrix(dfDim[,c(2:ncol(dfDim))])
barplot(t(dfDimM), beside=TRUE, legend=TRUE, cex.names=0.5)
library(tidytext)
library(tm)
library(shiny)
stp <- stopwords()
dfAll <- data.frame(sent=c(), cnt=c(), song=c())
s <- c('love', 'nationalanthem', 'outofthewoods', 'power')
for(f in s){
natL <- readLines(paste(f, '.txt', sep=""))
nat <- tolower(unlist(strsplit(natL, "\\s|[,?\\.()]")))
df <- data.frame(table(nat))
dfF <- df[(df$nat!='' & is.na(match(df$nat, stp))),]
dfM <- merge(dfF, sentiments, by.x=c('nat'), by.y=c('word'))
dfMA <- aggregate(Freq~nat+sentiment, data=dfM, FUN=max) # !!!
dfAgg <- aggregate(Freq~sentiment, data=dfMA, FUN=sum)
colnames(dfAgg) <- c('sent', 'cnt')
dfAgg$song <- f
dfAll <- rbind(dfAll, dfAgg)
}
dfDim <- data.frame(sent=unique(dfAll$sent))
for(f in s){
dfSlc <- dfAll[dfAll$song==f, c('sent', 'cnt')]
colnames(dfSlc) <- c('sent', f)
dfDim <- merge(dfDim, dfSlc, by=c('sent'), all=TRUE)
}
rownames(dfDim) <- dfDim$sent
dfDimM <- as.matrix(dfDim[,c(2:ncol(dfDim))])
#barplot(t(dfDimM), beside=TRUE, legend=TRUE, cex.names=0.5)
shinyApp(
ui = fluidPage(sidebarLayout(
sidebarPanel(verticalLayout(
selectInput(
"songs",
"Choose one or more songs:",
colnames(dfDimM),
multiple = TRUE,
selected = colnames(dfDimM)
),
selectInput(
"sentiments",
"Choose one or more sentiments:",
rownames(dfDimM),
multiple = TRUE,
selected = rownames(dfDimM),
selectize = FALSE,
size = nrow(dfDimM)
)
)),
mainPanel(plotOutput("plot"))
)),
server = function(input, output) {
output$plot <-
renderPlot(barplot(t(dfDimM[input$sentiments, input$songs]), beside = TRUE, legend = TRUE,))
}
)
library(shiny)
library(tidytext)
library(tm)
stp <- stopwords()
dfAll <- data.frame(sent=c(), cnt=c(), song=c())
dfDim <- NULL
analyzeNew <- function (uploadDf) {
for(i in c(1:nrow(uploadDf))){
natL <- readLines(uploadDf$datapath[i])
nat <- tolower(unlist(strsplit(natL, "\\s|[,?\\.()]")))
df <- data.frame(table(nat))
dfF <- df[(df$nat!='' & is.na(match(df$nat, stp))),]
dfM <- merge(dfF, sentiments, by.x=c('nat'), by.y=c('word'))
dfMA <- aggregate(Freq~nat+sentiment, data=dfM, FUN=max) # !!!
dfAgg <- aggregate(Freq~sentiment, data=dfMA, FUN=sum)
colnames(dfAgg) <- c('sent', 'cnt')
dfAgg$song <- sub("\\....$", "", uploadDf$name[i])
dfAll <<- rbind(dfAll, dfAgg)
}
dfDim <<- data.frame(sent=unique(dfAll$sent))
for(s in unique(dfAll$song)){
dfSlc <- dfAll[dfAll$song==s, c('sent', 'cnt')]
colnames(dfSlc) <- c('sent', s)
dfDim <<- merge(dfDim, dfSlc, by=c('sent'), all=TRUE)
}
rownames(dfDim) <<- dfDim$sent
}
vals <- reactiveValues(redraw = 0, selSong = c(), selSent = c())
shinyApp(
ui = fluidPage(sidebarLayout(
sidebarPanel(verticalLayout(
uiOutput("songSelection"),
uiOutput("sentimentSelection"),
fileInput(
"addFile",
"Upload text file w/ song:",
accept = "text/plain"
)
)),
mainPanel(plotOutput("plot"))
)),
server = function(input, output, session) {
observeEvent(input$addFile, {
oldSong <- unique(dfAll$song)
oldSent <- unique(dfAll$sent)
analyzeNew(input$addFile)
newSong <- unique(dfAll$song)
newSent <- unique(dfAll$sent)
vals$selSong <- c(vals$selSong, setdiff(newSong, oldSong))
vals$selSent <- c(vals$selSent, setdiff(newSent, oldSent))
vals$redraw <- vals$redraw + 1
}, ignoreNULL = TRUE, ignoreInit = TRUE)
observe({
output$songSelection <- renderUI(selectInput("songs", "Choose uploaded songs:", unique(dfAll$song), multiple = TRUE, selected = vals$selSong))
output$sentimentSelection <- renderUI(selectInput("sentiments", "Choose one or more sentiments:", unique(dfAll$sent), size = length(unique(dfAll$sent)), multiple = TRUE, selectize = FALSE, selected = vals$selSent))
if(vals$redraw > 0 && length(vals$selSong) > 0 && length(vals$selSent) > 0){
output$plot <- renderPlot(barplot(t(as.matrix(dfDim[vals$selSent, vals$selSong])), names.arg=vals$selSent, beside = TRUE, legend = TRUE))
}
})
observeEvent(input$songs, {
vals$selSong <- input$songs
}, ignoreInit = TRUE, priority = -1)
observeEvent(input$sentiments, {
vals$selSent <- input$sentiments
}, ignoreInit = TRUE, priority = -1)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment