File size: 5,050 Bytes
7bb9b48 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
from opencc import OpenCC
cc = OpenCC('t2s') # 't2s'表示繁体转简体
from h_corpus import Fileset
import unicodedata
import re
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.strip()
def safe_index(s, _s, _i):
try:
retn = s.index(_s, _i)
except:
return 0, ''
return retn+1, s[_i:retn]
def get_p(s, sep=' ', _max=4):
retn = []
idx = 0
for i in range(_max):
idx, tmp = safe_index(s, sep, idx)
if idx == 0:
break
retn.append(tmp)
retn.append(s[idx:])
return retn
a = Fileset(r'D:\datasets\tmp', ext='.ks')
b = r'D:\datasets\b-corpus\KAMIKIMIEX'
in_talk = False
_n = {
"???": "?",
"心の声": "旁白",
"块斗": "块斗",
"C‐AI": "C-AI",
"月咏": "月咏",
"雾香": "雾香",
"喧哗的女生": "喧哗的女生",
"爱凑热闹的女生": "爱凑热闹的女生",
"帅气的男生": "帅气的男生",
"拉娜": "拉娜",
"值日生的山本": "值日生的山本",
"目出岛": "目出岛",
"塊斗": "块斗",
"安保系统": "安保系统",
"叶月": "叶月",
"老师": "老师",
"OldMan": "OldMan",
"索菲娅": "索菲娅",
"玲音": "玲音",
"爱彩梨": "爱彩梨",
"高个子的男生": "高个子的男生",
"松冈": "松冈",
"霧香": "雾香",
"伊澄博士": "伊澄博士",
"记者": "记者",
"喜欢八卦的女生": "喜欢八卦的女生",
"不受欢迎的男生": "不受欢迎的男生",
"新闻部的男生": "新闻部的男生",
"新闻部部长": "新闻部部长",
"月咏1": "月咏1",
"月咏2": "月咏2",
"月咏3": "月咏3",
"月咏们": "月咏们",
"盖着帽子的男人": "盖着帽子的男人",
"女性播报员": "女性播报员",
"神秘的男子": "神秘的男子",
"特工": "特工",
"泽库斯": "泽库斯",
"艾恩斯": "艾恩斯",
"采访人员": "采访人员",
"一年级男生": "一年级男生",
"孤独者": "孤独者",
"完美超人": "完美超人",
"愛彩梨": "爱彩梨",
"部员": "部员",
"街头大屏幕": "街头大屏幕",
"部員": "部员",
"护士": "护士",
"店员": "店员",
"雾斗": "块斗",
"樱木明里": "樱木明里",
"黑江": "黑江",
"小女孩": "小女孩",
"医生": "医生",
"啊斗": "块斗",
"书记": "书记",
"不认识的男人": "不认识的男人",
"帅哥": "帅哥",
"新闻主持人": "新闻主持人",
"大妈": "大妈",
"大叔": "大叔",
"块斗&雾香": "块斗&雾香",
"爱斗": "块斗",
"女子篮球部员": "女子篮球部员",
"顾問": "顾问",
"对手队长": "对手队长",
"AI": "AI",
"流浪仓鼠": "流浪仓鼠",
"爱爱凑热闹的女生": "爱爱凑热闹的女生",
"ツクヨミ": "ツクヨミ", # 月咏 先不改,便于定位后进行全文翻译
"黄毛小混混": "黄毛小混混",
"纹身小混混": "纹身小混混",
"金发小混混": "金发小混混",
"冬守": "冬守",
"梦山": "梦山",
"强壮的保安": "强壮的保安",
"高个子保安": "高个子保安",
"强悍的保安": "强悍的保安",
"海枝所长": "海枝所长",
"女新闻主持人": "女新闻主持人",
"男新闻主持人": "男新闻主持人"
}
reg_name = re.compile(r' name=(.+?)($| )')
reg_cs = re.compile(r' text=(.+?)($| )')
d_s = []
idx = 0
sub_idx = 0
for i in range(len(a)):
sc = []
with open(a[i], encoding='UTF-16LE') as f:
for line in f:
if len(line) < idx+1:
continue
line = line[idx:]
if line.startswith('else'):
continue
if line.startswith('@if') and not line.startswith('@if exp="ChkSelect'):
idx += 1
continue
if line.startswith('endif') and idx > 0:
idx -= 1
continue
if line.startswith('@AddSelect') and 'invalid' not in line:
d_s.append(reg_cs.findall(line)[0][0])
if (line.startswith('@if exp="ChkSelect') or line.startswith('elsif exp="ChkSelect')) and d_s:
if line.startswith('@'):
idx += 1
n = '选择'
d = clearT(d_s[0])
d_s = d_s[1:]
if d:
sc.append(n + ':' + d)
if line.startswith('@Hitret'):
in_talk = False
if in_talk:
in_talk = False
d = clearT(line)
if d:
sc.append(n + ':' + d)
if line.startswith(r'@Talk'):
n = reg_name.findall(line)[0][0]
n = clearT(_n.get(n, n))
in_talk = True
if sc:
with open(b + f'\\{i}.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(sc))
tmp = json.dumps(_n, ensure_ascii=False, indent=2) |