-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (66 loc) · 2.25 KB
/
main.py
File metadata and controls
77 lines (66 loc) · 2.25 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
import pandas as pd
import numpy as np
import nltk
import pickle
import sklearn
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.corpus import stopwords
from nltk.tokenize import *
from nltk.stem import *
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import re
import streamlit as st
import boto3
from io import BytesIO
from st_files_connection import FilesConnection
conn = st.connection('s3', type=FilesConnection)
s3 = boto3.resource('s3')
with BytesIO() as data:
s3.Bucket("productreviewsentiment").download_fileobj("sentiment.pkl", data)
data.seek(0) # move back to the beginning after writing
model = pickle.load(data)
def process_comment(comment):
# remove stock market tickers like $GE
comment = re.sub(r'\$\w*', '', comment)
# remove filler words like "\\n"
comment = re.sub(r'[\n]', '', comment)
# remove punctuation so that "!" does not get assumed by the model to be a positive review
comment = re.sub(r'[?!]', '', comment)
# remove old style text "RT"
comment = re.sub(r'^RT[\s]+', '', comment)
# remove hyperlinks
comment = re.sub(r'https?:\/\/.*[\r\n]*', '', comment)
# remove hashtags, only removing the hash # sign from the word
comment = re.sub(r'#', '', comment)
return comment
def tokenizer_porter(text):
porter = PorterStemmer()
return [porter.stem(word) for word in text.split()]
def stop():
stop = stopwords.words('english')
return stop
def app_prediction(model, text):
answer = int(model.predict(text)[0])
if answer == 0:
label = f"This review is negative :anguished:"
if answer == 1:
label = f"This review is positive :smiley:"
return label
# App Interface
st.title("Product Review Positivity Predictor (positive or negative)")
# Input
text = st.text_input('Enter full review', 'Copy and paste the review here')
text = list(text)
# load model
with open("final.pkl", 'rb') as f:
model = pickle.load(f)
# prediction
# design user interface
if st.button("Find Out"):
x = process_comment(text)
prediction = app_prediction(model, x)
st.subheader("Prediction based on your inputs:")
st.write(prediction)