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').replace('\n','\\n') a = Fileset(r'D:\datasets\tmp', ext='.jsonl') b = r'D:\datasets\b-corpus\ChatHaruhi\RolePlaying' for i in range(len(a)): path = a[i] name = path[path.rindex('\\')+1:path.rindex('.')] print(name) sc = [] with open(path, encoding='utf-8') as f: for line in f: tmp = json.loads(line) if tmp['luotuo_openai'] == 'system_prompt': n = 'system' d = clearT(tmp['text']) sc.append(n + ':' + d) continue if tmp['luotuo_openai'] == 'config': continue tmp = tmp['text'].split('\n') for one in tmp: one = one.strip() if not one: continue if ':「' in one: one = one.replace(':「', ':「', 1) elif ': 「' in one: one = one.replace(': 「', ':「', 1) elif one.startswith(':'): one = '旁白:' + one[1:] elif ':「' in one or one.startswith('旁白:'): pass elif one.startswith('旁白: '): one = one.replace('旁白: ', '旁白:', 1) elif one.startswith('旁白:'): one = one.replace('旁白:', '旁白:', 1) elif one.startswith('scene:'): one = one.replace('scene:', '旁白:', 1) elif one.startswith('Narrator:'): one = one.replace('Narrator:', 'Narrator:', 1) elif one.startswith('春日:'): one = one.replace('春日:', '春日:', 1) elif ':' in one: pass elif ':' in one: one = one.replace(':', ':', 1) else: one = '旁白:' + one print(one) sc.append(one) if sc: with open(b + f'\\{name}.txt', 'w', encoding='utf-8') as f: f.write('\n'.join(sc))