-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
279 lines (221 loc) · 9.05 KB
/
test.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
from flask import Flask, request, render_template
import requests
import pandas as pd
from bs4 import BeautifulSoup
import string
import re
import unicodedata
from textblob import TextBlob
import string
import math
app = Flask(__name__)
@app.route("/")
def my_form():
return render_template("form.html")
@app.route("/", methods=["POST"])
def my_form_post():
reviews_url = request.form["productLink"].strip()
try:
headers = {
"authority": "www.amazon.com",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language": "en-US,en;q=0.9,bn;q=0.8",
"sec-ch-ua": '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
}
s = requests.Session()
def reviewsHtml(url, len_page):
soups = []
for page_no in range(1, len_page + 1):
params = {
"ie": "UTF8",
"reviewerType": "all_reviews",
"filterByStar": "critical",
"pageNumber": page_no,
}
response = requests.get(url, headers=headers)
if response.status_code != 200:
error = f"Invalid URL - Status Code: {response.status_code}"
return render_template("error.html", error=error)
soup = BeautifulSoup(response.text, "lxml")
soups.append(soup)
return soups
def getReviews(html_data):
data_dicts = []
boxes = html_data.select('div[data-hook="review"]')
for box in boxes:
try:
description = box.select_one(
'[data-hook="review-body"]'
).text.strip()
except Exception as e:
description = "N/A"
data_dict = {"Text": description}
data_dicts.append(data_dict)
return data_dicts
len_page = 10
def get_len_page(len_page):
response = requests.get(reviews_url, headers=headers)
if response.status_code != 200:
error = f"Invalid URL - Status Code: {response.status_code}"
return render_template("error.html", error=error)
rating_count_string2 = BeautifulSoup(response.text, "lxml").select(
'div[data-hook="cr-filter-info-review-rating-count"]'
)
rating_count_string = " "
rating_count_string = [element.text.strip() for element in rating_count_string2]
temp = rating_count_string[0]
with_reviews_index = temp.find("total ratings,")
if with_reviews_index != -1:
substring_before = temp[with_reviews_index:].strip()
len_page = "".join(char for char in substring_before if char.isdigit())
return int(len_page)
len_page = get_len_page(len_page)
html_datas = reviewsHtml(reviews_url, math.floor(len_page / 10))
reviews = []
for html_data in html_datas:
review = getReviews(html_data)
reviews += review
df_reviews = pd.DataFrame(reviews)
df_reviews.to_csv("./input/Reviews.csv", index=False)
df = pd.read_csv("./input/Reviews.csv")
df.head()
df.info()
df["Text"]
def punctuation_removal(messy_str):
clean_list = [char for char in messy_str if char not in string.punctuation]
clean_str = "".join(clean_list)
return clean_str
df["Text"] = df["Text"].apply(punctuation_removal)
def drop_numbers(list_text):
list_text_new = []
for i in list_text:
if not re.search(r"\d", i):
list_text_new.append(i)
return "".join(list_text_new)
df["Text"] = df["Text"].apply(drop_numbers)
df["Text"].head(10)
def remove_accented_chars(text):
new_text = (
unicodedata.normalize("NFKD", text)
.encode("ascii", "ignore")
.decode("utf-8", "ignore")
)
return new_text
df["Text"] = df.apply(lambda x: remove_accented_chars(x["Text"]), axis=1)
def remove_special_characters(text):
pat = r"[^a-zA-z0-9]"
return re.sub(pat, " ", text)
df["Text"] = df.apply(lambda x: remove_special_characters(x["Text"]), axis=1)
df.isnull().sum()
df["length"] = df["Text"].apply(len)
def get_polarity(text):
textblob = TextBlob(str(text.encode("utf-8")))
pol = textblob.sentiment.polarity
return pol
df["polarity"] = df["Text"].apply(get_polarity)
def get_subjectivity(text):
textblob = TextBlob(str(text.encode("utf-8")))
subj = textblob.sentiment.subjectivity
return subj
df["subjectivity"] = df["Text"].apply(get_subjectivity)
df[["length", "polarity", "subjectivity"]].describe()
df["char_count"] = df["Text"].apply(len)
df["word_count"] = df["Text"].apply(lambda x: len(x.split()))
df["word_density"] = df["char_count"] / (df["word_count"] + 1)
punctuation = string.punctuation
df["punctuation_count"] = df["Text"].apply(
lambda x: len("".join(_ for _ in x if _ in punctuation))
)
df[["char_count", "word_count", "word_density", "punctuation_count"]].describe()
def get_polarity(text):
textblob = TextBlob(str(text))
pol = textblob.sentiment.polarity
if pol == 0:
return "Neutral"
elif pol > 0 and pol <= 0.3:
return "Weakly Positive"
elif pol > 0.3 and pol <= 0.6:
return "Positive"
elif pol > 0.6 and pol <= 1:
return "Strongly Positive"
elif pol > -0.3 and pol <= 0:
return "Weakly Negative"
elif pol > -0.6 and pol <= -0.3:
return "Negative"
elif pol > -1 and pol <= -0.6:
return "Strongly Negative"
df["polarity"] = df["Text"].apply(get_polarity)
df["polarity"].value_counts()
neutral = 0
wpositive = 0
spositive = 0
positive = 0
negative = 0
wnegative = 0
snegative = 0
polarity = 0
NoOfTerms = len(df["Text"])
for i in range(0, NoOfTerms):
textblob = TextBlob(str(df["Text"][i]))
polarity += textblob.sentiment.polarity
pol = textblob.sentiment.polarity
if pol == 0:
neutral += 1
elif pol > 0 and pol <= 0.3:
wpositive += 1
elif pol > 0.3 and pol <= 0.6:
positive += 1
elif pol > 0.6 and pol <= 1:
spositive += 1
elif pol > -0.3 and pol <= 0:
wnegative += 1
elif pol > -0.6 and pol <= -0.3:
negative += 1
elif pol > -1 and pol <= -0.6:
snegative += 1
polarity = polarity / NoOfTerms
polarity
def percentage(part, whole):
temp = 100 * float(part) / float(whole)
return format(temp, ".2f")
positive = percentage(positive, NoOfTerms)
wpositive = percentage(wpositive, NoOfTerms)
spositive = percentage(spositive, NoOfTerms)
negative = percentage(negative, NoOfTerms)
wnegative = percentage(wnegative, NoOfTerms)
snegative = percentage(snegative, NoOfTerms)
neutral = percentage(neutral, NoOfTerms)
if polarity == 0:
print("Neutral")
elif polarity > 0 and polarity <= 0.3:
print("Weakly Positive")
elif polarity > 0.3 and polarity <= 0.6:
print("Positive")
elif polarity > 0.6 and polarity <= 1:
print("Strongly Positive")
elif polarity > -0.3 and polarity <= 0:
print("Weakly Negative")
elif polarity > -0.6 and polarity <= -0.3:
print("Negative")
elif polarity > -1 and polarity <= -0.6:
print("Strongly Negative")
print()
print(
"------------------------------------------------------------------------------------------"
)
return render_template(
"result.html",
positive=positive,
negative=negative,
wpositive=wpositive,
neutral=neutral,
spositive=spositive,
wnegative=wnegative,
snegative=snegative,
)
except requests.exceptions.RequestException as e:
error = f"Error fetching the URL: {str(e)}"
return render_template("error.html", error=error)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")