File size: 2,287 Bytes
3ca505b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
548542d
3ca505b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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


# load the TF-IDF vectorizer to a file
cv = joblib.load('tfidf_vectorizer.pkl')

# load the MLP classifier to a file
mlp_label = joblib.load('mlpLabel.pkl')

# load the MLP Aspect classifier to a file
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)} "
            # print(ch, emoji.demojize(ch))
        else :
            res_str += ch
    return res_str

def clean_review_text(text):
    
    # remove HTML Tags
    text = remove_html(text)
    
    # remove url to call function remover_url
    text = remove_url(text) 
    
    # convert text emoji into text
    text = emoji_to_text(text)
    
    # convert all text into lower case
    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)