import chardet from opencc import OpenCC cc = OpenCC('t2s') # 't2s'表示繁体转简体 from h_corpus import Fileset import unicodedata import json def fullwidth_to_halfwidth(input_str): return ''.join([unicodedata.normalize('NFKC', char) for char in input_str]) def clearT(s): s = cc.convert(fullwidth_to_halfwidth(s)) return s.lstrip('n').strip().strip(r'\n') def s_in(s, ss, es): return [x.split(es)[0] for x in s.split(ss) if es in x] _n = { "??": "?", "少年": "少年", "优": "优", "玛由": "玛由", "沙罗": "沙罗", "男孩": "男孩", "女孩": "女孩", "武": "武", "空": "空", "全体": "全体", "鸠": "鸠", "少女": "少女", "松永": "松永", "社长": "社长", "可可": "可可", "优、沙罗": "优&沙罗", "女": "女", "男": "男", "广播": "广播", "田中博士": "田中博士", "职员": "职员", "皮皮": "皮皮", "空A": "空A", "空B": "空B", "空C": "空C", "月": "月", "假的空": "假的空", "研究员": "研究员", "警备长": "警备长", "月海": "月海", "医师": "医师", "桑古木": "桑古木", "优秋": "优秋", "优春": "优春", "BW": "BW", "管制官": "管制官", "北斗": "北斗", "狸": "狸", "游客": "游客" } with open(r'E:\tmp\script.txt', 'rb') as f: tmp = f.read() b = open(r'E:\tmp\script_01.txt', 'w', encoding='utf-8') sep = b'\x05\x80\x00\x00' eos = b'\x02\x03\x0e' encode = 'GB2312' tmp = tmp.split(sep) 左括号 = b'\xa1\xbe' 右括号 = b'\xa1\xbf' 左引号 = b'\xa1\xb8' 右引号 = b'\xa1\xb9' sc = [] for line in tmp: if eos not in line: continue idx = line.index(eos) line = line[:idx] line = line.strip(b'\x00\x00') if b'\x00\x00' in line: continue # encode = chardet.detect(line) # if encode['confidence'] > 0.9: # print(encode['encoding']) if 左括号 in line and 右括号 in line: n = s_in(line, 左括号, 右括号) assert len(n) == 1 n = n[0].decode(encoding=encode) if 左引号 in line: r_idx = line.index(右括号) l_idx = line.index(左引号) if l_idx - r_idx == 3: line = line[l_idx:] # _n[n] = clearT(n) n = _n[n] else: n = '旁白' if 左引号 in line and 右引号 in line: line = line[:line.rindex(右引号)+2] d = clearT(line.decode(encoding=encode).replace('\n', '\\n')).replace('\x01', '') # if '不但不苦,而且吃下去之后,还会有一股芳香甘醇的' in d: # print(line) # break if d: sc.append(n + ':' + d) b.write('\n'.join(sc)) b.close() tmp = json.dumps(_n, ensure_ascii=False, indent=2)