2012-11-30 20 views
47

Próbowałem używać tm_map. Dało to następujący błąd. Jak mogę to obejść?Błąd konwersji tekstu na małe litery z 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

Co pakunek 'tm_map' z? Wydaje się, że zależy to od jakiegoś pakietu niebędącego pakietem podstawowym. Proszę rozważyć dołączenie instrukcji 'library' dla kompletności. –

+1

@DanielKrizian: 'tm_map()' pochodzi z pakietu 'tm', a' tolower() 'pochodzi z' bazy' – smci

Odpowiedz

101

Użyj R funkcja bazowa tolower():

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

Dzięki. Ale jakikolwiek wgląd w to, dlaczego dostałem ten błąd? Być może będę musiał użyć innych aplikacji tm_map! – jackStinger

+0

Plik pomocy dla 'tm_map' (w pakiecie' tm') pokazuje listę użytecznych funkcji transformacji, a 'tolower' nie jest jedną z nich. Transformacje wydają się być metodami S3, które działają na obiektach klasy "Corpus". Więc nie możesz użyć żadnej funkcji z 'tm_map'. – bdemarest

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

print(myCorpus[[1]]) 
+9

Powinieneś owinąć 'tolower' wewnątrz' content_transformer' by nie zepsuć obiektu 'VCorpus', np .:' tm_map (myCorpus, content_transformer (tolower)) ' – daroczig

+0

@daroczig: Proszę, zrób odpowiedź! – smci

+0

@ Dziękuję za pomysł, właśnie przesłałem powyższy komentarz jako nową odpowiedź poniżej :) – daroczig

1

użyciu tolower w ten sposób ma niepożądany efekt uboczny: jeśli starają się stworzyć macierz dokumentu termin z korpusu później to się nie powiedzie. Wynika to z niedawnej zmiany tm, która nie może obsłużyć typu zwrotu tolower. Zamiast tego należy użyć:

myCorpus <- tm_map(myCorpus, PlainTextDocument) 
6

Poszerzanie comment do bardziej szczegółowej odpowiedzi tutaj: trzeba owinąć tolower wewnątrz content_transformer aby nie zepsuć obiekt VCorpus - coś takiego:

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