Comment_Analysis / comment_analyzer.py
MAli7319's picture
Update comment_analyzer.py
f354ce2
raw
history blame
3.43 kB
import gradio as gr
import pandas as pd
import numpy as np
from langdetect import detect
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from sklearn.model_selection import train_test_split
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
data = pd.read_csv("modeled_data.csv")
analyzer = SentimentIntensityAnalyzer()
def sample_model(df, regressor):
X = df.drop("rate",axis=1)
y = df["rate"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=1)
model = regressor
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
return model
def calculate_sentiments(comment, model):
negative_score = analyzer.polarity_scores(comment)["neg"]
neutral_score = analyzer.polarity_scores(comment)["neu"]
positive_score = analyzer.polarity_scores(comment)["pos"]
compound_score = analyzer.polarity_scores(comment)["compound"]
rate_pred = model.predict([[negative_score, neutral_score, positive_score, compound_score]])
return round(negative_score,2), round(neutral_score,2), round(positive_score,2), round(compound_score,2), round(rate_pred[0],2)
def take_input(comment):
cons_tuned_svr = sample_model(data, SVR(C=3, kernel="rbf", tol=0.001))
return calculate_sentiments(comment, cons_tuned_svr)
with gr.Blocks() as demo:
gr.Markdown("# AIN311 Project P05 - MOOC Recommendation")
gr.Markdown("## Generating a Rating from User Comment")
with gr.Column():
gr.Markdown("""
##### Thanks for your interest and taking your time.
##### Tell us about your personal experience enrolling in this course. Was it the right match for you?
""")
input_comment = gr.Textbox(placeholder="Write your comment here...", show_label = False, lines=2)
button = gr.Button("What is the Rating I Have Given? Click me to Learn", variant="secondary").style(full_width=True)
with gr.Row():
with gr.Column():
gr.Markdown("#### Generated Rating from Your Comment")
rating = gr.Number().style(show_label=False)
with gr.Column():
gr.Markdown("#### Sentiment Scores of Your Comment")
with gr.Row():
negscore = gr.Number(label="Negativity Score")
neuscore = gr.Number(label="Neutrality Score")
posscore = gr.Number(label="Positivity Score")
compscore = gr.Number(label="Compound Score")
gr.Examples(
[["Totally enjoyed this course. Learnt whole new dimension of data science and its attributes"],
["The bad part of the course is that it doesn't get a person into the logics of some things right away or even doesn't get into them at all."],
["Not for the beginners very difficult to understand i gain nothing from this course i watch videos again and again but nothing fits in my mind"]],
[input_comment],
[[negscore, neuscore, posscore, compscore, rating]],
fn=take_input
)
button.click(fn=take_input, inputs=input_comment, outputs=[negscore, neuscore, posscore, compscore, rating])
demo.launch()