This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Processor.py
240 lines (208 loc) · 9.28 KB
/
Processor.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import re
import MeCab
import NakaDictionary
from Debug import DEBUG
class PlayMode:
"""enum class"""
HIRA_ZHUYIN = 1
NAKA_ZHUYIN = 2
TO_ROMAJI = 3
HIRA_ZHUYIN_SP = 4
class Processor:
def __init__(self, mode: int = PlayMode.HIRA_ZHUYIN):
self.mecab = MeCab.Tagger() # -Oyomi -Owakati
self.Mode = mode
def process(self, file_name: str = '123') -> str:
try:
text = open(file_name, encoding='utf-8').read()
except UnicodeDecodeError:
return ""
if text[0] != '{':
return ""
lyric_begin = text.find('"lyric"') # 是 "lyric" 的 " 的index
lyric_begin = lyric_begin + len('"lyric":"')
# begin = text[0:lyric_begin]
temp = text[lyric_begin:]
lyric_end = temp.find('"},') # 小心转义字符
lyric = temp[:lyric_end] # not with "
lines = lyric.split("\\n") # every lines lose \n
# u0800-u4e00 日文utf-8范围
processed_lyric = ""
for i in range(0, len(lines)):
time_end = lines[i].find(']') + 1
pure_l = lines[i][time_end:]
if len(pure_l) == 0: # 略去空行
processed_lyric += lines[i] + '\\n'
continue
if i <= 6: # 略去歌手名
if pure_l.find(':') != -1:
processed_lyric += lines[i] + '\\n'
continue
elif pure_l.find(':') != -1:
processed_lyric += lines[i] + '\\n'
continue
if re.search('[\u4E00-\u9FBF]', pure_l) is None: # 如果不存在汉字
processed_lyric += lines[i] + '\\n'
continue
if DEBUG:
print(pure_l)
# 初步处理 \u3000
pure_l = re.sub('\u3000', ' ', pure_l)
segment_line = pure_l.split(' ') # 有没有使用空格分开不同的句子
processed_text = ""
for segment in segment_line:
if self.Mode == PlayMode.HIRA_ZHUYIN:
processed_text += self._process_hira(segment) + ' '
elif self.Mode == PlayMode.NAKA_ZHUYIN:
processed_text += self._process_naka(segment) + ' '
elif self.Mode == PlayMode.TO_ROMAJI:
processed_text += self._process_romaji(segment) + ' '
elif self.Mode == PlayMode.HIRA_ZHUYIN_SP:
processed_text += self._process_hira_sp(segment) + ' '
processed_lyric += lines[i][:time_end] + processed_text[:-1] + '\\n'
# end for
if DEBUG:
print(processed_lyric)
result = text[:lyric_begin] + processed_lyric + temp[lyric_end:]
''' clear klyric'''
'''
klyric_begin = result.find('"klyric":{')
if klyric_begin != -1:
klyric_begin += len('"klyric":{')
if result[klyric_begin:].find('null') == -1 or result[klyric_begin:].find('null') > 35:
klyric_end = result[klyric_begin:].find('}')
clear_str = '"version":0,"lyric":null'
result = result[:klyric_begin] + clear_str + result[klyric_begin:][klyric_end:]
# version = 8 chu wen ti
version_begin = result.find('"lrc":{"version":') + len('"lrc":{"version":')
# print(result[version_begin]) first number
if 47 < ord(result[version_begin+1]) < 58:
result = result[:version_begin] + '10' + result[version_begin + 2:]
else:
result = result[:version_begin] + '10' + result[version_begin + 1:]
'''
if DEBUG:
print(result)
return result
def _process_hira(self, pure_line: str) -> str:
processed_line = ''
try:
result = str(self.mecab.parse(pure_line)).split("\n")
for single in result:
word = single.split('\t')[0] # word
if len(single.split(',')) < 3 or single.split(',')[-2] == '*': # 如果没那么多数据项说明不可读
if word != 'EOS':
processed_line += word
continue
read = single.split(',')[-2] # read
hiragana = ""
if word == read:
processed_line += word
continue
for kata in read: # 将得到的片假名注音(最后带有换行符)换成平假名
if kata in NakaDictionary.kata_to_hira:
hiragana += NakaDictionary.kata_to_hira.get(kata)
else:
hiragana += kata
if word == hiragana: # 如果注音和原句相同
processed_line += word
else:
processed_line += word + '(' + hiragana + ')'
if DEBUG:
print(processed_line)
print("######################")
except RuntimeError as e:
if DEBUG:
print("RuntimeError:" + str(e))
print('可能因为没有安装MeCab,出现了异常')
return processed_line
def _process_naka(self, pure_line: str) -> str:
processed_line = ''
try:
result = str(self.mecab.parse(pure_line)).split("\n")
for single in result:
word = single.split('\t')[0] # word
if len(single.split(',')) < 3 or single.split(',')[-2] == '*': # 如果没那么多数据项说明不可读
if word != 'EOS':
processed_line += word
continue
read = single.split(',')[-2] # read
hiragana = ""
if word == read:
processed_line += word
continue
for kata in read: # 将得到的片假名注音(最后带有换行符)换成平假名
if kata in NakaDictionary.kata_to_hira:
hiragana += NakaDictionary.kata_to_hira.get(kata)
else:
hiragana += kata
if word == hiragana: # 如果注音和原句相同
processed_line += word
else:
processed_line += word + '(' + read + ')'
if DEBUG:
print(processed_line)
print("######################")
except RuntimeError as e:
if DEBUG:
print("RuntimeError:" + str(e))
print('可能因为没有安装MeCab,出现了异常')
return processed_line
def _process_romaji(self, pure_line: str):
processed_line = ''
try:
result = str(self.mecab.parse(pure_line)).split("\n")
for single in result:
word = single.split('\t')[0] # word
if len(single.split(',')) < 3 or single.split(',')[-2] == '*': # 如果没那么多数据项说明不可读
if word != 'EOS':
processed_line += word
continue
read = single.split(',')[-2] # read
hiragana = ""
for kata in read: # 将得到的片假名注音(最后带有换行符)换成平假名
if kata in NakaDictionary.kata_to_romaji:
hiragana += NakaDictionary.kata_to_romaji.get(kata)
else:
hiragana += kata
processed_line += hiragana + " "
if DEBUG:
print(processed_line)
print("######################")
except RuntimeError as e:
if DEBUG:
print("RuntimeError:" + str(e))
print('可能因为没有安装MeCab,出现了异常')
return processed_line
def _process_hira_sp(self, pure_line: str) -> str:
processed_line = ''
try:
result = str(self.mecab.parse(pure_line)).split("\n")
for single in result:
word = single.split('\t')[0] # word
if len(single.split(',')) < 3 or single.split(',')[-2] == '*': # 如果没那么多数据项说明不可读
if word != 'EOS':
processed_line += word
continue
read = single.split(',')[-2] # read
hiragana = ""
if word == read:
processed_line += word
continue
for kata in read: # 将得到的片假名注音(最后带有换行符)换成平假名
if kata in NakaDictionary.kata_to_hira:
hiragana += NakaDictionary.kata_to_hira.get(kata)
else:
hiragana += kata
if word == hiragana: # 如果注音和原句相同
processed_line += word
else:
processed_line += '{' + word + '}' + '(' + hiragana + ')'
if DEBUG:
print(processed_line)
print("######################")
except RuntimeError as e:
if DEBUG:
print("RuntimeError:" + str(e))
print('可能因为没有安装MeCab,出现了异常')
return processed_line