|
import re |
|
import emoji |
|
import joblib |
|
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer |
|
from sklearn.neural_network import MLPClassifier |
|
from sklearn.preprocessing import LabelEncoder |
|
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix, f1_score |
|
import gradio as gr |
|
|
|
|
|
|
|
cv = joblib.load('tfidf_vectorizer.pkl') |
|
|
|
|
|
mlp_label = joblib.load('mlpLabel.pkl') |
|
|
|
|
|
mlp_aspect_label = joblib.load('mlpAspectLabel.pkl') |
|
|
|
|
|
def remove_html(text) : |
|
patt_html = r"<.*?>" |
|
text = re.sub(patt_html, "", text) |
|
return text |
|
|
|
def remove_url(text): |
|
patt_url = r"https?://\S+|www\.\S+" |
|
text = re.sub(patt_url, "", text) |
|
return text |
|
|
|
def emoji_to_text(text) : |
|
res_str = "" |
|
for ch in text : |
|
if emoji.is_emoji(ch) : |
|
res_str += f" {emoji.demojize(ch)} " |
|
|
|
else : |
|
res_str += ch |
|
return res_str |
|
|
|
def clean_review_text(text): |
|
|
|
|
|
text = remove_html(text) |
|
|
|
|
|
text = remove_url(text) |
|
|
|
|
|
text = emoji_to_text(text) |
|
|
|
|
|
text = text.lower() |
|
|
|
|
|
return text |
|
|
|
|
|
|
|
label = ['negative', 'neutral', 'positive'] |
|
aspect_label = ['Card Decks and Challenges', 'Card Play and Board Games', |
|
'Fun and Coin Collecting', 'Game Scores and Features', |
|
'Game Updates and User Desires', 'Gameplay and App Experience', |
|
'Gameplay and Trading', 'Gameplay and User Experience', |
|
'Property and Land Management', 'Subway Adventures'] |
|
|
|
|
|
def return_label_aspect(Review): |
|
review_vec = cv.transform([clean_review_text(Review)]) |
|
pred_label = mlp_label.predict_proba(review_vec)[0] |
|
pred_aspect = mlp_aspect_label.predict_proba(review_vec)[0] |
|
|
|
pred_label = { label[i]: round(pred_label[i], 2) for i in range(3) } |
|
pred_aspect = { aspect_label[i]: round(pred_aspect[i], 2) for i in range(10) } |
|
return pred_label, pred_aspect |
|
|
|
|
|
|
|
iface = gr.Interface(fn=return_label_aspect, inputs="text", outputs=[gr.Label(), gr.Label()]) |
|
iface.launch(inline = False) |