2016-06-21 11 views
1

어휘에 기반한 채점 방법을 사용하여 텍스트에 대한 몇 가지 정서 분석을 시도하고 있습니다. 내가 직접 스택 오버플로 포스트 읽은 후 http://analyzecore.com/2014/04/28/twitter-sentiment-analysis/에서 내 코드를 빌려 :R (코드가 올바르게 작동하지 않음)을 사용하는 센티멘트 분석

> summary(data$text) 
    Length  Class  Mode 
     30 character character 
> str(data$text) 
chr [1:30] "Hey everybody, are you guys free on Sunday for a game play + dinner afterwards? I'll reserve a"| __truncated__ ... 

와 내가 사용하고 코드 :

require(plyr) 
require(stringr) 
require(data.table) 
score.sentiment = function(sentences, pos.words, neg.words, .progress='none') 
{ 
    scores = laply(sentences, function(sentence, pos.words, neg.words) { 

    sentence = gsub('[[:punct:]]', '', sentence) 
    sentence = gsub('[[:cntrl:]]', '', sentence) 
    sentence = gsub('\\d+', '', 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+') 
    # sometimes a list() is one level of hierarchy too much 
    words = unlist(word.list) 

    # compare our words to the dictionaries of positive & negative terms 
    pos.matches = match(words, pos.words) 
    neg.matches = match(words, neg.words) 

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

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

    return(score) 
    } , pos.words, neg.words, .progress=.progress) 

    scores.df = data.frame(score = scores, text = sentences) 
    return(scores.df) 
} 
다음 R sentiment analysis with phrases in dictionaries

을 내 데이터 세트에 대한 약간의 요약입니다

Bing Liu의 의견 사전을 사용 중입니다.

pos_BL = read.table(file = 'positive-words.txt', stringsAsFactors = F) 
neg_BL = read.table(file = 'negative-words.txt', stringsAsFactors = F) 
아무리 내가 뭘, 난 단지 내 모든 30 문자열을 0의 점수를 얻을, 그러나

score_result = score.sentiment(sentences = data$text, 
           pos.words = pos_BL, 
           neg.words = neg_BL, 
           .progress= 'text') 

:210

여기에 내가 채점 기능을 통해 데이터와 사전을 실행하는 데 사용되는 코드입니다. (출력 요약 아래 표 참조)

> table(score_result$score) 
0 
30 

내가 (여기에이 질문을 게시하기 전에 내가 내 자신의 코드에 자리 많은 오류를했다) 고정 위치에 대한 아이디어 중입니다. 어떤 도움을 많이 주시면 감사하겠습니다!

+0

체크 아웃 극성! –

+0

@ChirayuChamoli 당신은 그것에 대해 좀 더 자세히 설명해 줄 수 있습니까? 그것은 무엇이며 어디서 간단한 튜토리얼을 찾을 수 있습니까? :) – alwaysaskingquestions

+0

기능의 극성은 기본적으로 당신이하는 것과 똑같은 일을하지만 그것은 sa를 구현하는 것이 훨씬 낫습니다. 표시되는 소스 코드를 확인하십시오. 구문에 대해? 극성을 확인하십시오. –

답변

0

예 : qdap 패키지

list=list(a='This place is awesome', b='I failed in the exam') 
lapply(list, polarity) 
+0

안녕하세요 chirayu, qdap 패키지를로드하려고했지만 아무리 여러 번 시도해도 항상 "오류 : qdap '이라는 패키지가 없습니다. 다른 모든 필요한 종속성 라이브러리가 이미로드되어 있습니다. 가능한 문제는 무엇일까요? 너 알아? 고맙습니다! – alwaysaskingquestions

+0

[여기] (https://cran.r-project.org/web/packages/qdap/index.html) –

+0

hi chirayu에서 수동으로 패키지를로드 할 수 있습니다. 나는 초보자이기 때문에 제 질문에 답해주십시오 ... 당신이 공유 한 링크를 클릭했는데, 그 페이지를 읽는 패키지를로드하는 방법을 아직도 모르겠습니다. ( – alwaysaskingquestions