#!/usr/bin/python3 # -*- coding: utf-8 -*- import argparse from pathlib import Path import re import shutil import string import pandas as pd from tqdm import tqdm from project_settings import project_path def get_args(): parser = argparse.ArgumentParser() parser.add_argument( "--data_dir", default=(project_path / "data").as_posix(), type=str ) parser.add_argument( "--output_file", default="metadata.csv", type=str ) args = parser.parse_args() return args class RepeatReplacer(object): def __init__(self, repeat_regexp: str = '(\\w*)(\\w)\\2(\\w*)', repl: str = '\\1\\2\\3'): self.repeat_regexp = re.compile(repeat_regexp) self.repl = repl def replace(self, word: str): repl_word = self.repeat_regexp.sub(self.repl, word) if repl_word != word: return self.replace(repl_word) else: return repl_word class FilenamePreprocess(object): def __init__(self): self.punctuation_map = { ",": ",", "。": "", ".": "", "、": ",", "?": "", "?": "", ":": "", ":": "", "/": "_", "<": "", ">": "", "{": "{", "}": "}", "(": "(", ")": ")", "【": "(", "】": ")", "「": "\'", "」": "\'", "『": "\'", "』": "\'", "《": "(", "》": ")", "”": "\'", "“": "\'", "‘": "\'", "’": "\'", "…": "-", "=": "", "^_^": "", "◆": "", "☆": "", "...": "", "": "", " ": "", " ": "", "\t": "", "\n": "", "\r": "", "\v": "", "\f": "", } self.rstrip_char = list("(_-") self.pattern_map = { # r"-+": "-", # r"!+": "!", } self.repeat_replacer = RepeatReplacer( repeat_regexp='(\\w*)([-!…])\\2(\\w*)', repl='\\1\\2\\3', ) def char_b2q(self, uchar): """单个字符 半角转全角""" inside_code = ord(uchar) if inside_code < 0x0020 or inside_code > 0x7e: return uchar if inside_code == 0x0020: inside_code = 0x3000 else: inside_code += 0xfee0 return chr(inside_code) def char_q2b(self, uchar): """单个字符 全角转半角""" inside_code = ord(uchar) if inside_code == 0x3000: inside_code = 0x0020 else: inside_code -= 0xfee0 if inside_code < 0x0020 or inside_code > 0x7e: return uchar return chr(inside_code) def q2b(self, text: str): """全角转半角""" result = "" for c in text: c = self.char_q2b(c) result += c return result def remove_space(self, text: str): text = text.replace(" ", "") return text def replace_punctuation(self, text: str): for k, v in self.punctuation_map.items(): text = text.replace(k, v) return text def replace_by_pattern(self, text: str): for k, v in self.pattern_map.items(): text = re.sub(k, v, text) return text def replace_repeat(self, text: str): text = self.repeat_replacer.replace(text) return text def strip_brackets(self, text: str): text_ = text if text_.startswith("("): text_ = text_.replace("(", "", 1) text_ = text_.replace(")", "", 1) if text_.startswith("《"): text_ = text_.replace("《", "", 1) text_ = text_.replace("》", "", 1) if text_.startswith("("): text_ = text_.replace("(", "", 1) text_ = text_.replace(")", "", 1) if text_.startswith("【"): text_ = text_.replace("【", "", 1) text_ = text_.replace("】", "", 1) if text_ != text: text_ = self.strip_brackets(text_) return text_ def rstrip(self, text: str): for c in self.rstrip_char: text = text.rstrip(c) return text def process(self, text: str): # print(text) text = self.q2b(text) text = self.strip_brackets(text) text = self.replace_punctuation(text) text = self.replace_by_pattern(text) text = self.replace_repeat(text) text = self.rstrip(text) # print(text) return text def main(): args = get_args() data_dir = Path(args.data_dir) filename_list = data_dir.glob("*/*.txt") fn_preprocess = FilenamePreprocess() result = list() for filename in tqdm(filename_list): name = filename.stem splits = name.split("_") idx = splits[-1] novel_name = "_".join(splits[:-1]) new_novel_name = fn_preprocess.process(novel_name) new_filename = filename.parent / "{}_{}.txt".format(new_novel_name, idx) new_filename = new_filename.as_posix().replace("/data/", "/data/") new_filename = Path(new_filename) new_filename.parent.mkdir(parents=True, exist_ok=True) shutil.move(filename.as_posix(), new_filename.as_posix()) return if __name__ == '__main__': main()