1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | library(tm) library(stringr) library(dplyr) library(tidyr) #데이터 정리정돈 패키지. library(tidytext) install.packages("tidyr") install.packages("tidytext") get_sentiments("afinn") summary(get_sentiments("afinn")) df = data.frame(get_sentiments("afinn")) hist(df$score, xlim = c(-6,6), col = "blue", breaks = 20) #breaks : 막대를 20개로 보이게 한다. oplex = data.frame(get_sentiments("bing")) table(oplex$sentiment) emolex = data.frame(get_sentiments("nrc")) table(emolex$sentiment) emolex$word[ emolex$sentiment == "anger"] # #감성분성 => 긍정 / 부정. #papers 에서의 감정 분석. c = VCorpus(DirSource("papers/")) mypaper = c inspect(c) c[[1]] #matadata : 설명. str(c[[1]]$content) class(c[[1]]$content) c[[1]]$content #문자벡터 c[[1]][1] #리스트 c[[1]][[1]] #문자벡터 length(c[[1]][[1]]) #document type안에는 길이 1짜리 벡터가 들어있다. t = c(rep(NA,24)) for(i in 1:24){ t[i] = c[[1]][[1]] # 각 문서의 contents만 추출. } #감정분석에 용이하게 사용되기 좋은 데이터 구조 data_frame #dplyr에 들어있다. my.df.text = data_frame(paper.id = 1:24, doc=t) my.df.text #문서 단위의 텍스트를 단어로. my.df.text.word = my.df.text %>% unnest_tokens(word, doc) #my df text에서 doc이라는 컬럼을 단어로 나누세요. #문서의 타입이 df가 아니라 tible 이어야 하므로 이전에 data_frame으로 바꾼것이다. #NA가 없는것만 나오므로 , get_sentiments에도 있고 text.word의 word에 있는 row만 출력된다 myresult = my.df.text.word %>% inner_join(get_sentiments("bing")) %>% count(word, paper.id, sentiment) %>% #등장 횟수 n이 추가된다. spread(sentiment, n) #원래 데이터로 존재하던 negative, positive가 열로 올라왔다. myresult myagg = summarise(group_by(myresult, paper.id) , pos.sum = sum(positive), neg.sum = sum(negative), pos.sum = pos.sum - neg.sum) myfilenames = list.files("papers", all.files = T) paper.name = myfilenames[3:26] pub.year = as.numeric(unlist(str_extract_all(paper.name, "[0-9]+"))) paper.id = 1:24 data.frame(paper.id, paper.name, pub.year) | cs |
'딥러닝 모델 설계 > R STUDIO' 카테고리의 다른 글
k-means clustering example with R studio and wine data (0) | 2019.05.28 |
---|---|
k-means clustering example with R studio and iris data (0) | 2019.05.28 |
난수, 시간측정, 그룹화 및 요약, 피봇팅, colSums, which.max (0) | 2019.05.28 |
k-means 클러스터링 예제 (0) | 2019.05.27 |
tm라이브러리를 통한 코퍼스 manipulation (0) | 2019.05.27 |