Repository navigation
Expand file tree
/
Copy pathalgo.py
More file actions
85 lines (63 loc) · 1.43 KB
/
Copy pathalgo.py
File metadata and controls
85 lines (63 loc) · 1.43 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
import HSWN
import preprocess
import MF
import tagger
import sys
import xlrd
import translate
import moduleeng
if(len(sys.argv)!=2):
sys.exit("Usage: python algo.py <input_filename.xlsx>")
#### Read the corpus
posts = []
book = xlrd.open_workbook(sys.argv[1])
sheet = book.sheet_by_index(0)
ctr = 0
for row in sheet.col(1):
if ctr!=0:
posts.append(row.value.encode('utf-8'))
ctr+=1
#### Write posts with polarity
writeDoc = open('OUTPUT.csv','w+')
#### STEP 1 - Apply preprocessing
for i in range(len(posts)):
posts[i] = preprocess.preProcess(posts[i])
#### STEP 2 - Actual work
sno = 1
for post in posts:
## Get multipliers
totalPol = 0.0
tagdata = tagger.getTag(post)
## List of multiplying factors
MFlist = MF.getMF(tagdata)
for word in post.split(' '):
## Get tag info
for mf in MFlist:
if mf[0] == word:
mult = mf[1]
typ = mf[2]
# If word exists in HSWN
if HSWN.searchHSWN(word) != 'NF':
wordPol = HSWN.searchHSWN(word)
####### HANDLE MULTIPLIER
wordPol = wordPol*mult
totalPol += wordPol
# If word not in HSWN
else:
#print 'yes'
#pass
if typ in ['NN','VB','JJ','RB']:
inEn = translate.translate(word)
pol = moduleeng.polarity(inEn,typ)
if pol != 'NF':
totalPol += pol
print 'yes'
writeDoc.write(str(sno)+',')
if totalPol>0.1:
writeDoc.write('1\n')
elif totalPol<-0.1:
writeDoc.write('-1\n')
else:
writeDoc.write('0\n')
sno+=1