Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.52 KB | None | 0 0
  1. library(shiny)
  2. library(tm)
  3. library(SnowballC)
  4. library(wordcloud)
  5. library(RColorBrewer)
  6. library(pdftools)
  7. library(DT)
  8. library(stringr)
  9.  
  10. server <- function(input, output) {
  11.  
  12.  
  13.  
  14.   output$plot <- renderPlot({
  15.  
  16.     file1 <- input$file1
  17.     file2 <- input$file2
  18.  
  19.     if (is.null(file1) && is.null(file2))
  20.     return(NULL)
  21.     if (!is.null(file1)) {
  22.       in2 <- pdf_text(file1$datapath)
  23.     }
  24.     if (!is.null(file2)) {
  25.       in2 <- readLines(file2$datapath)
  26.     }
  27.    
  28.     docs <- Corpus(VectorSource(in2))
  29.    
  30.    
  31.     toSpace <-
  32.       content_transformer(function (x , pattern)
  33.         gsub(pattern, " ",
  34.              x))
  35.     docs <- tm_map(docs, toSpace, "/")
  36.     docs <- tm_map(docs, toSpace, "@")
  37.     docs <- tm_map(docs, toSpace, "\\|")
  38.     docs <- tm_map(docs, content_transformer(tolower))
  39.     docs <- tm_map(docs, removeNumbers)
  40.     docs <- tm_map(docs, removeWords, stopwords("dutch"))
  41.     docs <- tm_map(docs, removeWords, stopwords("english"))
  42.     docs <- tm_map(docs, removePunctuation)
  43.     docs <- tm_map(docs, stripWhitespace)
  44.    
  45.     dtm <- TermDocumentMatrix(docs)
  46.     m <- as.matrix(dtm)
  47.     v <- sort(rowSums(m), decreasing = TRUE)
  48.     d <- data.frame(word = names(v), freq = v)
  49.    
  50.     wordcloud_rep <- repeatable(wordcloud)
  51.    
  52.     wordcloud_rep(
  53.       words = d$word,
  54.       freq = d$freq,
  55.       min.freq = input$freq,
  56.       scale = c(2, 0.5),
  57.       max.words = input$max,
  58.       random.order = FALSE,
  59.       rot.per = 0.35,
  60.       colors = brewer.pal(8, "Dark2")
  61.     )
  62.    
  63.   })
  64.   output$table <- renderDataTable({
  65.     file1 <- input$file1
  66.     file2 <- input$file2
  67.     if (is.null(file1) &&
  68.         is.null(file2))
  69.       return(NULL)
  70.    
  71.     if (!is.null(file1)) {
  72.       in2 <- pdf_text(file1$datapath)
  73.     }
  74.     if (!is.null(file2)) {
  75.       in2 <- readLines(file2$datapath)
  76.     }
  77.    
  78.     docs <- Corpus(VectorSource(in2))
  79.    
  80.    
  81.     toSpace <-
  82.       content_transformer(function (x , pattern)
  83.         gsub(pattern, " ",
  84.              x))
  85.     docs <- tm_map(docs, toSpace, "/")
  86.     docs <- tm_map(docs, toSpace, "@")
  87.     docs <- tm_map(docs, toSpace, "\\|")
  88.     docs <- tm_map(docs, content_transformer(tolower))
  89.     docs <- tm_map(docs, removeNumbers)
  90.     docs <- tm_map(docs, removeWords, stopwords("dutch"))
  91.     docs <- tm_map(docs, removeWords, stopwords("english"))
  92.     docs <- tm_map(docs, removePunctuation)
  93.     docs <- tm_map(docs, stripWhitespace)
  94.    
  95.  
  96.     dtm <-
  97.       TermDocumentMatrix(docs)
  98.     m <- as.matrix(dtm)
  99.     v <- sort(rowSums(m), decreasing = TRUE)
  100.     as.data.frame(findAssocs(dtm, terms = input$v, corlimit = 0.3))
  101.    
  102.   })
  103.  
  104.   output$sent <- renderText({
  105.     file1 <- input$file1
  106.     file2 <- input$file2
  107.     neg   <- input$neg
  108.     pos   <- input$pos
  109.     if (is.null(file1) && is.null(file2))
  110.       return(NULL)
  111.    
  112.     if (!is.null(file1)){
  113.       in2 <- pdf_text(file1$datapath)
  114.     }
  115.     if (!is.null(file2)){
  116.       in2 <- readLines(file2$datapath)
  117.     }
  118.     if (!is.null(neg)) {
  119.       neg <- readLines(neg$datapath)
  120.     }
  121.     if (!is.null(pos)) {
  122.       pos <- readLines(pos$datapath)
  123.     }
  124.    
  125.     text <- in2
  126.     text <- str_split(text, pattern="\\s+")
  127.     text <- unlist(text)
  128.    
  129.     pos.sum <- sum(!is.na(match(text,pos)))
  130.     neg.sum <- sum(!is.na(match(text,neg)))
  131.    
  132.     negposr <- pos.sum/neg.sum
  133.     print(negposr)
  134.    
  135. })
  136.  
  137.  
  138.   output$`2` <- renderDataTable({
  139.     file1 <- input$file1
  140.     file2 <- input$file2
  141.    
  142.     if (is.null(file1) &&
  143.         is.null(file2))
  144.       return(NULL)
  145.    
  146.     if (!is.null(file1)) {
  147.       in2 <- pdf_text(file1$datapath)
  148.     }
  149.     if (!is.null(file2)) {
  150.       in2 <- readLines(file2$datapath)
  151.     }
  152.    
  153.     text <- in2
  154.     docs <- Corpus(VectorSource(text))
  155.    
  156.     toSpace <-
  157.       content_transformer(function (x , pattern)
  158.         gsub(pattern, " ",
  159.              x))
  160.     text <-
  161.       tm_map(docs, toSpace, "/")
  162.     text <-
  163.       tm_map(text, toSpace, "@")
  164.     text <-
  165.       tm_map(text, toSpace, "\\|")
  166.     text <-
  167.       tm_map(text, content_transformer(tolower))
  168.     text <-
  169.       tm_map(text, removeNumbers)
  170.     text <-
  171.       tm_map(text, removeWords, stopwords("dutch"))
  172.     text <-
  173.       tm_map(text, removeWords, stopwords("english"))
  174.     text <-
  175.       tm_map(text, removePunctuation)
  176.     text <-
  177.       tm_map(text, stripWhitespace)
  178.    
  179.     dtm <-
  180.       TermDocumentMatrix(text)
  181.     m <- as.matrix(dtm)
  182.     v <- sort(rowSums(m), decreasing = TRUE)
  183.     d <- data.frame(word = names(v),freq=v)
  184.     head(d, 10)
  185.    
  186.   })
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement