-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsentiwn.py
More file actions
48 lines (44 loc) · 1.33 KB
/
sentiwn.py
File metadata and controls
48 lines (44 loc) · 1.33 KB
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
'''
Created on Dec 12, 2018
@author: dan
'''
from nltk.corpus import sentiwordnet as swn
import string
import fire
maketrans = str.maketrans
senti_dic={}
translate_dict = dict((c, " ") for c in string.punctuation)
translate_map = maketrans(translate_dict)
def _processing(line):
try:
line=line.lower()
text = line.translate(translate_map)
for t in text.split(): # sentence segmentation
if t not in senti_dic:
senti_dic[t]=None
except:
print("exp:",line)
def _get_senti(s_s):
slst=[s_s.pos_score(),s_s.neg_score(),s_s.obj_score()]
return (str(slst.index(max(slst))) ,",".join([str(s) for s in slst]))
def senti(file_path):
with open(file_path) as f:
for line in f:
_processing(line)
for t in senti_dic:
try:
senti_synset=list(swn.senti_synsets(t))
if len(senti_synset)==0:
continue
s=senti_synset[0]
senti_dic[t]=_get_senti(s)
except:
print("EXP:",t)
continue
with open(file_path+".senti.json","w") as w:
for s in senti_dic:
if senti_dic[s] is None:
continue
w.write(s+"\t"+senti_dic[s][0]+"\t"+senti_dic[s][1])
w.write("\n")
fire.Fire()