преобразование сбрасывает ошибки в документах R - PullRequest
0 голосов
/ 28 июня 2018

Всякий раз, когда я запускаю этот код, строка tm_map выдает мне предупреждающее сообщение как Предупреждающее сообщение: В tm_map.SimpleCorpus (docs, toSpace, "/"): преобразование отбрасывает документы

texts <- read.csv("./Data/fast food/Domino's/Domino's veg pizza.csv",stringsAsFactors = FALSE)
        docs <- Corpus(VectorSource(texts))
        toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ", x))
        docs <- tm_map(docs, toSpace, "/")
        docs <- tm_map(docs, toSpace, "@")
        docs <- tm_map(docs, toSpace, "\\|")
        docs <- tm_map(docs, content_transformer(tolower))
        docs <- tm_map(docs, removeNumbers)
        docs <- tm_map(docs, removeWords, stopwords("english"))
        docs <- tm_map(docs, removeWords, c("blabla1", "blabla2")) 
        docs <- tm_map(docs, removePunctuation)
        docs <- tm_map(docs, stripWhitespace)

1 Ответ

0 голосов
/ 04 июля 2018

Это предупреждение появляется только тогда, когда вы используете content_transformer для создания собственной определенной функции. И это появляется только тогда, когда у вас есть корпус на основе VectorSource.

Причина в том, что в базовом коде есть проверка, чтобы увидеть, соответствует ли количество имен содержимого корпуса длине содержимого корпуса. При чтении текста в виде вектора имена документов отсутствуют, и появляется это предупреждение. И это только предупреждение, никаких документов не было сброшено.

См. Следующие примеры:

text <- c("this is my text with a forward slash / and some other text")
library(tm)
toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ", x))

text <- c("this is my text with a forward slash / and some other text")
text_corpus <- Corpus(VectorSource(text))
inspect(text_corpus)
<<SimpleCorpus>>
Metadata:  corpus specific: 1, document level (indexed): 0
Content:  documents: 1

[1] this is my text with a forward slash / and some other text

# warning appears here
text_corpus <- tm_map(text_corpus, toSpace, "/")
inspect(text_corpus)
<<SimpleCorpus>>
Metadata:  corpus specific: 1, document level (indexed): 0
Content:  documents: 1

[1] this is my text with a forward slash   and some other text

Вы можете видеть, что в text_corpus нет имен с помощью следующей команды:

names(content(text_corpus))
NULL

Если вы не хотите, чтобы это предупреждение появлялось, вам нужно создать data.frame и использовать его в качестве источника с DataframeSource.

text <- c("this is my text with a forward slash / and some other text")
doc_ids <- c(1)

df <- data.frame(doc_id = doc_ids, text = text, stringsAsFactors = FALSE)
df_corpus <- Corpus(DataframeSource(df))
inspect(df_corpus)
# no warning appears
df_corpus <- tm_map(df_corpus, toSpace, "/")
inspect(df_corpus)

names(content(df_corpus))
"1"
...