Ошибка преобразования текста в нижний регистр с помощью tm_map(, tolower)

Я попытался использовать файл tm_map. Это дало следующую ошибку. Как я могу обойти это?

 require(tm)
 byword<-tm_map(byword, tolower)

Error in UseMethod("tm_map", x) : 
  no applicable method for 'tm_map' applied to an object of class "character"

person jackStinger    schedule 30.11.2012    source источник
comment
Из какого пакета tm_map? Кажется, это зависит от какого-то небазового пакета. Пожалуйста, рассмотрите возможность включения заявления library для полноты.   -  person Daniel Krizian    schedule 02.10.2014
comment
@DanielKrizian: tm_map() из пакета tm, а tolower() из пакета base   -  person smci    schedule 20.07.2016


Ответы (4)


Используйте базовую функцию R tolower():

tolower(c("THE quick BROWN fox"))
# [1] "the quick brown fox"
person bdemarest    schedule 30.11.2012
comment
Спасибо. Но есть ли понимание, почему я получил эту ошибку? Возможно, мне придется использовать другие приложения tm_map! - person jackStinger; 30.11.2012
comment
Файл справки для tm_map (в пакете tm) показывает список используемых функций преобразования, а tolower не входит в их число. Преобразования представляют собой методы S3, которые работают с объектами класса «Корпус». Таким образом, вы не можете использовать любую функцию с tm_map. - person bdemarest; 30.11.2012

Расширение моего комментария к более подробному ответу здесь: вам нужно обернуть tolower внутри content_transformer, чтобы не испортить объект VCorpus - что-то вроде:

> library(tm)
> data('crude')
> crude[[1]]$content
[1] "Diamond Shamrock Corp said that\neffective today it had cut its contract prices for crude oil by\n1.50 dlrs a barrel.\n    The reduction brings its posted price for West Texas\nIntermediate to 16.00 dlrs a barrel, the copany said.\n    \"The price reduction today was made in the light of falling\noil product prices and a weak crude oil market,\" a company\nspokeswoman said.\n    Diamond is the latest in a line of U.S. oil companies that\nhave cut its contract, or posted, prices over the last two days\nciting weak oil markets.\n Reuter"
> tm_map(crude, content_transformer(tolower))[[1]]$content
[1] "diamond shamrock corp said that\neffective today it had cut its contract prices for crude oil by\n1.50 dlrs a barrel.\n    the reduction brings its posted price for west texas\nintermediate to 16.00 dlrs a barrel, the copany said.\n    \"the price reduction today was made in the light of falling\noil product prices and a weak crude oil market,\" a company\nspokeswoman said.\n    diamond is the latest in a line of u.s. oil companies that\nhave cut its contract, or posted, prices over the last two days\nciting weak oil markets.\n reuter"
person daroczig    schedule 20.07.2016

Использование tolower таким образом имеет нежелательный побочный эффект: если вы позже попытаетесь создать матрицу документа термина из корпуса, это не удастся. Это связано с недавним изменением в tm, которое не может обрабатывать возвращаемый тип tolower. Вместо этого используйте:

myCorpus <- tm_map(myCorpus, PlainTextDocument)
person Raja    schedule 25.06.2015

person    schedule
comment
Вы должны обернуть tolower внутри content_transformer, чтобы не испортить объект VCorpus, например: tm_map(myCorpus, content_transformer(tolower)) - person daroczig; 08.09.2014
comment
@daroczig: Пожалуйста, сделайте это ответом! - person smci; 20.07.2016
comment
@smci спасибо за идею, я только что отправил комментарий выше в качестве нового ответа ниже :) - person daroczig; 20.07.2016