Example #1
0
    def __init__(self, file_addr: str):
        """
        :param file_addr: 需要被读入的地址
        """
        self.exhausted = False  # 因为new里面有个迭代器可以调用,当一轮跑完时重新跑就返回None
        self.addr = file_addr

        if config.read_code != "":
            self.f = open(file_addr, 'r', encoding=config.read_code)
        else:
            tool.ready("识别格式")
            # 检验格式
            f3 = open(file=file_addr, mode='rb')  # 以二进制模式读取文件
            data = None
            if config.auto_detect_bytes == 0:
                data = f3.read()  # 获取文件内容
            else:
                data = f3.read(config.auto_detect_bytes)
            f3.close()  # 关闭文件
            result = chardet.detect(data)
            code_type = result["encoding"]
            print("  格式为:", code_type)
            print("  置信度:", result["confidence"])
            if code_type == "GB2312":
                # 很奇怪的是识别为GB2312但是并无法完全解码。。。。可能置信度不足100%
                code_type = "GBK"
                print("  GB2312默认转为GBK")
            self.f = open(file_addr, 'r', encoding=code_type)
            tool.done()
        tool.done()
Example #2
0
 def reform(self):
     """进行重整
     """
     p = self.head
     if p is None:
         p = Text("空文本")
     tool.ready("对各内容节点进行格式化并归并为连续结构")
     while p is not None:
         if type(p) is Chapter:
             p.reform()  # todo:这个情况是不可能的,应该抛个错误
         elif type(p) is Volume:
             p.reform()
             self.child.append(p)
         elif type(p) is Text:
             p.reform()
             self.child.append(p)
         elif type(p) is Enter:
             if config.delete_enter:
                 p.delete()  # 重整空行
             else:
                 p.reform()
                 self.child.append(p)
         p = p.next
     tool.done()
     self.cv_sort()
     self.delete_reduplicate()
Example #3
0
    def cv_sort(self):
        """
递归排序卷和章
        """

        # 排序必须是稳定的,不然卷前语和书前语顺序可能改变
        def key(a):
            # 专门为内部排序写的,只考虑可能的情况.所有非段卷只可能在最前面,保持不变
            if type(a) is Text or type(a) is Enter:
                return 0
            else:
                return a.num

        tool.ready("章卷排序")
        for c in self.child:
            if type(c) is Volume:
                # 每一卷进行卷内排序
                c.child.sort(key=key)
        # python3 删除了自定义的比较函数,所以只能这样写...
        self.child.sort(key=key)
        tool.done()
Example #4
0
def main():
    # 各个阶段也加入打表信息,并与debug分离
    # 读入
    tool.ready("正在读入文件")
    addr = tool.get_file()
    if addr == "":
        return
    con = contents.Contents(addr)
    # 处理
    tool.ready("文本结构展示")
    tool.analyse_list(con.head)
    tool.done()
    con.reform()
    tool.ready("重整为单一文本")
    s = con.output()
    tool.done()
    # 输出
    tool.ready("导出")
    (filepath, temp_filename) = os.path.split(addr)
    (filename, extension) = os.path.splitext(temp_filename)
    if not config.over:
        addr = filepath + "/" + filename + "_修改后" + extension
    with open(addr, "w", encoding=config.write_code) as file:
        file.write(s)
    tool.done()
Example #5
0
    def delete_reduplicate(self):
        """
递归删除child的重复章卷
        """
        tool.ready("删除重复章卷")
        # 删除重复卷
        new_child = []
        last_num = -1  # 上一个章节号
        for c in self.child:
            if type(c) is Text:
                new_child.append(c)
            if type(c) is Enter:
                new_child.append(c)
            if type(c) is Volume:
                if c.num != last_num:
                    new_child.append(c)
                    last_num = c.num
                    # todo: 优化重复章删除,优先删除空章,而非删除除第一个的
                    # 删除重复章
                    new_child2 = []
                    last_num_2 = -1  # 上一个章节号
                    for cc in c.child:
                        if type(cc) is Text:
                            new_child2.append(cc)
                        if type(cc) is Enter:
                            new_child2.append(cc)
                        if type(cc) is Chapter:
                            if cc.num != last_num_2:
                                new_child2.append(cc)
                                last_num_2 = cc.num
                            else:
                                if config.debug:
                                    print("第", c.num, "卷 第", cc.num, "章重复已被删除")
                    c.child = new_child2
                else:
                    if config.debug:
                        print("第", c.num, "卷重复已被删除")
        self.child = new_child
        tool.done()
Example #6
0
    def __init__(self, addr: str):
        """
        :param addr:需要被格式化的字符串地址
        """
        # 首尾指针
        self.head = Content.Head
        self.last = Content.Head
        self.reader = reader.Reader(addr)
        self.child = []
        no_chap = True
        no_volume = True
        tool.ready("生成文本链")
        for a in self.reader.gene():
            if a is not None:
                if self.head is None:
                    self.head = a
                a.inject(self.last)
                self.last = a
            if type(a) is Chapter:
                no_chap = False
            if type(a) is Volume:
                no_volume = False
        tool.done()

        tool.ready("插入隐式章卷")
        if no_chap:
            # 就是一段话没有章节划分
            c = Chapter(1, "总章")
            c.inject(self.head)
            c.swap(self.head)  # 插在最前
            self.head = c
        if no_volume:
            # 不分卷
            no = None
            c = None
            if type(self.head) is not Chapter:
                # 需要插入卷前语的时候

                no = 0
                c = Volume(no, "书前语")
                if config.debug:
                    print("插入隐式书前语")
            else:
                no = 1
                c = Volume(no, "自动生成卷")
                if config.debug:
                    print("插入隐式卷", no)

            c.inject(self.head)
            c.swap(self.head)  # 插在最前
            self.head = c
            last_chap = -1  # 上一章章节号

            # 扫描以生成隐式张卷
            p = self.head
            while p is not None:
                if type(p) is Chapter:
                    if p.num == 1 and (last_chap == -1 or last_chap > 5):
                        # 为了避免重复的第一章或短范围乱章被当成新的卷
                        no += 1
                        v = Volume(no, "自动生成卷")
                        v.inject(p.last)
                        if config.debug:
                            print("插入隐式卷", no)
                    last_chap = p.num
                p = p.next
        else:
            # 有卷的时候也需要检查是否需要插入书首语
            if type(self.head) is not Volume:
                # 需要插入书前语的时候
                if config.debug:
                    print("插入隐式书前语")
                c = Volume(0, "书前语")  # 对于存在0为卷号的情况会触发问题进行修复(我决定不修复了 TODO:修不修?)
                c.inject(self.head)
                c.swap(self.head)  # 插在最前

                self.head = c
        tool.done()