Стоп-слова, остающиеся в корпусе после очистки - PullRequest
1 голос
/ 19 февраля 2020

Я пытаюсь удалить стоп-слово «the» из моего корпуса, однако не все экземпляры удаляются.

library(RCurl)
library(tm)

url <- "https://raw.githubusercontent.com/angerhang/statsTutorial/master/src/textMining/data/1.txt"
file1 <- getURL(url)
url <- "https://raw.githubusercontent.com/angerhang/statsTutorial/master/src/textMining/data/2.txt"
file2 <- getURL(url)
url <- "https://raw.githubusercontent.com/angerhang/statsTutorial/master/src/textMining/data/3.txt"
file3 <- getURL(url)


shakespeare <- VCorpus(VectorSource(c(file1,file2,file3)))

list<-inspect(
  DocumentTermMatrix(shakespeare,list(dictionary = c("the","thee")))
)
shakespeare <- tm_map(shakespeare, stripWhitespace)
shakespeare <- tm_map(shakespeare, stemDocument)
shakespeare <- tm_map(shakespeare, removePunctuation)
tm_map(shakespeare, content_transformer(tolower))
#taken directly from tm documentation
shakespeare <- tm_map(shakespeare, removeWords, c(stopwords("english"),"the"))
list<-inspect(
  DocumentTermMatrix(shakespeare,list(dictionary = c("the","thee")))
)

Первый вызов проверки показывает:

    Terms
Docs   the thee
   1 11665  752
   2 11198  660
   3  4866  382

И второй, после очистки:

    Terms
Docs  the thee
   1 1916 1298
   2 1711 1140
   3  760  740

Чего мне здесь не хватает в логи c из removeWords, что он будет игнорировать все эти экземпляры "the"?

EDIT

Я смог получить экземпляры "the" ниже 1000 путем небольшого изменения вызова и выполнения удаление слов вызывает самый первый шаг очистки:

shakespeare <- tm_map(shakespeare, removeWords, c(stopwords("english"),"the","The"))

Что приводит меня к:

Docs the thee
   1 145  752
   2 130  660
   3  71  382

Тем не менее, я хотел бы знать, почему я не могу устранить их всех.

1 Ответ

1 голос
/ 24 февраля 2020

Настоящим воспроизводимый код, который приводит к 0 экземплярам "the". Я решил вашу опечатку и использовал ваш код до редактирования.

library(RCurl)
library(tm)
library(SnowballC)

url <- "https://raw.githubusercontent.com/angerhang/statsTutorial/master/src/textMining/data/1.txt"
file1 <- getURL(url)
url <- "https://raw.githubusercontent.com/angerhang/statsTutorial/master/src/textMining/data/2.txt"
file2 <- getURL(url)
url <- "https://raw.githubusercontent.com/angerhang/statsTutorial/master/src/textMining/data/3.txt"
file3 <- getURL(url)


shakespeare <- VCorpus(VectorSource(c(file1,file2,file3)))

list<-inspect(
  DocumentTermMatrix(shakespeare,list(dictionary = c("the","thee")))
)

приводит к:

<<DocumentTermMatrix (documents: 3, terms: 2)>>
Non-/sparse entries: 6/0
Sparsity           : 0%
Maximal term length: 4
Weighting          : term frequency (tf)
Sample             :
    Terms
Docs   the thee
   1 11665  752
   2 11198  660
   3  4866  382

и после очистки и решения опечатки:

shakespeare <- tm_map(shakespeare, stripWhitespace)
shakespeare <- tm_map(shakespeare, stemDocument)
shakespeare <- tm_map(shakespeare, removePunctuation)
shakespeare = tm_map(shakespeare, content_transformer(tolower)) ## FIXED TYPO
#taken directly from tm documentation
shakespeare <- tm_map(shakespeare, removeWords, c(stopwords("english"),"the"))
list<-inspect(
  DocumentTermMatrix(shakespeare,list(dictionary = c("the","thee")))
)

это приводит к:

<<DocumentTermMatrix (documents: 3, terms: 2)>>
Non-/sparse entries: 3/3
Sparsity           : 50%
Maximal term length: 4
Weighting          : term frequency (tf)
Sample             :
    Terms
Docs the thee
   1   0 1298
   2   0 1140
   3   0  740
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...