-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReviewMaching.py
289 lines (212 loc) · 7.87 KB
/
ReviewMaching.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from Product import *
from SearchResPage import *
from Xpath import *
from IdentifyReview import *
import re
'''
Create a list of product information
where each entry contain
whole detail about product.
'''
def getProductList(filename):
productList = []; doc = []
inf = open(filename)
for line in inf:
for stream in line.split('|'):
token = stream.split('\n')[0]
if '</page>' in token:
currentDoc = ''.join(doc)
revTitle = re.search('<title>(.*?)</title>', currentDoc, re.DOTALL)
revSpec = re.search('<spec>(.*?)</spec>', currentDoc, re.DOTALL)
imp = re.search('<imp>(.*?)</imp>', currentDoc, re.DOTALL)
cat = re.search('<cat>(.*?)</cat>', currentDoc, re.DOTALL)
ptitleid = re.search('<ptitleid>(.*?)</ptitleid>', currentDoc, re.DOTALL)
pid = re.search('<pid>(.*?)</pid>', currentDoc, re.DOTALL)
try: # for title
title = revTitle.group(1)
for el in title.split('\n'):
if el is '':
continue
else:
title = el
break
except Exception:
print 'Unstructured Product Info File : no <title> tag found'
try: #for specification
spec = revSpec.group(1)
except Exception:
print 'Unstructured Product Info File : no <spec> tag found'
try: #for imp tag
imp = imp.group(1)
implist = []
for el in imp.split('\n'):
if el == '':
continue
else:
for word in el.split(' '):
if word.strip() == '':
continue
implist.append(word)
break
except Exception:
print 'Unstructured Product Info File : no <imp> tag found '
try: #for cat tag
cat = cat.group(1)
for el in cat.split('\n'):
if el == '':
continue
else:
cat = el
break
except Exception:
print 'Unstructured Product Info File : no <cat> tag found '
try: #for ptitleid tag
ptitleid = ptitleid.group(1)
for el in ptitleid.split('\n'):
if el == '':
continue
else:
ptitleid = el
break
except Exception:
print 'Unstructured Product Info File : no <ptitleid> tag found '
try: #for pid tag
pid = pid.group(1)
for el in pid.split('\n'):
if el == '':
continue
else:
pid = el
break
except Exception:
print 'Unstructured Product Info File : no <pid> tag found '
productList.append(Product(title, spec, implist, cat, ptitleid, pid))
doc = []
doc.append(stream)
return productList
def getDomainMap(filename):
file = open(filename)
string = ''
domainMap = {}
for line in file:
string+=line
all = re.search('<all>(.*?)</all>', string, re.DOTALL)
try:
list =[]
for line in all.group(1).split('\n'):
if line.strip() == '':
continue
else:
list.append(line.strip())
domainMap['all'] = list
except Exception:
print 'Error in file :', filename, ' : no <all> tag is found'
ele = re.search('<electronics>(.*?)</electronics>', string, re.DOTALL)
try:
list =[]
for line in ele.group(1).split('\n'):
if line.strip() == '':
continue
else:
list.append(line.strip())
domainMap['electronics'] = list
except Exception:
print 'Error in file :', filename, ' : no <electronic> tag is found'
app = re.search('<appliances>(.*?)</appliances>', string, re.DOTALL)
try:
list =[]
for line in app.group(1).split('\n'):
if line.strip() == '':
continue
else:
list.append(line.strip())
domainMap['appliances'] = list
except Exception:
print 'Error in file :', filename, ' : no <appliances> tag is found'
comp = re.search('<computers>(.*?)</computers>', string, re.DOTALL)
try:
list =[]
for line in comp.group(1).split('\n'):
if line.strip() == '':
continue
else:
list.append(line.strip())
domainMap['computers'] = list
except Exception:
print 'Error in file :', filename, ' : no <computers> tag is found'
other = re.search('<others>(.*?)</others>', string, re.DOTALL)
try:
list =[]
for line in other.group(1).split('\n'):
if line.strip() == '':
continue
else:
list.append(line.strip())
domainMap['others'] = list
except Exception:
print 'Error in file :', filename, ' : no <others> tag is found'
return domainMap
def getDomainSearchLinkMap(filename):
file = open(filename)
dsmap = {}
for line in file:
try:
tokens = line.split('|')
domain = tokens[0].strip()
s_link = tokens[1].strip()
dsmap[domain] = s_link.split('\n')[0]
except Exception:
print 'File Format Error : ', filename
return dsmap
'''
Main module
'''
def main():
productList = getProductList('textfile/productInfo.txt')
d_map = getDomainMap('textfile/domainList.txt')
d_slink_map = getDomainSearchLinkMap('textfile/searchLink.txt')
'''
Object of xpath class
'''
xp = Xpath()
xp.loadXpaths('textfile/xpath.txt', 'textfile/allDomain.txt' )
sp = SearchResPage(xp)
ir = IdentifyReview(xp)
for el in productList:
cat = el.getCatogary()
'''
Get all domain from which we have to find reviews.
'''
dset = set()
if cat in d_map.keys():
try:
for domain in d_map[cat]:
dset.add(domain)
except Exception:
print 'no domain in catogary : ', cat
else:
for domain in d_map['others']: # if no domain for product category website we take domain
dset.add(domain) # from other category
for domain in d_map['all']: # this category websites always included
dset.add(domain)
'''
Search Link for Domains
'''
s_link = []
for dom in dset:
s_link.append(d_slink_map[dom])
ppUrlSet = set()
for slink in s_link:
plink_list = sp.getProductPageURLs(el.getTitle(), slink)
for plink in plink_list:
#if title and specification mached in this
#link then add this link to file reviewurl.txt
if ir.mach(el, plink):
ppUrlSet.add(plink)
print '************ Mached Link ************'
for link in ppUrlSet:
print link
print '************ end ********************'
#save mached link to file
if __name__ == '__main__':
main()