-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment.py
59 lines (41 loc) · 1.42 KB
/
sentiment.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# reads full_text.csv
# assigns sentimatent (polarity, subjectivity) to each text
# writes to sentiment.csv
from textblob_de import TextBlobDE as TextBlob
import readability
import unicodecsv as csv
import sys
import multiprocessing
csv.field_size_limit(sys.maxsize)
def main():
with open('full_text_mapped.csv','rb') as f:
r = csv.reader(f,delimiter='|', quotechar='@')
data = list(r)
results = []
results.append(data[0] + ['polarity','subjectivity','readability'])
data = data[1:] # no header
# parallel
pool = multiprocessing.Pool(processes=4)
results += pool.map(get_sentiment, data)
#for x in data[0:3]:
# results.append(get_sentiment(x))
with open('full_text_mapped_sentiment.csv','wb') as aa:
writer = csv.writer(aa, delimiter='|',quotechar = '@', quoting=csv.QUOTE_ALL)
writer.writerows(results)
def get_sentiment(row):
# last column of each row is the text
# polarity and subjectivity
blob = TextBlob(row[-1])
mood = blob.sentiment
pol_score = mood.polarity
sub_score = mood.subjectivity
# polarity is a float within the range [-1.0, 1.0]
# subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.
# readability
rb = readability.getmeasures(row[-1], lang='de')
rb_score = rb['readability grades']['FleschReadingEase']
return [row, pol_score, sub_score, rb_score]
if __name__ == "__main__":
main()