2012-11-30 14 views
47

Ich habe versucht, die tm_map zu verwenden. Es gab den folgenden Fehler. Wie kann ich das umgehen?Fehler beim Umwandeln von Text in Kleinbuchstaben mit 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

Was Paket 'tm_map' von? Dies scheint von einigen Nicht-Basis-Paketen abhängig zu sein. Bitte beachten Sie, dass die "library" -Anweisung der Vollständigkeit halber enthalten ist. –

+1

@DanielKrizian: 'tm_map()' ist von 'tm' Paket, und' tolower() 'ist von' Basis' – smci

Antwort

101

Verwenden Sie die Basis R-Funktion tolower():

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

Danke. Aber jede Einsicht, warum ich diesen Fehler bekommen habe? Ich muss möglicherweise andere tm_map-Anwendungen verwenden! – jackStinger

+0

Die Hilfedatei für 'tm_map' (im Paket' tm') zeigt eine Liste verwendbarer Transformationsfunktionen und 'tolower' gehört nicht dazu. Die Transformationen scheinen S3-Methoden zu sein, die auf Objekte der Klasse 'Corpus' wirken. Sie können also keine Funktion mit 'tm_map' verwenden. – bdemarest

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

print(myCorpus[[1]]) 
+9

Sie sollten "tolower" innerhalb von "content_transformer" einpacken, um das 'VCorpus'-Objekt nicht zu vermasseln, wie:' tm_map (myCorpus, content_transformer (tolower)) ' – daroczig

+0

@daroczig: Bitte antworten Sie darauf! – smci

+0

@smci danke für die Idee, ich habe gerade den obigen Kommentar als neue Antwort unten eingereicht :) – daroczig

1

tolower mit auf diese Weise einen unerwünschten Nebeneffekt hat: wenn Sie versuchen, ein Begriff Dokument-Matrix aus dem Korpus später zu erstellen, es wird scheitern. Dies liegt an einer kürzlichen Änderung in tm, die den Rückgabetyp von tolower nicht handhaben kann. Verwenden Sie stattdessen:

myCorpus <- tm_map(myCorpus, PlainTextDocument) 
6

Erweiterung meiner comment auf eine ausführlichere Antwort hier: Sie tolower innerhalb von content_transformer nicht vermasseln die VCorpus Objekt zu wickeln haben - so etwas wie:

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