У меня возникла проблема с токенизацией Bigram, отображающей те же результаты, что и с токенизацией ngram. Он продолжает показывать только слово против двух слов на графике. Я новичок в R, и поэтому я пытался следовать этому уроку:
https://rstudio -pubs-static.s3.amazonaws.com / 40817_63c8586e26ea49d0a06bcba4e794e43d.html
Я использовал большую часть кода из поста, но также пытался использовать его из другого источника, с которым я играл. Я читал, что замена корпуса на VCorpus прояснила эту проблему для нескольких человек. Я попытался изменить этот код: corpus <- Corpus(review_source) to corpus <- VCorpus(review_source)
с теми же результатами. Любое руководство будет оценено. Вот код, с которым я сейчас работаю
##Load in the data setwd("G:\\Customer Analytics\\AdHoc")
reviews <- read.csv("Reseller Ratings_clean.csv", stringsAsFactors = FALSE,
header=TRUE)
review_text <- paste(reviews$reviews)
#setting up source and corpus
review_source <- VectorSource(review_text)
corpus <- Corpus(review_source)
########################
# Data Cleaning - Applying Transformations
########################
toSpace <- content_transformer(function(x, pattern) gsub(pattern, " ", x))
corpus <- tm_map(corpus, toSpace, "/|@|//|$|:|:)|*|&|!|?|_|-|#|") ##
replace special characters by space
corpus <- tm_map(corpus, content_transformer(tolower)) # Conversion to Lower
Case
corpus <- tm_map(corpus, removePunctuation) # Punctuation can provide
gramatical context which supports
corpus <- tm_map(corpus, removeWords, stopwords("english")) # common stop
Words like for, very, and, of, are, etc,
corpus <- tm_map(corpus, removeWords, c("the", "will", "The", "also",
"that", "and", "for", "in", "is", "it", "not", "to"))
corpus <- tm_map(corpus, removeNumbers) # removal of numbers
corpus <- tm_map(corpus, stripWhitespace) # removal of whitespace
corpus <- tm_map(corpus, stemDocument) # Stemming uses an algorithm that
removes common word endings for English
#Making a document-term matrix
dtm <- DocumentTermMatrix(corpus)
################################
# The transpose of the document term matrix
################################
tdm <- TermDocumentMatrix(corpus)
# Frequency
freq <- sort(colSums(as.matrix(dtm)), decreasing=TRUE)
wf <- data.frame(word=names(freq), freq=freq)
# Plot Histogram
subset(wf, freq>500) %>%
ggplot(aes(word, freq)) +
geom_bar(stat="identity", fill="darkred", colour="darkgreen") +
theme(axis.text.x=element_text(angle=45, hjust=1))
# Create Wordcloud
library(wordcloud)
set.seed(100)
wordcloud(names(freq), freq, min.freq=100, colors=brewer.pal(6, "Dark2"))
###############################
# N-gram tokenization of the Corpus
###############################
OnegramTokenizer <- function(x) NGramTokenizer(x,
Weka_control(min = 1, max =1))
dtm <- DocumentTermMatrix(docs, control = list(tokenize = OnegramTokenizer))
freq <- sort(colSums(as.matrix(dtm)), decreasing=TRUE)
wof <- data.frame(word=names(freq), freq=freq)
pl <- ggplot(subset(wof, freq > 500), aes(word, freq))
pl <- pl + geom_bar(stat="identity", fill="darkred", colour="blue")
pl + theme(axis.text.x=element_text(angle=45, hjust=1)) + ggtitle("Uni-Gram
Frequency")
############################
# Bi-Gram Tokenization of the Corpus
############################
BigramTokenizer <- function(x) NGramTokenizer(x,
Weka_control(min = 2, max = 2))
dtm <- DocumentTermMatrix(docs, control = list(tokenize = BigramTokenizer))
freq <- sort(colSums(as.matrix(dtm)), decreasing=TRUE)
wof <- data.frame(word=names(freq), freq=freq)
pl <- ggplot(subset(wof, freq >500) ,aes(word, freq))
pl <- pl + geom_bar(stat="identity", fill="darkgreen", colour="blue")
pl + theme(axis.text.x=element_text(angle=45, hjust=1)) + ggtitle("Bi-Gram
Frequency")