MENU

wxpython小试牛刀

June 23, 2019 • python阅读设置

GUI初次尝试

import wx
import os
import nltk.tokenize as tk
import re
import nltk
nltk.download('stopwords')
nltk.download('punkt')
class SiteLog(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='自动翻译---BY Choin', size=(1080, 720))

        self.SelBtn = wx.Button(self, label='选择中文文件', pos=(305, 5), size=(80, 25))
        self.SelBtn2 = wx.Button(self, label='选择英文文件', pos=(305, 35), size=(80, 25))

        self.SelBtn.Bind(wx.EVT_BUTTON, self.OnOpenFile)
        self.SelBtn2.Bind(wx.EVT_BUTTON, self.OnOpenFile2)
        self.OkBtn = wx.Button(self, label='开始翻译', pos=(405, 25), size=(80, 25))
        self.OkBtn.SetBackgroundColour("#FFCC66")
        self.OkBtn.Bind(wx.EVT_BUTTON, self.chulifile)

        self.FileName = wx.TextCtrl(self, pos=(5, 5), size=(250, 25))#中文
        self.FileName2 = wx.TextCtrl(self, pos=(5, 35), size=(250, 25))#英文
        self.FileContent = wx.TextCtrl(self, pos=(5, 75), size=(1000, 600), style=(wx.TE_MULTILINE))

    def OnOpenFile(self, event):
        wildcard = 'All files(*.*)|*.*'
        dialog = wx.FileDialog(None, 'select', os.getcwd(), '', wildcard, style=wx.FD_OPEN | wx.FD_CHANGE_DIR)
        if dialog.ShowModal() == wx.ID_OK:
            self.FileName.SetValue(dialog.GetPath())
            dialog.Destroy

    def OnOpenFile2(self, event):
        wildcard = 'All files(*.*)|*.*'
        dialog = wx.FileDialog(None, 'select', os.getcwd(), '', wildcard, style=wx.FD_OPEN | wx.FD_CHANGE_DIR)
        if dialog.ShowModal() == wx.ID_OK:
            self.FileName2.SetValue(dialog.GetPath())
            dialog.Destroy

    def ReadFile(self,filename):
        file = open(filename,'r',encoding='utf-8')
        self.FileContent.SetValue(file.read())
        print(file.read())
        file.close()



    def parse_eng(self,filename):
        """
        解析英文
        :param filename: 传入文件名字
        :return:返回一个英文句子的列表
        """
        en_data = open(filename, 'r', encoding='utf-8').read()
        tokens = tk.sent_tokenize(en_data)
        return tokens

    def get_ch(self,para):
        # 版本为python3,如果为python2需要在字符串前面加上u

        para = re.sub('([。!?\?])([^”’])', r"\1\n\2", para)  # 单字符断句符
        para = re.sub('(\.{6})([^”’])', r"\1\n\2", para)  # 英文省略号
        para = re.sub('(\…{2})([^”’])', r"\1\n\2", para)  # 中文省略号
        para = re.sub('([。!?\?][”’])([^,。!?\?])', r'\1\n\2', para)
        # 如果双引号前有终止符,那么双引号才是句子的终点,把分句符\n放到双引号后,注意前面的几句都小心保留了双引号
        para = para.rstrip()  # 段尾如果有多余的\n就去掉它
        # 很多规则中会考虑分号;,但是这里我把它忽略不计,破折号、英文双引号等同样忽略,需要的再做些简单调整即可。
        return para.split("\n")

    def parse_ch(self,filename):
        """
        解析中文分句
        :param filename: 传入文件名
        :return: 一个中文分句列表
        """
        cn_data = open(filename, 'r', encoding='utf-8').read().strip()
        data = self.get_ch(cn_data)
        c = []
        for i in data:
            if i == "":
                continue
            else:
                c.append(i)
        return c

    def max_len(self,a, b):
        """
        比较两个列表大小
        :param a:
        :param b:
        :return:
        """
        if len(a) > len(b):
            return len(a)
        else:
            return len(b)

    def chulifile(self,event):
        filename_cn = self.FileName.GetValue()
        filename_en = self.FileName2.GetValue()
        print(filename_cn,filename_en)
        list1 = self.parse_eng(filename_en)  # 是一个列表
        list2 = self.parse_ch(filename_cn)
        max_length = self.max_len(list1, list2)
        count = 1
        fakefile = open('fuck.txt','a',encoding='utf-8')
        for i in range(max_length):
            try:
                fakefile.write("===========第%s句===========\n" % count)
                fakefile.write("%s\n%s\n"%(list2[i],list1[i]))
                count += 1
            except Exception:
                continue
        fakefile.close()
        self.ReadFile('fuck.txt')



if __name__ == '__main__':
    app = wx.App()
    SiteFrame = SiteLog()
    SiteFrame.Show()
    app.MainLoop()