#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 5.3
#  in conjunction with Tcl version 8.6
#    Jun 04, 2020 07:48:22 PM IST  platform: Windows NT

import sys
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.svm import LinearSVC
import re
import pickle
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

import sentiment_analysis_tool_support

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    top = Toplevel1 (root)
    sentiment_analysis_tool_support.init(root, top)
    root.mainloop()

w = None
def create_Toplevel1(rt, *args, **kwargs):
    '''Starting point when module is imported by another module.
       Correct form of call: 'create_Toplevel1(root, *args, **kwargs)' .'''
    global w, w_win, root
    #rt = root
    root = rt
    w = tk.Toplevel (root)
    top = Toplevel1 (w)
    sentiment_analysis_tool_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_Toplevel1():
    global w
    w.destroy()
    w = None

class Toplevel1:
    def say_hi(self):
        a=self.Text1.get("1.0",'end-1c')
        r = []
        r=a.splitlines()
        REPLACE_NO_SPACE = re.compile("[.;:!\'?,\"()\[\]]")
        REPLACE_WITH_SPACE = re.compile("(<br\s*/><br\s*/>)|(\-)|(\/)")
        review_set_clean = [REPLACE_NO_SPACE.sub("", line.lower()) for line in r]
        review_set_clean = [REPLACE_WITH_SPACE.sub(" ", line) for line in review_set_clean]
        # stop_words = ['in', 'of', 'at', 'a', 'the']
        # filename = 'finalized_classifier.sav'
        # loaded_classifier = pickle.load(open(filename, 'rb'))
        # loaded_vec = CountVectorizer(binary=True, ngram_range=(1, 3), stop_words=stop_words,vocabulary=pickle.load(open("feature.pkl", "rb")))
        reviewset_vectorized = loaded_vec.transform(review_set_clean)
        l=loaded_classifier.predict(reviewset_vectorized)
        print(l)
        k=0
        n=0
        for i in l:
            n=n+1
            if(i==1):
                self.Text2.insert('end',"review %s is positive\n" %n);
            else:
                self.Text2.insert('end',"review %s is negative\n" %n);
            k=k+i
        k=k/len(l)
        print("\nsentiment score is:",k)
        self.TLabel1.configure(text=k)
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'
        font11 = "-family {Segoe UI} -size 10"
        font9 = "-family {Segoe UI} -size 16"
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=
            [('selected', _compcolor), ('active',_ana2color)])

        top.geometry("813x542+695+155")
        top.minsize(148, 1)
        top.maxsize(1924, 1055)
        top.resizable(1, 1)
        top.title("Sentiment analysis tool")
        top.configure(background="#d9d9d9")
        top.configure(highlightbackground="#d9d9d9")
        top.configure(highlightcolor="black")

        self.Button1 = tk.Button(top)
        self.Button1.place(relx=0.418, rely=0.646, height=33, width=142)
        self.Button1.configure(activebackground="#ececec")
        self.Button1.configure(activeforeground="#000000")
        self.Button1.configure(background="#d9d9d9")
        self.Button1.configure(disabledforeground="#a3a3a3")
        self.Button1.configure(foreground="#000000")
        self.Button1.configure(highlightbackground="#d9d9d9")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(pady="0")
        self.Button1.configure(text='''Compute sentiment''')
        self.Button1["command"]=self.say_hi

        self.TLabel1 = ttk.Label(top)
        self.TLabel1.place(relx=0.492, rely=0.775, height=29, width=61)
        self.TLabel1.configure(background="#d9d9d9")
        self.TLabel1.configure(foreground="#000000")
        self.TLabel1.configure(font="TkDefaultFont")
        self.TLabel1.configure(relief="flat")
        self.TLabel1.configure(anchor='w')
        self.TLabel1.configure(justify='left')
        self.TLabel1.configure(text='''Tlabel''')

        self.Text1 = tk.Text(top)
        self.Text1.place(relx=0.039, rely=0.203, relheight=0.387, relwidth=0.422)

        self.Text1.configure(background="white")
        self.Text1.configure(font="TkTextFont")
        self.Text1.configure(foreground="black")
        self.Text1.configure(highlightbackground="#d9d9d9")
        self.Text1.configure(highlightcolor="black")
        self.Text1.configure(insertbackground="black")
        self.Text1.configure(selectbackground="#c4c4c4")
        self.Text1.configure(selectforeground="black")
        self.Text1.configure(wrap="word")

        self.Text2 = tk.Text(top)
        self.Text2.place(relx=0.541, rely=0.203, relheight=0.387, relwidth=0.389)

        self.Text2.configure(background="white")
        self.Text2.configure(font="TkTextFont")
        self.Text2.configure(foreground="black")
        self.Text2.configure(highlightbackground="#d9d9d9")
        self.Text2.configure(highlightcolor="black")
        self.Text2.configure(insertbackground="black")
        self.Text2.configure(selectbackground="#c4c4c4")
        self.Text2.configure(selectforeground="black")
        self.Text2.configure(wrap="word")

        self.TLabel2 = ttk.Label(top)
        self.TLabel2.place(relx=0.308, rely=0.018, height=49, width=330)
        self.TLabel2.configure(background="#d9d9d9")
        self.TLabel2.configure(foreground="#000000")
        self.TLabel2.configure(font=font9)
        self.TLabel2.configure(relief="flat")
        self.TLabel2.configure(anchor='w')
        self.TLabel2.configure(justify='left')
        self.TLabel2.configure(text='''Sentiment Analysis Tool''')

        self.TLabel3 = ttk.Label(top)
        self.TLabel3.place(relx=0.039, rely=0.129, height=29, width=348)
        self.TLabel3.configure(background="#d9d9d9")
        self.TLabel3.configure(foreground="#000000")
        self.TLabel3.configure(font=font11)
        self.TLabel3.configure(relief="flat")
        self.TLabel3.configure(anchor='w')
        self.TLabel3.configure(justify='left')
        self.TLabel3.configure(text='''Enter reviewset(seperate review by newline):''')

        self.TLabel4 = ttk.Label(top)
        self.TLabel4.place(relx=0.541, rely=0.129, height=24, width=246)
        self.TLabel4.configure(background="#d9d9d9")
        self.TLabel4.configure(foreground="#000000")
        self.TLabel4.configure(font="TkDefaultFont")
        self.TLabel4.configure(relief="flat")
        self.TLabel4.configure(anchor='w')
        self.TLabel4.configure(justify='left')
        self.TLabel4.configure(text='''Computed sentiment of each review:''')

        self.TLabel5 = ttk.Label(top)
        self.TLabel5.place(relx=0.271, rely=0.775, height=24, width=160)
        self.TLabel5.configure(background="#d9d9d9")
        self.TLabel5.configure(foreground="#000000")
        self.TLabel5.configure(font="TkDefaultFont")
        self.TLabel5.configure(relief="flat")
        self.TLabel5.configure(anchor='w')
        self.TLabel5.configure(justify='left')
        self.TLabel5.configure(text='''overall sentiment score:''')

        self.TLabel6 = ttk.Label(top)
        self.TLabel6.place(relx=0.209, rely=0.83, height=44, width=434)
        self.TLabel6.configure(background="#d9d9d9")
        self.TLabel6.configure(foreground="#000000")
        self.TLabel6.configure(font="TkDefaultFont")
        self.TLabel6.configure(relief="flat")
        self.TLabel6.configure(anchor='w')
        self.TLabel6.configure(justify='left')
        self.TLabel6.configure(text='''(value  closer to 1 corresponds to highly positive sentiment
and value closer to 0 corresponds to highly negative sentiment)''')

if __name__ == '__main__':
    stop_words = ['in', 'of', 'at', 'a', 'the']
    filename = 'finalized_classifier.sav'
    loaded_classifier = pickle.load(open(filename, 'rb'))
    loaded_vec = CountVectorizer(binary=True, ngram_range=(1, 3), stop_words=stop_words,vocabulary=pickle.load(open("feature.pkl", "rb")))
    vp_start_gui()





