2014-07-16 13 views
27

Sto cercando di eseguire questo codice (Ubuntu 12.04, R 3.1.1)R-Project metodo non applicabile per la 'meta' applicata a un oggetto della classe "carattere"

# Load requisite packages 
library(tm) 
library(ggplot2) 
library(lsa) 

# Place Enron email snippets into a single vector. 
text <- c(
    "To Mr. Ken Lay, I’m writing to urge you to donate the millions of dollars you made from selling Enron stock before the company declared bankruptcy.", 
    "while you netted well over a $100 million, many of Enron's employees were financially devastated when the company declared bankruptcy and their retirement plans were wiped out", 
    "you sold $101 million worth of Enron stock while aggressively urging the company’s employees to keep buying it", 
    "This is a reminder of Enron’s Email retention policy. The Email retention policy provides as follows . . .", 
    "Furthermore, it is against policy to store Email outside of your Outlook Mailbox and/or your Public Folders. Please do not copy Email onto floppy disks, zip disks, CDs or the network.", 
    "Based on our receipt of various subpoenas, we will be preserving your past and future email. Please be prudent in the circulation of email relating to your work and activities.", 
    "We have recognized over $550 million of fair value gains on stocks via our swaps with Raptor.", 
    "The Raptor accounting treatment looks questionable. a. Enron booked a $500 million gain from equity derivatives from a related party.", 
    "In the third quarter we have a $250 million problem with Raptor 3 if we don’t “enhance” the capital structure of Raptor 3 to commit more ENE shares.") 
view <- factor(rep(c("view 1", "view 2", "view 3"), each = 3)) 
df <- data.frame(text, view, stringsAsFactors = FALSE) 

# Prepare mini-Enron corpus 
corpus <- Corpus(VectorSource(df$text)) 
corpus <- tm_map(corpus, tolower) 
corpus <- tm_map(corpus, removePunctuation) 
corpus <- tm_map(corpus, function(x) removeWords(x, stopwords("english"))) 
corpus <- tm_map(corpus, stemDocument, language = "english") 
corpus # check corpus 

# Mini-Enron corpus with 9 text documents 

# Compute a term-document matrix that contains occurrance of terms in each email 
# Compute distance between pairs of documents and scale the multidimentional semantic space (MDS) onto two dimensions 
td.mat <- as.matrix(TermDocumentMatrix(corpus)) 
dist.mat <- dist(t(as.matrix(td.mat))) 
dist.mat # check distance matrix 

# Compute distance between pairs of documents and scale the multidimentional semantic space onto two dimensions 
fit <- cmdscale(dist.mat, eig = TRUE, k = 2) 
points <- data.frame(x = fit$points[, 1], y = fit$points[, 2]) 
ggplot(points, aes(x = x, y = y)) + geom_point(data = points, aes(x = x, y = y, color = df$view)) + geom_text(data = points, aes(x = x, y = y - 0.2, label = row.names(df))) 

Tuttavia, quando corro io ottenere questo errore (in linea td.mat <- as.matrix(TermDocumentMatrix(corpus))):

Error in UseMethod("meta", x) : 
    no applicable method for 'meta' applied to an object of class "character" 
In addition: Warning message: 
In mclapply(unname(content(x)), termFreq, control) : 
    all scheduled cores encountered errors in user code 

non sono sicuro di cosa guardare - tutti i moduli caricati.

+0

Non ho potuto riprodurre. È possibile che tu non abbia le versioni più recenti dei pacchetti (in particolare 'tm')? –

+0

@DavidRobinson Quale versione di 'tm' hai testato? 0.6 è l'ultimo per quanto ne so. – MrFlick

+0

@MrFlick: Il mio errore: l'ho installato ieri sera con 'install.packages' e ho ricevuto' tm_0.5-10', ma ora mi rendo conto che è perché sto usando R '3.0.1' (tempo di aggiornamento) e l'ultimo 'tm' richiede'> = 3.1.0'. –

risposta

84

L'ultima versione di tm (0,60) lo ha reso così che non è possibile utilizzare le funzioni con tm_map che operano su valori di carattere semplice. Quindi il problema è il tuo passaggio tolower dal momento che non è una trasformazione "canonica" (Vedi getTransformations()). Basta sostituirlo con

corpus <- tm_map(corpus, content_transformer(tolower)) 

La content_transformer funzione wrapper convertirà tutto per il tipo di dati corretto all'interno del corpus. È possibile utilizzare content_transformer con qualsiasi funzione destinata a manipolare i vettori di caratteri in modo che funzioni in una pipeline tm_map.

+0

Grazie, ma come lo fai nelle versioni più recenti? corpus <- tm_map (corpus, stemDocument, language = "english") @MrFlick –

+0

@VladimirStazhilov Quella linea dovrebbe funzionare senza modifiche. Se questo non è il tuo caso, dovresti considerare l'apertura di una nuova domanda con un errore riproducibile. – MrFlick

+0

queste soluzioni non sembrano funzionare in 7.1:/ – cdaringe

29

Questo è un po 'vecchio, ma solo per scopi di ricerche successive su google: c'è una soluzione alternativa. Dopo corpus <- tm_map(corpus, tolower) è possibile utilizzare corpus <- tm_map(corpus, PlainTextDocument) che lo riporta nuovamente al tipo di dati corretto.

+3

questa soluzione giusta! Grazie. –

+0

Sei una leggenda, signore !!!. Ho appena salvato un giorno di lavoro semplicemente non ignorando ancora una volta i commenti in Stackoverflow :) –

1

Ho avuto lo stesso problema, e, infine, è venuto a una soluzione:

Sembra che il meta informazioni all'interno dell'oggetto corpus viene danneggiato dopo l'applicazione di trasformazioni su di esso.

Quello che ho fatto è solo la creazione di nuovo il corpus alla fine del processo, dopo che era completamente pronto. Dovendo superare altri problemi, ho scritto anche un ciclo per copiare il testo nel mio dataframe:

a<- list() 
for (i in seq_along(corpus)) { 
    a[i] <- gettext(corpus[[i]][[1]]) #Do not use $content here! 
} 

df$text <- unlist(a) 
corpus <- Corpus(VectorSource(df$text)) #This action restores the corpus. 
Problemi correlati