단어 목록을 문장 목록과 대조하고 일치하는 단어와 문장이있는 데이터 프레임을 구성하려고합니다.단어 목록을 문장 목록과 일치 시키려고 할 때 성능 문제가 발생했습니다. R
sentences words
This document is far better better
This is a great app great
The night skies were sombre and starless sombre
The app is too good and i am happy using it good, happy
This is how it works -
을 내가 이것을 달성하기 위해 다음 코드를 사용하고 예를 들어 다음과 같이
words <- c("far better","good","great","sombre","happy")
sentences <- c("This document is far better","This is a great app","The night skies were sombre and starless", "The app is too good and i am happy using it", "This is how it works")
예상되는 결과 (A dataframe)입니다.
lengthOfData <- nrow(sentence_df)
pos.words <- polarity_table[polarity_table$y>0]$x
neg.words <- polarity_table[polarity_table$y<0]$x
positiveWordsList <- list()
negativeWordsList <- list()
for(i in 1:lengthOfData){
sentence <- sentence_df[i,]$comment
#sentence <- gsub('[[:punct:]]', "", sentence)
#sentence <- gsub('[[:cntrl:]]', "", sentence)
#sentence <- gsub('\\d+', "", sentence)
sentence <- tolower(sentence)
# get unigrams from the sentence
unigrams <- unlist(strsplit(sentence, " ", fixed=TRUE))
# get bigrams from the sentence
bigrams <- unlist(lapply(1:length(unigrams)-1, function(i) {paste(unigrams[i],unigrams[i+1])}))
# .. and combine into data frame
words <- c(unigrams, bigrams)
#if(sentence_df[i,]$ave_sentiment)
pos.matches <- match(words, pos.words)
neg.matches <- match(words, neg.words)
pos.matches <- na.omit(pos.matches)
neg.matches <- na.omit(neg.matches)
positiveList <- pos.words[pos.matches]
negativeList <- neg.words[neg.matches]
if(length(positiveList)==0){
positiveList <- c("-")
}
if(length(negativeList)==0){
negativeList <- c("-")
}
negativeWordsList[i]<- paste(as.character(unique(negativeList)), collapse=", ")
positiveWordsList[i]<- paste(as.character(unique(positiveList)), collapse=", ")
positiveWordsList[i] <- sapply(positiveWordsList[i], function(x) toString(x))
negativeWordsList[i] <- sapply(negativeWordsList[i], function(x) toString(x))
}
positiveWordsList <- as.vector(unlist(positiveWordsList))
negativeWordsList <- as.vector(unlist(negativeWordsList))
scores.df <- data.frame(ave_sentiment=sentence_df$ave_sentiment, comment=sentence_df$comment,pos=positiveWordsList,neg=negativeWordsList, year=sentence_df$year,month=sentence_df$month,stringsAsFactors = FALSE)
28k 문장과 65k 단어가 있습니다. 위의 코드는 작업을 완료하는 데 45 초가 걸립니다. 현재 접근 방식으로 코드의 성능을 향상시키는 방법에 대한 제안은 많은 시간이 필요합니까?
편집 : 나는 정확하게 문장에있는 단어와 일치하는 경우에만 해당 단어를 얻고 싶은
. 예를 들면 :
sentences words
Since the app crashes frequently, I advice you guys to fix crahses
the issue ASAP
당신은 병렬로 그것을 할 수 있습니다. –
그게 더 나아 졌습니까? '도서관 (stringi); –
@David 나는이 솔루션을 사용하여 계산 시간을 줄 였지만 출력을 데이터 프레임으로 필요로 할 때 그 방법을 말해 줄 수 있습니까? (예 : 문장, function (x) toString (words [stri_detect_fixed (x, words)]) – Venu