我是Python新手, 我使用R创建了一个术语文档矩阵,我想学习如何使用Python创建相同的文档矩阵。
我正在读取数据框Res_Desc_Train中可用的描述列中的文本数据。但不确定如何在python中使用创建文档术语矩阵的功能,如果有任何有助于学习的文档,将会很有帮助。
以下是我在R中使用的代码。
docs <- Corpus(VectorSource(Res_Desc_Train$Description))
docs <-tm_map(docs,content_transformer(tolower))
#remove potentially problematic symbols
toSpace <- content_transformer(function(x, pattern) { return (gsub(pattern, " ", x))})
removeSpecialChars <- function(x) gsub("[^a-zA-Z0-9 ]","",x)
docs <- tm_map(docs, toSpace, "/")
docs <- tm_map(docs, toSpace, "-")
docs <- tm_map(docs, toSpace, ":")
docs <- tm_map(docs, toSpace, ";")
docs <- tm_map(docs, toSpace, "@")
docs <- tm_map(docs, toSpace, "\\(" )
docs <- tm_map(docs, toSpace, ")")
docs <- tm_map(docs, toSpace, ",")
docs <- tm_map(docs, toSpace, "_")
docs <- tm_map(docs, content_transformer(removeSpecialChars))
docs <- tm_map(docs, content_transformer(tolower))
docs <- tm_map(docs, removeWords, stopwords("en"))
docs <- tm_map(docs, removePunctuation)
docs <- tm_map(docs, stripWhitespace)
docs <- tm_map(docs, removeNumbers)
#inspect(docs[440])
dataframe<-data.frame(text=unlist(sapply(docs, `[`, "content")), stringsAsFactors=F)
BigramTokenizer <-
function(x)
unlist(lapply(ngrams(words(x), 2), paste, collapse = " "), use.names = FALSE)
dtm <- DocumentTermMatrix(docs,control=list(stopwords=FALSE,wordLengths =c(2,Inf),tokenize = BigramTokenizer))
Weighteddtm <- weightTfIdf(dtm,normalize=TRUE)
mat.df <- as.data.frame(data.matrix(Weighteddtm), stringsAsfactors = FALSE)
mat.df <- cbind(mat.df, Res_Desc_Train$Group)
colnames(mat.df)[ncol(mat.df)] <- "Group"
Assignment.Distribution <- table(mat.df$Group)
Res_Desc_Train_Assign <- mat.df$Group
Assignment.Distribution <- table(mat.df$Group)
### Feature has different ranges, normalizing to bring ranges from 0 to 1
### Another way to standardize using z-scores
normalize <- function(x) {
y <- min(x)
z <- max(x)
temp <- x - y
temp1 <- (z - y)
temp2 <- temp / temp1
return(temp2)
}
#normalize(c(1,2,3,4,5))
num_col <- ncol(mat.df)-1
mat.df_normalize <- as.data.frame(lapply(mat.df[,1:num_col], normalize))
mat.df_normalize <- cbind(mat.df_normalize, Res_Desc_Train_Assign)
colnames(mat.df_normalize)[ncol(mat.df_normalize)] <- "Group"
答案 0 :(得分:1)
通常当你需要在python中处理文本时,最好的工具是NLTK。在您的特定情况下,有一个特定的python包创建term-document-matrix。此包称为Textmining。
此外,如果你需要使用正则表达式,你可以使用python的re
包。否则,您可以直接使用表示器形成NLTK。