user3465732
user3465732

Reputation: 1

document classification with r

I have been trying to follow the document classification tutorial on YouTube using R and it's really interesting, but when I tried to run the first part of the script I keep getting this error Error in FUN(c("obama", "romney")[[1L]], ...) : could not find function "corpus". I really don't know why that is, but I am hoping someone could help me figure it out.

This is the script:

#init
libs <- c("tm", "plyr", "class")
lapply(libs, require, character.only = TRUE)

# set options
options(stringAsFactors = FALSE)

#set parameters
candidates <- c("obama","romney")
pathname <- "C:\\Users\\admin\\Documents\\speeches"

#clean text
  cleanCorpus <- function(corpus){
  corpus.tmp <- tm_map(corpus, removePunctuation)
  corpus.tmp <- tm_map(corpus.tmp, stripWhitespace)
  corpus.tmp <- tm_map(corpus.tmp, tolower)
  corpus.tmp <- tm_map(corpus, removeWords, stopWords("english"))
  return(corpus.tmp)
 }

#Build TDM
 generateTDM <- function(cand, path){
  s.dir <- sprintf("%s/%s", path, cand)
  s.cor <- corpus(DirSource(directory = s.dir, encoding = "ANSI"))
  s.cor.cl <- cleanCorpus(s.cor)
  s.tdm <-TermDocumentMatrix(s.cor.cl)

  s.tdm <- removeSparseTerms(s.tdm, 0.7)
  result <- list(name = cand,  tdm = s.tdm)
}

tdm <- lapply(candidates, generateTDM, path = pathname)

Upvotes: 0

Views: 1729

Answers (1)

Ashlesh
Ashlesh

Reputation: 357

your path name should be

pathname <- "C:/Users/admin/Documents/speeches"

Note: there is forward slash in pathname

Upvotes: 2

Related Questions