2012-11-30 7 views
47

Ho provato a utilizzare tm_map. Ha dato il seguente errore. Come posso aggirare questo?Errore durante la conversione del testo in minuscolo con 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

Quale pacchetto è 'tm_map' da? Questo sembra dipendere da un pacchetto non di base. Per favore considera di includere la dichiarazione 'library' per completezza. –

+1

@DanielKrizian: 'tm_map()' è dal pacchetto 'tm', e' tolower() 'è da' base' – smci

risposta

101

Utilizzare la funzione R di base tolower():

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

Grazie. Qualche idea sul perché ho ricevuto quell'errore? Potrei aver bisogno di usare altre applicazioni tm_map! – jackStinger

+0

Il file di aiuto per 'tm_map' (nel pacchetto' tm') mostra un elenco di funzioni di trasformazione utilizzabili e 'tolower' non è uno di questi. Le trasformazioni sembrano essere i metodi S3 che operano sugli oggetti della classe 'Corpus'. Quindi non puoi usare solo una funzione con 'tm_map'. – bdemarest

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

print(myCorpus[[1]]) 
+9

Dovresti racchiudere 'tolower' all'interno di' content_transformer' per non rovinare l'oggetto 'VCorpus', come:' tm_map (myCorpus, content_transformer (tolower)) ' – daroczig

+0

@daroczig: Per favore, fai una risposta! – smci

+0

@smci grazie per l'idea, ho appena inviato il commento sopra come una nuova risposta qui sotto :) – daroczig

1

utilizzando tolower in questo modo ha un effetto collaterale indesiderato: se si tenta di creare una matrice termine documento dal corpus tardi, fallirà. Questo a causa di una recente modifica in tm che non può gestire il tipo restituito di tolower. Invece, utilizzare:

myCorpus <- tm_map(myCorpus, PlainTextDocument) 
6

Ampliare la mia comment ad una risposta più dettagliata qui: bisogna avvolgere tolower all'interno di content_transformer per non rovinare l'oggetto VCorpus - qualcosa come:

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