-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.py
More file actions
158 lines (139 loc) · 6.59 KB
/
Copy pathlib.py
File metadata and controls
158 lines (139 loc) · 6.59 KB
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
from email.message import Message
import pandas as pd
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
import numpy as np
import re # support regular expressions
from bs4 import BeautifulSoup
import requests
import urllib.parse
from flask import Flask, render_template, request
import pickle
from nltk.corpus import stopwords
from collections import Counter
import pyodbc as odbc
from flask_sqlalchemy import sqlalchemy
from datetime import datetime
# ----------------------------------------------------------------
# Defining Connection
# -----------------------------------------------------------------
def connection():
s = 'DESKTOP-B3U0GP9\SQLEXPRESS' # Your server name
d = 'Movies'
cstr = 'DRIVER={SQL Server};SERVER='+s+';DATABASE='+d
conn = odbc.connect(cstr)
return conn
conn = connection()
cursor = conn.cursor()
trainedModel = pickle.load(open("trainedModelNews", 'rb'))
vectorizer = pickle.load(open("vectorizer", 'rb'))
# ----------------------------------------------------------------
# Vectorization
# -----------------------------------------------------------------
def vectorization(preprocessedInput):
preprocessedInput = vectorizer.transform(preprocessedInput).toarray()
return preprocessedInput
# ----------------------------------------------------------------
# Web Scrapping Movie code and movie name
# -----------------------------------------------------------------
def webScrapping(userInput):
safe_string = urllib.parse.quote_plus(userInput)
# Request Page Source for URL
url = f"https://www.imdb.com/find?q={safe_string}&ref_=nv_sr_sm"
page = requests.get(url)
# Displaying Page Source Code
soup = BeautifulSoup(page.content, "html.parser")
scraped_movies = soup.find('td', class_="result_text")
# names = []
# for scraped_names in scraped_movies:
# scraped_names = scraped_names.get_text().replace('\n', "")
# names.append(scraped_remark)
# scraped_movies
if(scraped_movies == None):
return [],''
else:
movieCode = scraped_movies.find('a')['href'].split('/')[2]
url1 = f"https://www.imdb.com/title/{movieCode}/reviews/"
pages = requests.get(url1)
codes=[]
for movieCode in scraped_movies:
scraped_remark = scraped_remark.get_text().replace('\n', "")
codes.append(movieCode)
# ---------------------------------------------------------
soup = BeautifulSoup(pages.content, "html.parser")
scraped_remarks = soup.find_all('div', class_="text show-more__control")
reviews = []
for scraped_remark in scraped_remarks:
scraped_remark = scraped_remark.get_text().replace('\n', "")
reviews.append(scraped_remark)
return reviews, codes
# ----------------------------------------------------------------
# Showing Output
# -----------------------------------------------------------------
def ScrappReviews(userInput):
scrapedMovieReviews, codes = webScrapping(userInput)
if(scrapedMovieReviews == []):
return "No movie Found please try again.",''
afterVec = vectorization(scrapedMovieReviews)
result1 = trainedModel.predict(afterVec)
return result1,codes
def PredictPercentage(userInput):
result1, codes = (ScrappReviews(userInput))
total = dict(Counter(result1))
if (codes== ''):
return "No movie Found please try again.","0", []
else:
if (total.get('positive') == 0 or total.get('positive') == None):
{
total.update({'positive': 1})
}
elif(total.get('negative') == 0 or total.get('negative') == None):
{
total.update({'negative': 1})
}
totalPos = total['positive']
totalNeg = total['negative']
totalSum = totalPos + totalNeg
if(totalPos >= totalNeg):
overall = "Positive"
totalPercentage = round((totalPos/totalSum)*100)
else:
overall = "Negative"
totalPercentage = round((totalNeg/totalSum)*100)
return overall, totalPercentage, codes
def getMessage(userInput):
movies = []
overall, totalPercentage, codes = PredictPercentage(userInput)
if (codes== []):
return f"'{overall} '", f"\n Percentage: {totalPercentage} %", movies
else:
cursor.execute(
"Select Movies.MovieID,Movies.MovieName,Prediction.PredictionResult,Prediction.Percentage,Prediction.PredictedDate from Movies Join Prediction on Movies.MovieID=Prediction.MovieID WHERE DATEDIFF(day,GETDATE(),PredictedDate) <= 30 AND Movies.MovieName like '%'+?+'%'", [
userInput]
)
for row in cursor.fetchall():
movies.append({
"MovieID": row[0], "MovieName": row[1],
"PredictionResult": row[2], "Percentage": row[3], "PredictedDate": row[4]})
return f"Mostly Comments are: '{overall} '", f"\n Percentage: {totalPercentage} %", movies
cursor.execute("Update Prediction Set Prediction.PredictionResult=?,Prediction.Percentage=?,Prediction.PredictedDate=GetDate() from Prediction where DATEDIFF(day,GETDATE(),PredictedDate) >= 30 ", (overall, totalPercentage))
cursor.execute(
"Select Movies.MovieID,Movies.MovieName,Prediction.PredictionResult,Prediction.Percentage,Prediction.PredictedDate from Movies Join Prediction on Movies.MovieID=Prediction.MovieID WHERE DATEDIFF(day,GETDATE(),PredictedDate) <= 30 AND Movies.MovieName like '%'+?+'%'", [
userInput]
)
for row in cursor.fetchall():
movies.append({
"MovieID": row[0], "MovieName": row[1],
"PredictionResult": row[2], "Percentage": row[3], "PredictedDate": row[4]})
return f"Mostly Comments are: '{overall} '", f"\n Percentage: {totalPercentage} %", movies
cursor.execute(
"Insert into dbo.movies(MovieID,MovieName) Values(?,?)", codes, userInput)
cursor.execute(
"Insert into dbo.prediction(MovieID,PredictionResult,Percentage,PredictedDate) values(?,?,?,GetDate())", codes, overall, totalPercentage)
conn.commit()
for row in cursor.fetchall():
movies.append({
"MovieID": row[0], "MovieName": row[1],
"PredictionResult": row[2], "Percentage": row[3], "PredictedDate": row[4]})
return f"Mostly Comments are: '{overall} '", f"\n Percentage: {totalPercentage} %", movies
# Select Movies.MovieID,Movies.MovieName,Prediction.PredictionResult,Prediction.percentage ,Prediction.PredictedDate from Movies Join Prediction on Movies.MovieID=Prediction.MovieID
return f"Mostly Comments are: '{overall} '", f"\n Percentage: {totalPercentage} %", movies