2

我正在尝试使用预先确定的术语构建文档术语矩阵。语料库在变量 cname 中标识,具有预标识术语的文件被读入 terms 变量,然后转换为列表。当我运行下面的代码时,我得到一个空的 DTM。我在下面使用的代码。关于我做错了什么有什么想法吗?谢谢!!!

汤姆

library(tm) 
library(Rmpfr) 
library(stm)

#Loading Documents
cname <- file.path("", "corpus", "goodsmoklss")
library(tm)
corp <- VCorpus(DirSource(cname))

#Transformations
docs<-tm_map(corp,tolower) #AllLowerCase
docs<-tm_map(corp,removeNumbers) #RemoveNumbers

#Remove Stopwords like is, was, the etc
docs<-tm_map(corp, removeWords, stopwords("english"))

#make Sure it is a PLainTextDocument
documents<-tm_map(docs,PlainTextDocument)


#read in list of preidentified terms
terms=read.delim("C:/corpus/TermList.csv", header=F, stringsAsFactor=F)
tokenizing.phrases <- c(terms)

library("RWeka")

phraseTokenizer <- function(x) {
  require(stringr)

  x <- as.character(x) # extract the plain text from TextDocument object
  x <- str_trim(x)
  if (is.na(x)) return("")

  phrase.hits <- str_detect(x, coll(tokenizing.phrases))


  if (any(phrase.hits)) {
    # only split once on the first hit, so we don't have to worry about    #multiple occurences of the same phrase
    split.phrase <- tokenizing.phrases[which(phrase.hits)[1]] 
    #warning(paste("split phrase:", split.phrase))
    temp <- unlist(str_split(x, coll(split.phrase), 2))
    out <- c(phraseTokenizer(temp[1]), split.phrase, phraseTokenizer(temp[2])) 
  } else {
    #out <- MC_tokenizer(x)
    out <- " "
  }

  # get rid of any extraneous empty strings, which can happen if a phrase occurs just before a punctuation
  out[out != ""]
}

dtm <- DocumentTermMatrix(documents, control = list(tokenize = phraseTokenizer))
4

1 回答 1

0

我对 TM 不太熟悉,但在 quanteda 包中,您可以简单地进行子集化或过滤。这里应该适用同样的原则。我认为您应该能够构建 DTM,然后简单地根据您感兴趣的术语向量进行过滤。首先按照上述方式制作您的 DTM。

v <- ("your","terms","here")
to_filter <- colnames(dtm)

#then you can simply filter based on the vector
dtm2 <- dtm[,to_filter %in% v]

您可能需要先考虑对字典和语料库进行词干处理。如果语料库有很多术语和文档,内存可能是个问题。

于 2016-01-08T03:39:53.787 回答