# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled2.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

import stanza
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog,QColorDialog
from PyQt5.QtGui import QTextCursor

#stanza.download('ru')

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1210, 885)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(10, 0, 681, 821))
        self.textEdit.setObjectName("textEdit")
        self.zapysk = QtWidgets.QPushButton(self.centralwidget)
        self.zapysk.setGeometry(QtCore.QRect(890, 800, 131, 21))
        self.zapysk.setObjectName("zapysk")

        self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(740, 100, 70, 17))
        self.checkBox.setObjectName("checkBox")

        self.checkBox_2 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_2.setGeometry(QtCore.QRect(740, 130, 111, 21))
        self.checkBox_2.setObjectName("checkBox_2")

        self.checkBox_3 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_3.setGeometry(QtCore.QRect(740, 160, 70, 17))
        self.checkBox_3.setObjectName("checkBox_3")

        self.checkBox_4 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_4.setGeometry(QtCore.QRect(740, 190, 151, 17))
        self.checkBox_4.setObjectName("checkBox_4")

        self.checkBox_5 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_5.setGeometry(QtCore.QRect(740, 220, 181, 21))
        self.checkBox_5.setObjectName("checkBox_5")

        self.checkBox_6 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_6.setGeometry(QtCore.QRect(740, 250, 91, 17))
        self.checkBox_6.setObjectName("checkBox_6")

        self.checkBox_7 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_7.setGeometry(QtCore.QRect(740, 280, 91, 17))
        self.checkBox_7.setObjectName("checkBox_7")

        self.checkBox_8 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_8.setGeometry(QtCore.QRect(740, 310, 111, 17))
        self.checkBox_8.setObjectName("checkBox_8")

        self.checkBox_9 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_9.setGeometry(QtCore.QRect(739, 340, 101, 20))
        self.checkBox_9.setObjectName("checkBox_9")

        self.checkBox_10 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_10.setGeometry(QtCore.QRect(740, 370, 81, 21))
        self.checkBox_10.setObjectName("checkBox_10")

        self.checkBox_11 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_11.setGeometry(QtCore.QRect(740, 400, 101, 20))
        self.checkBox_11.setObjectName("checkBox_11")

        self.checkBox_12 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_12.setGeometry(QtCore.QRect(740, 430, 111, 17))
        self.checkBox_12.setObjectName("checkBox_12")

        self.checkBox_13 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_13.setGeometry(QtCore.QRect(740, 460, 141, 16))
        self.checkBox_13.setObjectName("checkBox_13")

        self.checkBox_14 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_14.setGeometry(QtCore.QRect(740, 490, 141, 16))
        self.checkBox_14.setObjectName("checkBox_14")

        self.checkBox_15 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_15.setGeometry(QtCore.QRect(740, 520, 70, 17))
        self.checkBox_15.setObjectName("checkBox_15")

        self.checkBox_16 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_16.setGeometry(QtCore.QRect(740, 550, 70, 17))
        self.checkBox_16.setObjectName("checkBox_16")

        self.checkBox_17 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_17.setGeometry(QtCore.QRect(740, 580, 70, 17))
        self.checkBox_17.setObjectName("checkBox_17")

        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(780, 10, 271, 81))
        self.textBrowser.setObjectName("textBrowser")

        self.ColorButton = QtWidgets.QPushButton(self.centralwidget)
        self.ColorButton.setGeometry(QtCore.QRect(740, 640, 81, 21))
        self.ColorButton.setObjectName("ColorButton")

        MainWindow.setCentralWidget(self.centralwidget)

        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1210, 21))
        self.menubar.setObjectName("menubar")

        self.menu = QtWidgets.QMenu(self.menubar)
        self.menu.setObjectName("menu")
        MainWindow.setMenuBar(self.menubar)

        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

       
        
        self.menubar.addAction(self.menu.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.menu.addAction('Открыть .txt',self.action_cliked)
        
        self.ColorButton.clicked.connect(self.changecolor)
       
        self.zapysk.clicked.connect(self.searchText)
        
    # Открытие файла .txt
    def action_cliked(self):

        
        
        
        fname = QFileDialog.getOpenFileName(self.menu,'Open File', ".", "Files (*.txt)")[0]
        try:
            f = open(fname,'r')
            with f:
                data = f.read()
                self.textEdit.setTextColor(QtGui.QColor("black"))
                self.textEdit.setText(data)
        except FileNotFoundError:
            print("No such file")
    # изменение цвета
    def changecolor(self):
      global color
      color= QColorDialog.getColor(QtGui.QColor("red"))

    
    
        
    def searchText(self):
    
         self.textEdit.setTextColor(color)
        
          
            
        
         spisok = []
         # обработать ошибку если цвет ввели не тот
         # 
         # 
        
         if self.checkBox.isChecked():
            spisok.append('ADP')
         if self.checkBox_2.isChecked():
            spisok.append('ADJ')
         if self.checkBox_3.isChecked():
            spisok.append('ADV')
         if self.checkBox_4.isChecked():
            spisok.append('AUX')
         if self.checkBox_5.isChecked():
            spisok.append('CCONJ')
         if self.checkBox_6.isChecked():
            spisok.append('DET')
         if self.checkBox_7.isChecked():
            spisok.append('INTJ')
         if self.checkBox_8.isChecked():
            spisok.append('NOUN')
         if self.checkBox_9.isChecked():
            spisok.append('NUM')
         if self.checkBox_10.isChecked():
            spisok.append('PART')
         if self.checkBox_11.isChecked():
            spisok.append('PRON')   
         if self.checkBox_12.isChecked():
            spisok.append('PROPN')
         if self.checkBox_13.isChecked():
            spisok.append('PUNCT')
         if self.checkBox_14.isChecked():
            spisok.append('SCONJ')
         if self.checkBox_15.isChecked():
            spisok.append('SYM')
         if self.checkBox_16.isChecked():
            spisok.append('VERB')
         if self.checkBox_17.isChecked():
            spisok.append('X')



            #stanza
         
         nlp = stanza.Pipeline(lang ='ru',processors='tokenize,pos')
         text2=str(self.textEdit.toPlainText())
         doc = nlp(text2)
         fmt =QtGui.QTextCharFormat()
         fmt.setUnderlineColor(color)
         spisokid=[]
         i=0
         self.textEdit.setTextColor(QtGui.QColor("black"))
         self.textEdit.setText(text2)

         self.textEdit.moveCursor(QTextCursor.Start)
         cursor=self.textEdit.textCursor()
         isentence=0;
         for sentence in doc.sentences:
            isentence+=1
            print('*******************Sentence: '+str(isentence)+'*********************')
            spisok2=[]
            spisok2.extend(spisok)
            spisokid2=[]
            for word in sentence.words:
                i+=1
                print(i)
                print(word.text +':'+word.upos)
                if word.upos in spisok2:
                    print("dada")
                    spisok2.remove(word.upos)
                    spisokid2.append(word.text)#word.id
                else:
                    if word.upos != 'PUNCT':
                        spisok2=[]
                        spisok2.extend(spisok)
                        spisokid2=[]
                    
                        if word.upos in spisok2:
                            print("dada")
                            spisok2.remove(word.upos)
                            spisokid2.append(word.text)#word.id

                if not spisok2:
                    spisokid.extend(spisokid2)
                    spisok2.extend(spisok)
                        

            if not spisok2:
                print("1")
                spisokid.extend(spisokid2)
                

            print(spisok2)
            
         print(spisokid)
         for seek in spisokid:
                    self.textEdit.find(seek)
                    self.textEdit.moveCursor(QTextCursor.WordLeft)
                    
                    cursor=self.textEdit.textCursor()
                    cursor.select(cursor.WordUnderCursor)
                    print(cursor.selectedText())
                    self.textEdit.setTextCursor(cursor)
                    self.textEdit.setTextColor(color)
                    #self.textEdit.setTextCursor(cursor)
                    cursor.clearSelection()
             
         self.textEdit.setTextCursor(cursor)
         self.textEdit.setTextColor(color)
         cursor.clearSelection()

         #self.textEdit.insertPlainText("Hellow")
         self.textEdit.setTextColor(QtGui.QColor("black"))
         
        # self.textEdit.insertPlainText(str(spisok))
   





    
    
    

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.zapysk.setText(_translate("MainWindow", "Начать поиск"))
        self.checkBox.setText(_translate("MainWindow", "Предлог"))
        self.checkBox_2.setText(_translate("MainWindow", "Прилагательное"))
        self.checkBox_3.setText(_translate("MainWindow", "Наречие"))
        self.checkBox_4.setText(_translate("MainWindow", "Вспомогательный глагол"))
        self.checkBox_5.setText(_translate("MainWindow", "Координирующее соединение"))
        self.checkBox_6.setText(_translate("MainWindow", "Детерминант"))
        self.checkBox_7.setText(_translate("MainWindow", "Междометие"))
        self.checkBox_8.setText(_translate("MainWindow", "Существительное"))
        self.checkBox_9.setText(_translate("MainWindow", "Число"))
        self.checkBox_10.setText(_translate("MainWindow", "Частица"))
        self.checkBox_11.setText(_translate("MainWindow", "Местоимение"))
        self.checkBox_12.setText(_translate("MainWindow", "Имя собственное"))
        self.checkBox_13.setText(_translate("MainWindow", "Пунтакционный знак"))
        self.checkBox_14.setText(_translate("MainWindow", "Подчиняющийся союз"))
        self.checkBox_15.setText(_translate("MainWindow", "Символ"))
        self.checkBox_16.setText(_translate("MainWindow", "Глагол"))
        self.checkBox_17.setText(_translate("MainWindow", "Другие"))
        self.textBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Настройки(что искать) </p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Выбирите, что вы хотите найти.</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Выбирите цвет.</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Нажмите на кнопку &quot;Начать поиск&quot;</p></body></html>"))
        self.ColorButton.setText(_translate("MainWindow", "Цвет"))
        self.menu.setTitle(_translate("MainWindow", "Файл"))
       # self.action.setText(_translate("MainWindow", "Открыть .txt"))


if __name__ == "__main__":
    import sys
    
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
