-
Notifications
You must be signed in to change notification settings - Fork 73
/
Flipkart_Reviews_Extraction_And_Sentiment_Analysis.py
194 lines (157 loc) · 6 KB
/
Flipkart_Reviews_Extraction_And_Sentiment_Analysis.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
#Create a conda environment and install the required libraries
#conda create -n flipkart python=3.9
#conda activate flipkart
#pip install flask nltk requests numpy bs4 matplotlib wordcloud
import re
import os
import nltk
import joblib
import requests
import numpy as np
from bs4 import BeautifulSoup
import urllib.request as urllib
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
from wordcloud import WordCloud,STOPWORDS
from flask import Flask,render_template,request
import time
# Flipkart Reviews extraction and sentiment analysis
# nltk.download('stopwords')
# nltk.download('punkt')
# nltk.download('wordnet')
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
def clean(x):
x = re.sub(r'[^a-zA-Z ]', ' ', x) # replace evrything thats not an alphabet with a space
x = re.sub(r'\s+', ' ', x) #replace multiple spaces with one space
x = re.sub(r'READ MORE', '', x) # remove READ MORE
x = x.lower()
x = x.split()
y = []
for i in x:
if len(i) >= 3:
if i == 'osm':
y.append('awesome')
elif i == 'nyc':
y.append('nice')
elif i == 'thanku':
y.append('thanks')
elif i == 'superb':
y.append('super')
else:
y.append(i)
return ' '.join(y)
def extract_all_reviews(url, clean_reviews, org_reviews,customernames,commentheads,ratings):
with urllib.urlopen(url) as u:
page = u.read()
page_html = BeautifulSoup(page, "html.parser")
reviews = page_html.find_all('div', {'class': 't-ZTKy'})
commentheads_ = page_html.find_all('p',{'class':'_2-N8zT'})
customernames_ = page_html.find_all('p',{'class':'_2sc7ZR _2V5EHH'})
ratings_ = page_html.find_all('div',{'class':['_3LWZlK _1BLPMq','_3LWZlK _32lA32 _1BLPMq','_3LWZlK _1rdVr6 _1BLPMq']})
for review in reviews:
x = review.get_text()
org_reviews.append(re.sub(r'READ MORE', '', x))
clean_reviews.append(clean(x))
for cn in customernames_:
customernames.append('~'+cn.get_text())
for ch in commentheads_:
commentheads.append(ch.get_text())
ra = []
for r in ratings_:
try:
if int(r.get_text()) in [1,2,3,4,5]:
ra.append(int(r.get_text()))
else:
ra.append(0)
except:
ra.append(r.get_text())
ratings += ra
print(ratings)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/results',methods=['GET'])
def result():
url = request.args.get('url')
nreviews = int(request.args.get('num'))
clean_reviews = []
org_reviews = []
customernames = []
commentheads = []
ratings = []
with urllib.urlopen(url) as u:
page = u.read()
page_html = BeautifulSoup(page, "html.parser")
proname = page_html.find_all('span', {'class': 'B_NuCI'})[0].get_text()
price = page_html.find_all('div', {'class': '_30jeq3 _16Jk6d'})[0].get_text()
# getting the link of see all reviews button
all_reviews_url = page_html.find_all('div', {'class': 'col JOpGWq'})[0]
all_reviews_url = all_reviews_url.find_all('a')[-1]
all_reviews_url = 'https://www.flipkart.com'+all_reviews_url.get('href')
url2 = all_reviews_url+'&page=1'
# start reading reviews and go to next page after all reviews are read
while True:
x = len(clean_reviews)
# extracting the reviews
extract_all_reviews(url2, clean_reviews, org_reviews,customernames,commentheads,ratings)
url2 = url2[:-1]+str(int(url2[-1])+1)
if x == len(clean_reviews) or len(clean_reviews)>=nreviews:break
org_reviews = org_reviews[:nreviews]
clean_reviews = clean_reviews[:nreviews]
customernames = customernames[:nreviews]
commentheads = commentheads[:nreviews]
ratings = ratings[:nreviews]
# building our wordcloud and saving it
for_wc = ' '.join(clean_reviews)
wcstops = set(STOPWORDS)
wc = WordCloud(width=1400,height=800,stopwords=wcstops,background_color='white').generate(for_wc)
plt.figure(figsize=(20,10), facecolor='k', edgecolor='k')
plt.imshow(wc, interpolation='bicubic')
plt.axis('off')
plt.tight_layout()
CleanCache(directory='static/images')
plt.savefig('static/images/woc.png')
plt.close()
# making a dictionary of product attributes and saving all the products in a list
d = []
for i in range(len(org_reviews)):
x = {}
x['review'] = org_reviews[i]
# x['sent'] = predictions[i]
x['cn'] = customernames[i]
x['ch'] = commentheads[i]
x['stars'] = ratings[i]
d.append(x)
for i in d:
if i['stars']!=0:
if i['stars'] in [1,2]:
i['sent'] = 'NEGATIVE'
else:
i['sent'] = 'POSITIVE'
np,nn =0,0
for i in d:
if i['sent']=='NEGATIVE':nn+=1
else:np+=1
return render_template('result.html',dic=d,n=len(clean_reviews),nn=nn,np=np,proname=proname,price=price)
@app.route('/wc')
def wc():
return render_template('wc.html')
class CleanCache:
'''
this class is responsible to clear any residual csv and image files
present due to the past searches made.
'''
def __init__(self, directory=None):
self.clean_path = directory
# only proceed if directory is not empty
if os.listdir(self.clean_path) != list():
# iterate over the files and remove each file
files = os.listdir(self.clean_path)
for fileName in files:
print(fileName)
os.remove(os.path.join(self.clean_path,fileName))
print("cleaned!")
if __name__ == '__main__':
app.run(debug=True)
# this was the code for Flipkart Reviews extraction and sentiment analysis