-
Notifications
You must be signed in to change notification settings - Fork 0
/
stan.py
72 lines (55 loc) · 1.42 KB
/
stan.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
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
from konlpy.tag import Okt
from konlpy.utils import pprint
import codecs
import csv
def mkNoun(filename):
src = codecs.open(filename, 'r', encoding='utf8').read()
okt = Okt()
# print(src)
# print('-' * 20)
malist = okt.nouns(src)
# malist = okt.nouns(str)
# pprint(malist)
return malist
def mkDic():
dic = {}
f = open('./polarity.csv', 'r')
rdr = csv.reader(f)
first = True
for line in rdr:
if first:
first = False
continue
if not line[0].split('/')[0] in dic:
dic[line[0].split('/')[0]] = [line[3], line[6]]
# print(dic[line[0].decode('utf-8').split('/')[0]])
f.close()
return dic
def countPN(malist, dic):
pos = 0
neg = 0
# sum = len(malist)
for m in malist:
if m[0] in dic:
pos = pos + (float)(dic[m[0]][1])
neg = neg + (float)(dic[m[0]][0])
# if dic[m[0]][1] == '1':
# print(m[0])
sum = neg + pos
return pos / sum, neg / sum
def stan(filename):
malist = mkNoun(filename)
dic = mkDic()
pos, neg = countPN(malist, dic)
print(pos, neg)
if (pos > neg):
return 1 # 1: 긍정
else:
return -1 # -1: 부정
if __name__ == '__main__':
result = stan('./src_neg.txt')
if result == 1:
print('긍정')
elif result == -1:
print('부정')