-
Notifications
You must be signed in to change notification settings - Fork 0
/
opinionextraction.py
218 lines (193 loc) · 6.75 KB
/
opinionextraction.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from nltk.tokenize import StanfordTokenizer
import nltk
from nltk import *
import codecs
from nltk.stem import WordNetLemmatizer
def readreview(file_path):
file_object = codecs.open(file_path,'r','utf-8')
reviewarr = []
wordnet_lemmatizer = WordNetLemmatizer()
symbols = getsymbol('F:/course/sentimentcode/feature/symbol.txt')
try:
lines = file_object.readlines()
for line in lines:
line = line.replace('\n','')
for s in symbols:
if s in line:
line = line.replace(s,' ')
ar = line.split()
newarr = []
for i in ar:
a = wordnet_lemmatizer.lemmatize(i)
newarr.append(a)
reviewarr.append(newarr)
finally:
file_object.close()
return reviewarr
def getnounandadj(file_path):
reviewarr = readreview(file_path)
nountag = "NN"
adjtag = "JJ"
noun=[]
adj=[]
arrwithna = []
index = []
initindex = 0
for arr in reviewarr:
newarr = []
taglist = nltk.pos_tag(arr)
l = len(taglist)
tempnoun = set()
tempadj = set()
for i in range(l):
if taglist[i][1] == nountag:
tempnoun.add(taglist[i][0])
newarr.append(taglist[i][0])
if taglist[i][1] == adjtag:
tempadj.add(taglist[i][0])
newarr.append(taglist[i][0])
if not len(tempnoun)==0:
noun.append(list(tempnoun))
adj.append(list(tempadj))
arrwithna.append(newarr)
index.append(initindex)
initindex+=1
return noun,adj,arrwithna,index
def create_txt(file_path, noun, adj):
if os.path.exists(file_path):
os.remove(file_path)
f=codecs.open(file_path,'w','utf-8')
lennoun = len(noun)
for i in range(lennoun):
l = len(noun[i])
for j in range(l):
f.write(noun[i][j])
f.write(" ")
l = len(adj[i])
if l==0:
f.write('\n')
continue
f.write(": ")
for j in range(l):
f.write(adj[i][j])
f.write(" ")
f.write('\n')
f.close()
def getsymbol(file_path):
file_object = codecs.open(file_path,'r','utf-8')
try:
alltext = file_object.read()
symbols = alltext.split()
finally:
file_object.close()
return symbols
def create_txt1(file_path, content):
if os.path.exists(file_path):
os.remove(file_path)
f=codecs.open(file_path,'w','utf-8')
for x in content:
f.write(x)
f.write('\n')
f.close()
import json
def writedictojson(file_path, dic):
json.dump(dic, codecs.open(file_path,'w','utf-8'))
def getfeature(file_path):
file_object = codecs.open(file_path,'r','utf-8')
featurearr = []
try:
all_text = file_object.read()
arr = all_text.split()
finally:
file_object.close()
return arr
def extractopinion():
(noun, adj,arrwithna,index) = getnounandadj("F:/course/sentimentcode/feature/data/corpuswithoutstop")#handle file make it leave only noun and adj
create_txt("F:/course/sentimentcode/feature/data/nounadj",noun,adj)
featurearr = getfeature("F:/course/sentimentcode/feature/data/newfeatures")
l = len(noun)
feature = dict()
opinion = set()
reviewstr = []
for arr in arrwithna:
str = ""
for word in arr:
str += word
str += " "
reviewstr.append(str)
create_txt1("F:/course/sentimentcode/feature/data/reviewwithadjandnoun", reviewstr)
for i in range(l):
nounlen = len(noun[i])
if nounlen == 1:#just one feature in a review,let all the adj in the review as opinions
w = noun[i][0]
effadj = set()
dic = dict()
if not w in featurearr:
continue
for a in adj[i]:
opinion.add(a)
effadj.add(a)
if not w in feature:
feature[w]=[]
dic[index[i]] = list(effadj)
feature[w].append(dic)
continue
for w in noun[i]:#many features in a review,every feature get its adj near it
if not w in featurearr:
continue
if not w in feature:
feature[w]=[]
dic = dict()
effadj = set()
arr = reviewstr[i].split()
indices = [j for j, s in enumerate(arr) if w in s]
for j in indices:
temp = j-1
find = False
while temp >= 0:
if arr[temp] in effadj:
break
if arr[temp] in noun[i]:#in other noun range
break
if arr[temp] in adj[i]:
effadj.add(arr[temp])
opinion.add(arr[temp])
find = True
break
temp-=1
if find:
continue
temp = j+1
while temp<len(indices):
if arr[temp] in effadj:
break
if arr[temp] in noun[i]:
break
if arr[temp] in adj[i]:
effadj.add(arr[temp])
opinion.add(arr[temp])
break
temp += 1
# reex = '((?:\w*\W*){,3})'
# reex += '('
# reex += w
# reex += ')'
# reex += '\W*((?:\w*\W*){,3})'
# m = re.search(reex, reviewstr[i])#find effective opinions
# if m:
# l = [ x.strip().split() for x in m.groups()]
# left, right = l[0], l[2]
# for word in l[0]:
# if word in adj[i]:
# effadj.add(word)
# opinion.add(word)
# for word in l[2]:
# if word in adj[i]:
# effadj.add(word)
# opinion.add(word)
dic[index[i]] = list(effadj)
feature[w].append(dic)
create_txt1("F:/course/sentimentcode/feature/data/opinion", opinion)
writedictojson("F:/course/sentimentcode/feature/data/featuredic", feature)
# featureshandle()
feature = extractopinion()