如何在不删除R中的句点的情况下使用tm包删除标点符号?

时间:2015-09-29 08:13:04

标签: r tm

我正在使用tm包来删除标点符号。当我在句号和后续单词之间没有空格时,删除标点符号会删除句点并连接前一个单词

例如:

"transactions.Proceessed"  
"trnsaction.It"

使用tm包应用“删除标点符号”后,我得到如下输出:

"transactionsProceessed"  
"trnsactionIt"

是否可以在单词之间留出空格或保留句点,尽管使用删除标点符号功能?

更新

提供的示例是示例。输入文件是一个巨大的文本文件。我正在使用tm_map函数来删除标点符号。这是我使用的代码

# set parameters
candidates <- c("Obama", "Romney")
pathname <- "H:/datasets/"

# clean texts
cleanCorpus <- function(corpus){
  #corpus.tmp <- tm_map(corpus, removePunctuation)
  ##corpus.tmp <- gsub(".", " ", corpus, fixed = TRUE)
  f <- content_transformer(function(x, pattern) sub(pattern, " ", s.cor  ))
  corpus.tmp <- tm_map(s.cor, f, "[[:punct:]]")
  corpus.tmp <- tm_map(corpus.tmp, stripWhitespace)
  corpus.tmp <- tm_map(corpus.tmp, content_transformer(tolower))
  ##corpus.tmp <- tm_map(corpus.tmp, stemDocument)
  corpus.tmp <- tm_map(corpus.tmp, removeWords, stopwords("english"))
  return(corpus.tmp)
}
# create text document matrix
generateTDM <- function(cand, path){
  s.dir <- sprintf("%s/%s", path, cand)
  s.cor <- Corpus(DirSource(directory = s.dir, encoding = "UTF-8"))
  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)
}
# execute function and create a Text Document Matrix
tdm <- lapply(candidates, generateTDM, path = pathname)

............................................... .............................

1 个答案:

答案 0 :(得分:2)

此(这些)解决方案适用于您的第一个选项(删除句号,一般来说,所有标点符号并在其间添加空格):

如果您的输入与示例一样简单,您可以尝试sub来自基地:

sub(".", " ", "transactions.Proceessed", fixed=TRUE)
#[1] "transactions Proceessed"
sub(".", " ", "trnsaction.It", fixed=TRUE)
#[1] "trnsaction It"
x <- c("transactions.Processed", "trnsaction.It")
sub(".", " ", x, fixed=TRUE)
#[1] "transactions Processed" "trnsaction It"
#this one should remove all punctuation
sub("[[:punct:]]", " ",x)
#[1] "transactions Processed" "trnsaction It" 

课程VCorpusCorpus的对象的想法是相同的,但您必须使用content_transformer来执行此操作:

#You would have to switch to your actual corpus
x <- c("transactions.Processed", "trnsaction.It")
sub("[[:punct:]]", " ",x)
#[1] "transactions Processed" "trnsaction It"         
xx <- VCorpus(VectorSource(x))
f <- content_transformer(function(x, pattern) sub(pattern, " ", x))
xx2 <- tm_map(xx, f, "[[:punct:]]")

在这里你可以看到完整的结构:

xx2
#    List of 2
# $ 1:List of 2
#  ..$ content: chr "transactions Processed"
#  ..$ meta   :List of 7
#  .. ..$ author       : chr(0) 
#  .. ..$ datetimestamp: POSIXlt[1:1], format: "2015-09-29 09:24:42"
#  .. ..$ description  : chr(0) 
#  .. ..$ heading      : chr(0) 
#  .. ..$ id           : chr "1"
#  .. ..$ language     : chr "en"
#  .. ..$ origin       : chr(0) 
#  .. ..- attr(*, "class")= chr "TextDocumentMeta"
#  ..- attr(*, "class")= chr [1:2] "PlainTextDocument" "TextDocument"
# $ 2:List of 2
#  ..$ content: chr "trnsaction It"
#  ..$ meta   :List of 7
#  .. ..$ author       : chr(0) 
#  .. ..$ datetimestamp: POSIXlt[1:1], format: "2015-09-29 09:24:42"
#  .. ..$ description  : chr(0) 
#  .. ..$ heading      : chr(0) 
#  .. ..$ id           : chr "2"
#  .. ..$ language     : chr "en"
#  .. ..$ origin       : chr(0) 
#  .. ..- attr(*, "class")= chr "TextDocumentMeta"
#  ..- attr(*, "class")= chr [1:2] "PlainTextDocument" "TextDocument"
# - attr(*, "class")= chr [1:2] "VCorpus" "Corpus"

或只是内容:

xx2[[1]][1]
#$content
#[1] "transactions Processed"

xx2[[2]][1]
#$content
#[1] "trnsaction It"