-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparser.py
205 lines (132 loc) · 4.36 KB
/
parser.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
"""
Parser
========
Parse the txt files for outputting the reviews as a list of tuples
where each tuple is
(filename, linenumber, aggregatevote, sentence)
author = "Shreyas"
email = "[email protected]"
python_version = "Python 2.7.5 :: Anaconda 1.6.1 (x86_64)"
"""
from __future__ import division
from optparse import OptionParser
import os
import re
def getInput():
optionparser = OptionParser()
optionparser.add_option('-t', '--train', dest='train')
(option, args) = optionparser.parse_args()
if not option.train:
return optionparser.error('data not provided.\n Usage: --data="path.to.data"')
return { 'train' : option.train }
def getFiles(dir):
# os.lisdir(dir)
os.chdir(dir)
datafiles = [f for f in os.listdir('.') if f.endswith('.txt')]
return datafiles
def parseFiles(fList):
allSents = []
for f in fList:
fileObj = open(f)
linenum = 0
for l in fileObj:
linenum += 1
if not l.startswith('*'):
#find all occurrence of ## in the sentence
delimPos = [(a.start(), a.end()) for a in list(re.finditer('##', l))]
if len(delimPos) == 0:
# no reviews in a sentence
feat = 'None'
rev = '[t]'
vote = 'neutral'
allSents.append((f, linenum, vote, rev))
# print l
elif len(delimPos) == 1:
#simple case of only 1 sentence
feat = l.split('##')[0]
vote = getVotes(feat)
rev = l.split('##')[1]
new_vote = standardizeVote(vote)
rev = re.sub('\\r\\n', '', rev)
allSents.append((f, linenum, new_vote, rev))
else:
# more than 1 reviews in a sentence
feat1 = l.split('##')[0]
rev2 = l.split('##')[2]
rev2 = re.sub('\\r\\n', '', rev2)
rev1_feat2 = l.split('##')[1]
rev1 = cleanReview(rev1_feat2)
rev1 = re.sub('\\r\\n', '', rev1)
vote1 = getVotes(feat1)
vote2 = getVotes(rev1_feat2)
new_vote1 = standardizeVote(vote1)
new_vote2 = standardizeVote(vote2)
allSents.append((f, linenum, new_vote1, rev1))
allSents.append((f, linenum, new_vote2, rev2))
# print allSents[-1]
# to check error handling uncomment the code below
# print vote1, rev1
# print vote2, rev2
fileObj.close()
return allSents
def standardizeVote(v):
new_v = ''
if v > 0:
new_v = 'pos'
elif v == 0:
new_v = 'neutral'
else:
new_v = 'neg'
return new_v
def cleanReview(revstr):
"""
return the cleaned up review after getting a raw string
"""
eolregEx = re.compile('[\.|\?]')
voteregEx = re.compile('\[[\+\-][0-3]?\]')
eol = [int(a.end()) for a in eolregEx.finditer(revstr)]
# print eol
if eol:
cleanrev = revstr[:eol[-1]]
temp = revstr[eol[-1]:]
if not voteregEx.search(temp):
cleanrev.join(temp)
else:
cleanrev = 'N.A.'
# print revstr
# print cleanrev, '\n'
return cleanrev
def getVotes(rawstr):
voteregEx = re.compile('\[[\+\-][0-3]?\]')
vote_raw = voteregEx.findall(rawstr)
if len(vote_raw) == 0:
# rev1 = rawstr
# feat2 = 0
aggrvote = 0.0
else:
votes = []
for vote in vote_raw:
v = vote[1:-1]
if v == '+':
vt = 1
elif v == '-':
vt = -1
else:
vt = int(v)
votes.append(vt)
# meanvote = sum(votes)/len(votes)
aggrvote = sum(votes)
return aggrvote + 0.0
def main():
userInput = getInput()
fileList = getFiles(userInput['train'])
sents = parseFiles(fileList)
# print fileList
print "-" * 79
print "No. of files parsed: %d" % (len(fileList))
print sents[:2]
print "Total No of sentences: %d" % (len(sents))
if __name__ == "__main__":
main()