2012-11-30 72 views
47

我嘗試使用tm_map。它給出了以下錯誤。我怎樣才能解決這個問題?使用tm_map(...,tolower)將文本轉換爲小寫時出錯

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" 
+2

什麼包'tm_map'來自?這似乎取決於一些非基礎包。請考慮包括'庫'聲明的完整性。 –

+1

@DanielKrizian:'tm_map()'來自'tm'包,'tolower()'來自'base' – smci

回答

101

使用基礎R功能tolower()

tolower(c("THE quick BROWN fox")) 
# [1] "the quick brown fox" 
+0

謝謝。但是,爲什麼我得到這個錯誤的任何見解?我可能需要使用其他tm_map應用程序! – jackStinger

+0

'tm_map'(包'tm')的幫助文件顯示了可用的轉換函數列表,而'tolower'不是其中的一個。這些轉換似乎是對類「語料庫」的對象進行操作的S3方法。所以你不能只用'tm_map'來使用任何函數。 – bdemarest

3
myCorpus <- Corpus(VectorSource(byword)) 
myCorpus <- tm_map(myCorpus , tolower) 

print(myCorpus[[1]]) 
+9

你應該在'content_transformer'裏面包裝'tolower',而不是搞砸'VCorpus'對象,如:'tm_map(myCorpus,content_transformer(tolower))' – daroczig

+0

@daroczig:請做出答案! – smci

+0

@smci感謝您的想法,我剛剛提交了上述評論作爲新的答案:) – daroczig

1

使用tolower的這種方式有一個不良的副作用:如果您嘗試了陰莖的後創造的一個術語文檔矩陣,它會失敗。這是因爲tm最近發生的變化,無法處理tolower的返回類型。相反,使用:

myCorpus <- tm_map(myCorpus, PlainTextDocument) 
6

這裏擴大我comment一個更詳細的解答:你有包裹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"