2017-02-02 3 views
0

Ich habe einen Datenrahmen mit mehr als 100 Spalten und 1 Million Zeilen. Eine Spalte enthält die Textdaten. Die Textdatenspalte enthält große Sätze. Ich habe einen Code geschrieben, um die Daten zu säubern, aber es wird nicht geputzt. Ich möchte alle Stoppwörter entfernen, "das", "Sie", "wie", "für" usw.Reinigen von Textdaten mit R

scorel= function(sentences, pos.words, .progress='none') 
{ 
    require(plyr) 
require(stringr) 


scores = laply(sentences, function(sentence, pos.words) 
{ 

# clean up sentences with R's regex-driven global substitute, gsub(): 
sentence = gsub('[[:punct:]]', '', sentence) 
sentence = gsub('[[:cntrl:]]', '', sentence) 
sentence = gsub('\\d+', '', sentence) 
sentence = gsub("@\\w+ *", "", sentence) 
# and convert to lower case: 
sentence = tolower(sentence) 

# split into words. str_split is in the stringr package 
word.list = str_split(sentence, '\\s+') 
words = unlist(word.list) 
# compare our words to the dictionaries of positive & negative terms 
pos.matches = match(words, pos.words) 
# match() returns the position of the matched term or NA 
# we just want a TRUE/FALSE: 
#  pos.matches = !is.na(pos.matches) 

pos.matches=!is.na(pos.matches) 

# and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum(): 
#score = sum(pos.matches) 
score = sum(pos.matches) 
return(score) 
}, #pos.words, neg.words, .progress=.progress) 
    pos.words, .progress=.progress) 

scores.df = data.frame(score=scores, text=sentences) 
return(scores.df) 
} 
Data <- read.csv("location", stringsAsFactors=FALSE) 
Data<-Data[!duplicated(Data), ] 
Text <- data.frame(as.factor(Data$speech)) 
names(Text)<-"Conversation" 
textf<-Text$Conversation 
textf<- unique(textf) 
Text <- as.factor(textf) 

score<- scorel(Text, disgust, .progress='text') 
+4

Sind Sie sich bewusst von 'tm' Paket? [Diesen Link prüfen] (http://stackoverflow.com/questions/37526550/removing-stopwords-from-a-user-defined-corpus-in-r) – Sotos

+0

In diesem Link müssen wir zu Corpus konvertieren und dann stop entfernen Worte ... Gibt es eine Möglichkeit, wie wir zurück in den Datenrahmen konvertieren und ihn an die obige Funktion übergeben können. –

+0

@Olay Bitte überprüfen Sie die Antwort, die ich zur Verfügung gestellt habe. Sie können den vorgeschlagenen Code direkt in den Code kopieren, der in der Frage erwähnt wurde. –

Antwort

1

Mit tm Paket wie folgt:

corpus <- Corpus(VectorSource(sentence)) # Convert input data to corpus 
corpus <- tm_map(corpus, removeWords, stopwords('english')) # Remove stop word using tm package 
dataframe<-data.frame(text=unlist(sapply(corpus, `[`, "content")), 
        stringsAsFactors=F) # Convert data back to data frame from corpus 
sentence<-as.character(dataframe) 

R Konsolenausgabe wie folgt lautet:

> sentence=c('this is an best example','A person is nice') 
> sentence 
[1] "this is an best example" "A person is nice"  
> corpus <- Corpus(VectorSource(sentence)) 
> corpus <- tm_map(corpus, removeWords, stopwords('english')) 
> dataframe<-data.frame(text=unlist(sapply(corpus, `[`, "content")), 
+      stringsAsFactors=F) 
> sentence<-as.character(dataframe) 
> sentence 
[1] "c(\" best example\", \"A person nice\")"