def compute_sentiment_score_byAnew(wordsList): arousalScore = 0.0 valenceScore = 0.0 for word in wordsList: if anew.exist(word): arousalScore += anew.arousal(word) valenceScore += anew.valence(word) return (arousalScore, valenceScore)
def compute_sentiment_score_byAnew(wordsList): arousalScore = 0.0 valenceScore = 0.0 for word in wordsList: if anew.exist(word): arousalScore += anew.arousal(word) valenceScore += anew.valence(word) return (arousalScore,valenceScore)
def stringfunction(string): result = [] valence = [] arousal = [] k = 1 line = "" for i in string: #break sentences into paragraphs if i not in (".", "!", "?"): line = "".join([line, i]) else: k == k + 1 #feed each sentence through the ANEW scorer #First check to see that each line has at least 2 terms term_list = line.split() total = 0 for j in term_list: if anew.exist(j) == True: total = total + 1 if total > 1: #At least 2 registering terms, score the line! #return arousal with the highest valence! grFiveV = False grFiveA = False toBeat = 0 key = anew.sentiment(term_list) #finds distance value is from 5, puts into lisis #retains +/- from center (5) if key['valence'] > 5: grFiveV = True else: grFiveV = False if key['arousal'] > 5: grFiveA = True else: grFiveA = False #how far from 5 valence.append(key['valence']) arousal.append(key['arousal']) line = "" #takes max list value and puts into result list. #choose biggest valence away from 5 goal = 0 current1 = 0 for l in valence: if abs(l - 5) > goal: current1 = l #repeat for arousal goal = 0 current2 = 0 for n in arousal: if abs(n - 5) > goal: current2 = n result.append(current1) #valence result.append(current2) #arousal return result
#iterating over term vectors, obtaining sentiment valence and arousal if more than two terms exist in the dictionary print("Cleaning and scoring") #keeping track of the vid column vid = -1 for item in text_noasc: termlist = item.split() clean_terms = [] vid += 1 #remove the stopwords for words in termlist: if words not in stopwords: clean_terms.append(words) if sum(anew.exist(clean_terms)) >= 2: valence.append((anew.sentiment(clean_terms))['valence']) arousal.append((anew.sentiment(clean_terms))['arousal']) scored_terms.append([term for term in clean_terms if anew.exist(term)]) video_ids.append((data['vid'][vid]).split()[:2]) #cleaning up the candidate names video_ids = [' '.join(vid) for vid in video_ids] #cleaning up the terms scored_terms = [','.join(s_term) for s_term in scored_terms] #combining the list of terms with their scores values = zip(scored_terms, valence, arousal, video_ids) #writing file
d = punc.sub( '', d ) term_vec.append( word_tokenize( d ) ) print len(term_vec) # term_list = term_vec[100] # print anew.exist(term_list) # print anew.sentiment(term_list) valence=[] arousal=[] sentiment_list=[] for i in term_vec: if sum(anew.exist(i)) >=2: sentiment_list.append(i) valence.append ((anew.sentiment(i))['valence']) arousal.append ((anew.sentiment(i))['arousal']) values = zip(sentiment_list, valence, arousal, email_id) print ('Writing file') headers = ['sentiment list','valence', 'arousal', 'ID'] with open('email_scores.csv', 'wb') as out: writer = csv.writer( out ) #write headers writer.writerow(headers) #write the csv file for value in values: writer.writerow(value)