R не удаляет пользовательские стоп-слова в корпусе - PullRequest
0 голосов
/ 08 апреля 2020

Я пытаюсь удалить пользовательский список стоп-слов вместе с общими стоп-словами Engli sh. Ниже мой код. К сожалению, общие стоп-слова удаляются, но указанные мной пользовательские стоп-слова - нет. Я попробовал синтаксис этого рекомендуемого решения: tm_map(abs, removeWords, c(stopwords("english"),"my","custom","words")) и я попытался импортировать csv-файл пользовательских стоп-слов и чтения оттуда, но ничего не работает - по крайней мере, последовательно.
Я заметил, что когда я запускаю код несколько раз, иногда он удаляет пользовательские слова, а иногда нет, что заставляет меня звучать безумно, но я наблюдал это явление несколько раз. Есть идеи, пожалуйста, как это исправить?

#Load libraries
Sys.setenv(JAVA_HOME="C:/Program Files/Java/jdk-14")
library(rJava)
library(qdap)
library(dplyr)
library(tm)
library(wordcloud)
library(dendextend)
library(RWeka)
library(quanteda)
library(SnowballC)
library(readxl)

#Get complaint files
file.list <- list.files(pattern='*.xlsx')
complaints <- lapply(file.list, read_xlsx, skip=4)
complaints <- bind_rows(complaints)
names(complaints)[16]<-"Complaint.Description"
names(complaints)[21]<-"Selling.Description"

#Make a vector source and a corpus
corpus_selling_complaints=Corpus(VectorSource(complaints$Selling.Description))

#Make text lowercase
corpus_selling_complaints=tm_map(corpus_selling_complaints, tolower)

#Remove punctuation
corpus_selling_complaints=tm_map(corpus_selling_complaints, removePunctuation)

#Remove common stopwords
corpus_selling_complaints=tm_map(corpus_selling_complaints, removeWords, stopwords("english"))


# Remove context specific stop words
corpus_selling_complaints<-tm_map(corpus_selling_complaints, 
removeWords, c("need","give","got","just","go","gave","name","info"))

#Stem
corpus_selling_complaints=tm_map(corpus_selling_complaints, stemDocument)

# Find the 20 most frequent terms: term_count
term_count <- freq_terms(corpus_selling_complaints, 20)

selling_dtm <- DocumentTermMatrix(corpus_selling_complaints)
selling_tdm <- TermDocumentMatrix(corpus_selling_complaints)

# Convert TDM to matrix
selling_m <- as.matrix(selling_tdm)
# Sum rows and frequency data frame
selling_term_freq <- rowSums(selling_m)
# Sort term_frequency in descending order
selling_term_freq <- sort(selling_term_freq, decreasing = T)
# View the top 10 most common words
selling_term_freq[1:100]

selling_word_freq <- data.frame(term = names(selling_term_freq),
                               num = selling_term_freq)

# Create a wordcloud for the values in word_freqs
wordcloud(selling_word_freq$term, selling_word_freq$num,
          max.words = 50, colors = c("red","aquamarine","darkgoldenrod","tomato"))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...