Difference between revisions of "R: tidytext RPJP BAPPENAS"

From OnnoWiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
install.packages("rJava")
 
  install.packages("xlsx")
 
  install.packages("xlsx")
 
  install.packages("tm")
 
  install.packages("tm")
 
  install.packages("wordcloud")
 
  install.packages("wordcloud")
 
  install.packages("ggplot2")
 
  install.packages("ggplot2")
 
+
install.packages("RWeka")
 +
 
  library(xlsx)
 
  library(xlsx)
 
  library(tm)
 
  library(tm)
Line 11: Line 13:
 
  library(tidyverse)
 
  library(tidyverse)
 
  library(tidytext)
 
  library(tidytext)
 +
library(RWeka)
 
  library(tm)
 
  library(tm)
 
  directory <- "data-pdf"
 
  directory <- "data-pdf"
Line 20: Line 23:
 
  # Get the document term matrices
 
  # Get the document term matrices
  
 +
# dengan Stemming
 +
#
 
  BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
 
  BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
 
  dtm_unigram <- DocumentTermMatrix(docs, control = list(tokenize="words",  
 
  dtm_unigram <- DocumentTermMatrix(docs, control = list(tokenize="words",  
Line 29: Line 34:
 
     stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan"),
 
     stopwords = c(stopwords::stopwords("id", source = "stopwords-iso"),"tabel","pada","dan"),
 
     stemming = TRUE))
 
     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_unigram)
 
  inspect(dtm_bigram)
 
  inspect(dtm_bigram)
 
 
converted %>%
 
  tidy() %>%
 
  filter(!grepl("[0-9]+", term))
 
# converted adalah DocumentTermMatrix
 
 
 
  
  

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