Difference between revisions of "R: tidytext RPJP BAPPENAS"

From OnnoWiki
Jump to navigation Jump to search
(Created page with " install.packages("pdftools") library(pdftools) rpjp2005 <- pdf_text("RPJP_2005-2025.pdf") %>% strsplit(split = "\n") original_rpjp2005 <- rpjp2005 %>% group_by(book...")
 
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
install.packages("rJava")
 +
install.packages("xlsx")
 +
install.packages("tm")
 +
install.packages("wordcloud")
 +
install.packages("ggplot2")
 +
install.packages("RWeka")
 +
 +
library(xlsx)
 +
library(tm)
 +
library(wordcloud)
 +
library(ggplot2)
  
 +
library(tidyverse)
 +
library(tidytext)
 +
library(RWeka)
 +
library(tm)
 +
directory <- "data-pdf"
 +
 +
# create corpus from pdfs
 +
docs <- VCorpus(DirSource(directory), readerControl = list(reader = readPDF))
  
  install.packages("pdftools")
+
  # docs <- VCorpus(DirSource("data", recursive=TRUE))
library(pdftools)
+
  # Get the document term matrices
  rpjp2005 <- pdf_text("RPJP_2005-2025.pdf") %>% strsplit(split = "\n")
 
  
 +
# dengan Stemming
 +
#
 +
BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
 +
dtm_unigram <- DocumentTermMatrix(docs, control = list(tokenize="words",
 +
    removePunctuation = TRUE,
 +
    stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan"),
 +
    stemming = TRUE))
 +
dtm_bigram <- DocumentTermMatrix(docs, control = list(tokenize = BigramTokenizer,
 +
    removePunctuation = TRUE,
 +
    stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan"),
 +
    stemming = TRUE))
  
  original_rpjp2005 <- rpjp2005 %>%
+
  # tanpa Stemming
  group_by(book) %>%
+
#
  mutate(linenumber = row_number(),
+
BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
          chapter = cumsum(str_detect(text, regex("^chapter [\\divxlc]",
+
dtm_unigram <- DocumentTermMatrix(docs, control = list(tokenize="words",
                                                  ignore_case = TRUE)))) %>%
+
    removePunctuation = TRUE,  
  ungroup()
+
    stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan")))
  original_books
+
dtm_bigram <- DocumentTermMatrix(docs, control = list(tokenize = BigramTokenizer,
 +
    removePunctuation = TRUE,
 +
    stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan")))
 +
 
 +
inspect(dtm_unigram)
 +
  inspect(dtm_bigram)
  
  

Latest revision as of 12:20, 26 November 2019

install.packages("rJava")
install.packages("xlsx")
install.packages("tm")
install.packages("wordcloud")
install.packages("ggplot2")
install.packages("RWeka")

library(xlsx)
library(tm)
library(wordcloud)
library(ggplot2)
library(tidyverse)
library(tidytext)
library(RWeka)
library(tm)
directory <- "data-pdf"

# create corpus from pdfs
docs <- VCorpus(DirSource(directory), readerControl = list(reader = readPDF))
# docs <- VCorpus(DirSource("data", recursive=TRUE))
# Get the document term matrices
# dengan Stemming
#
BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
dtm_unigram <- DocumentTermMatrix(docs, control = list(tokenize="words", 
    removePunctuation = TRUE, 
    stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan"),
    stemming = TRUE))
dtm_bigram <- DocumentTermMatrix(docs, control = list(tokenize = BigramTokenizer,
    removePunctuation = TRUE,
    stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan"),
    stemming = TRUE))
# tanpa Stemming
#
BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
dtm_unigram <- DocumentTermMatrix(docs, control = list(tokenize="words", 
    removePunctuation = TRUE, 
    stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan")))
dtm_bigram <- DocumentTermMatrix(docs, control = list(tokenize = BigramTokenizer,
    removePunctuation = TRUE,
    stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan")))
inspect(dtm_unigram)
inspect(dtm_bigram)


Pranala Menarik