2012-08-09 13 views
11

Estoy tratando de hacer algo de raíz en R, pero parece que solo funciona en documentos individuales. Mi objetivo final es una matriz de documento de términos que muestra la frecuencia de cada término en el documento.R derivando una cadena/documento/corpus

He aquí un ejemplo:

require(RWeka) 
require(tm) 
require(Snowball) 

worder1<- c("I am taking","these are the samples", 
"He speaks differently","This is distilled","It was placed") 
df1 <- data.frame(id=1:5, words=worder1) 

> df1 
    id     words 
1 1   I am taking 
2 2 these are the samples 
3 3 He speaks differently 
4 4  This is distilled 
5 5   It was placed 

Este método funciona para la parte derivada pero no la parte de la matriz término documento:

> corp1 <- Corpus(VectorSource(df1$words)) 
> inspect(corp1) 
A corpus with 5 text documents 

The metadata consists of 2 tag-value pairs and a data frame 
Available tags are: 
    create_date creator 
Available variables in the data frame are: 
    MetaID 

[[1]] 
I am taking 

[[2]] 
these are the samples 

[[3]] 
He speaks differently 

[[4]] 
This is distilled 

[[5]] 
It was placed 

> corp1 <- tm_map(corp1, SnowballStemmer) 
> inspect(corp1) 
A corpus with 5 text documents 

The metadata consists of 2 tag-value pairs and a data frame 
Available tags are: 
    create_date creator 
Available variables in the data frame are: 
    MetaID 

[[1]] 
[1] I am tak 

[[2]] 
[1] these are the sampl 

[[3]] 
[1] He speaks differ 

[[4]] 
[1] This is distil 

[[5]] 
[1] It was plac 

> class(corp1) 
[1] "VCorpus" "Corpus" "list" 
> tdm1 <- TermDocumentMatrix(corp1) 
Error in UseMethod("Content", x) : 
    no applicable method for 'Content' applied to an object of class "character" 

Así que en vez intenté crear la matriz término documento primera pero esta vez las palabras no se derivan:

> corp1 <- Corpus(VectorSource(df1$words)) 
> tdm1 <- TermDocumentMatrix(corp1, control=list(stemDocument=TRUE)) 
> as.matrix(tdm1) 
      Docs 
Terms   1 2 3 4 5 
    are   0 1 0 0 0 
    differently 0 0 1 0 0 
    distilled 0 0 0 1 0 
    placed  0 0 0 0 1 
    samples  0 1 0 0 0 
    speaks  0 0 1 0 0 
    taking  1 0 0 0 0 
    the   0 1 0 0 0 
    these  0 1 0 0 0 
    this  0 0 0 1 0 
    was   0 0 0 0 1 

Aquí las palabras son obviamente no derivado.

¿Alguna sugerencia?

+0

El tallo solo ha funcionado en la última palabra de sus documentos, ¿no? Porque '" habla "' no se ha detenido, mientras que creo que debería. Mi opinión es que la función de derivación en R tiene muchos problemas. Mis colegas y yo nunca hemos podido hacerlo funcionar. Ejecutamos un script de Python en su lugar ... – Pop

+0

@AllenR .: Tienes razón. No me di cuenta de eso. Voy a echarle un vistazo a Python. Gracias. – screechOwl

+1

No sé si has oído hablar del paquete ** nltk ** en Python que hace este tipo de cosas. – Pop

Respuesta

9

El paquete RTextTools en CRAN le permite hacer esto.

library(RTextTools) 
worder1<- c("I am taking","these are the samples", 
"He speaks differently","This is distilled","It was placed") 
df1 <- data.frame(id=1:5, words=worder1) 

matrix <- create_matrix(df1, stemWords=TRUE, removeStopwords=FALSE, minWordLength=2) 
colnames(matrix) # SEE THE STEMMED TERMS 

Esto devuelve una DocumentTermMatrix que se puede utilizar con el paquete tm. Puede jugar con los otros parámetros (por ejemplo, eliminando las palabras vacías, cambiando la longitud mínima de la palabra, usando una lectora para un idioma diferente) para obtener los resultados que necesita. Cuando aparece as.matrix el ejemplo produce el término matriz siguiente:

      Terms 
Docs      am are differ distil he is it place sampl speak take the these this was 
    1 I am taking   1 0  0  0 0 0 0  0  0  0 1 0  0 0 0 
    2 these are the samples 0 1  0  0 0 0 0  0  1  0 0 1  1 0 0 
    3 He speaks differently 0 0  1  0 1 0 0  0  0  1 0 0  0 0 0 
    4 This is distilled  0 0  0  1 0 1 0  0  0  0 0 0  0 1 0 
    5 It was placed   0 0  0  0 0 0 1  1  0  0 0 0  0 0 1 
+1

Muchas gracias. – screechOwl

+0

Muchas gracias. Sin embargo, RTextTools no incluye el idioma turco para la derivación. Por lo tanto, agregué otra solución. –

1

Sí para steming palabras del documento en un Corpus que requiere Rweka, Snowball y tm paquete.

uso tras la instrucción

> library (tm) 
#set your directory Suppose u have set "F:/St" then next command is 
> a<-Corpus(DirSource("/st"), 
      readerControl=list(language="english")) # "/st" it is path of your directory 
> a<-tm_map(a, stemDocument, language="english") 
> inspect(a) 

seguro de que encontrará el resultado deseado.

3

Esto funciona en R como se esperaba con tm versión 0.6. Tuviste algunos errores menores que impidieron que la derivación funcionara correctamente, tal vez son de una versión anterior de tm.De todos modos, aquí es cómo hacer que funcione:

require(RWeka) 
require(tm) 

El paquete derivada no es su Snowball pero SnowballC:

require(SnowballC) 

worder1<- c("I am taking","these are the samples", 
      "He speaks differently","This is distilled","It was placed") 
df1 <- data.frame(id=1:5, words=worder1) 
corp1 <- Corpus(VectorSource(df1$words)) 
inspect(corp1) 

Cambiar la SnowballStemmer a stemDocument en la línea siguiente modo:

corp1 <- tm_map(corp1, stemDocument) 
inspect(corp1) 

Las palabras se derivan, como se esperaba:

<<VCorpus (documents: 5, metadata (corpus/indexed): 0/0)>> 

[[1]] 
<<PlainTextDocument (metadata: 7)>> 
I am take 

[[2]] 
<<PlainTextDocument (metadata: 7)>> 
these are the sampl 

[[3]] 
<<PlainTextDocument (metadata: 7)>> 
He speak differ 

[[4]] 
<<PlainTextDocument (metadata: 7)>> 
This is distil 

[[5]] 
<<PlainTextDocument (metadata: 7)>> 
It was place 

Ahora hacer la matriz término documento:

corp1 <- Corpus(VectorSource(df1$words)) 

Cambiar la stemDocument-stemming:

tdm1 <- TermDocumentMatrix(corp1, control=list(stemming=TRUE)) 
as.matrix(tdm1) 

Y obtenemos un TDM de deriva palabras, como se esperaba:

 Docs 
Terms 1 2 3 4 5 
    are 0 1 0 0 0 
    differ 0 0 1 0 0 
    distil 0 0 0 1 0 
    place 0 0 0 0 1 
    sampl 0 1 0 0 0 
    speak 0 0 1 0 0 
    take 1 0 0 0 0 
    the 0 1 0 0 0 
    these 0 1 0 0 0 
    this 0 0 0 1 0 
    was 0 0 0 0 1 

Así que ahí lo tienes. Quizás una lectura más cuidadosa de los documentos tm podría haber ahorrado un poco de su tiempo con esto;)

0

Otra solución es la codificación difícil. Simplemente divide los textos y los tallos y luego los reconcentra:

library(SnowballC) 
i=1 
#Snowball stemming 
while(i<=nrow(veri)){ 
    metin=veri[i,2] 
    stemmed_metin=""; 
    parcali=unlist(strsplit(metin,split=" ")) #split the text 
    for(klm in parcali){ 
    stemmed_klm=wordStem(klm,language = "turkish") #stem word by word 
    stemmed_metin=sprintf("%s %s",stemmed_metin,stemmed_klm) #reconcantrate 
    } 

    veri[i,4]=stemmed_metin #write to new column 

    i=i+1 
}