-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
247 lines (228 loc) · 7.89 KB
/
Copy pathmodels.py
File metadata and controls
247 lines (228 loc) · 7.89 KB
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
241
242
243
244
245
246
247
from pydantic import BaseModel, TypeAdapter, model_validator
from pathlib import Path
import json
class UnscriptedLine(BaseModel):
"""
{
"voice_id": "0010000793V",
"text": "お昼頃か ギルドの研修の真っ最中ねー"
}
"""
voice_id: str
text: str
@property
def scene_id(self):
return self.voice_id[3:6]
@property
def scene_seq_id(self):
return int(self.voice_id[6:10] or "-1")
class Line(UnscriptedLine):
"""
{
"character_id": "0xF",
"voice_id": "0940010125V",
"script_id": 49,
"text": "おや、嬢ちゃんたちは……",
"source_file": "C0100.txt",
"context_prev": "",
"context_next": "あなたが鉱山長さん?よかった、やっと見つけたわ。"
}
"""
character_id: str
script_id: int
source_file: str
context_prev: str
context_next: str
@property
def speaker_code(self):
"""voice_id 前三位的角色码,如 0010290822V -> 001"""
return self.voice_id[:3] if len(self.voice_id) >= 3 else (self.voice_id or "")
class Conversation(BaseModel):
lines: list[Line]
def __len__(self) -> int:
return len(self.lines)
def __getitem__(self, index: int) -> Line:
return self.lines[index]
def __iter__(self):
return iter(self.lines)
class RemakeCommand(BaseModel):
"""
{
"file": "F:\\code\\sora-script-test\\scena\\jp\\mp0000.py",
"line": 9941,
"column": 4,
"type": "Command",
"code": "Command('Cmd_text_00', [INT(10007), '<#E_0#M_0#B_0>', '何だ、エステル。', INT(10), 'どっか出かけんのか?'])",
"normalized_args": "5,0,10007,<#E_0#M_0#B_0>,何だ、エステル。,10,どっか出かけんのか?",
"command": "Cmd_text_00",
"args": [
10007,
"何だ、エステル。どっか出かけんのか?"
],
"line_corr": 9291
}
"""
file: str
line: int
column: int
type: str
code: str
normalized_args: str
command: str
args: list
line_corr: int | None
class RemakeLine(BaseModel):
id: int
text: str
remake_voice_id: int | None = None
speaker: int | None = None
function: str | None = None
filebase: str
lineno: int
lineno_corr: int | None = None
@model_validator(mode="before")
@classmethod
def handle_remake_commands(cls, data):
if isinstance(data, dict):
args = data.pop("args", None)
if args:
data["text"] = args[-1]
if len(args) >= 3 and args[-3] == 11 and isinstance(args[-2], int):
data["remake_voice_id"] = args[-2]
if len(args) >= 2 and args[-2] == 11 and isinstance(args[-1], int):
data["remake_voice_id"] = args[-1]
data["text"] = ""
if isinstance(args[0], int):
data["speaker"] = args[0]
function = data.pop("function", None)
if function:
data["function"] = function
file = data.pop("file", None)
if file:
data["filebase"] = Path(file).stem
line = data.pop("line", None)
if line:
data["lineno"] = line
line_corr = data.pop("line_corr", None)
if line_corr:
data["lineno_corr"] = line_corr
elif isinstance(data, RemakeCommand):
args = data.args
if args:
data["text"] = args[-1]
if len(args) >= 3 and args[-3] == 11 and isinstance(args[-2], int):
data["remake_voice_id"] = args[-2]
if len(args) >= 2 and args[-2] == 11 and isinstance(args[-1], int):
data["remake_voice_id"] = args[-1]
data["text"] = ""
if isinstance(args[0], int):
data["speaker"] = args[0]
function = data.function
if function:
data["function"] = function
file = data.file
if file:
data["filebase"] = Path(file).stem
line = data.line
if line:
data["lineno"] = line
line_corr = data.line_corr
if line_corr:
data["lineno_corr"] = line_corr
return data
class RemakeConversation(BaseModel):
lines: list[RemakeLine]
def __len__(self) -> int:
return len(self.lines)
def __getitem__(self, index: int) -> RemakeLine:
return self.lines[index]
def __iter__(self):
return iter(self.lines)
class UnscriptedConversation:
lines: list[UnscriptedLine]
def __init__(self, file: str | None = None) -> None:
self.lines = []
if file:
with open(file, "r", encoding="utf-8") as f:
adapter = TypeAdapter(list[UnscriptedLine])
self.lines = adapter.validate_json(f.read())
self.texts = [line.text for line in self.lines]
def __len__(self) -> int:
return len(self.lines)
def __getitem__(self, index: int) -> UnscriptedLine:
return self.lines[index]
def __iter__(self):
return iter(self.lines)
class Script :
def __init__(self, file:str) -> None:
with open(file, "r", encoding="utf-8") as f:
adapter = TypeAdapter(list[Line])
self.lines = adapter.validate_json(f.read())
self.texts = [line.text for line in self.lines]
def __len__(self) -> int:
return len(self.lines)
def __getitem__(self, index: int) -> Line:
return self.lines[index]
def __iter__(self):
return iter(self.lines)
class RemakeScript :
NEW_ID_START=50001
lines: list[RemakeLine]
def __init__(self, file:str, new_id_start:int = 50001) -> None:
self.NEW_ID_START = new_id_start
self.lines = []
try:
with open(file, "r", encoding="utf-8") as f:
commands: list[dict] = json.load(f)
for i, entry in enumerate(commands):
remake_line = RemakeLine(id=self.NEW_ID_START + i, **entry)
self.lines.append(remake_line)
except FileNotFoundError:
print(f"File {file} not found.")
self.texts = [line.text for line in self.lines]
def __len__(self) -> int:
return len(self.lines)
def __getitem__(self, index: int) -> RemakeLine:
return self.lines[index]
def __iter__(self):
return iter(self.lines)
def test_lines():
with open("script_data.json", "r", encoding="utf-8") as f:
# The JSON file is a list of objects, so we use TypeAdapter to validate it as a list[Line]
adapter = TypeAdapter(list[Line])
lines = adapter.validate_json(f.read())
for line in lines:
print(line)
def test_unscriptedline():
with open("additional_voice_fc.json", "r", encoding="utf-8") as f:
adapter = TypeAdapter(list[UnscriptedLine])
lines = adapter.validate_json(f.read())
for line in lines:
print(line)
def test_voice_id():
voice_id = VoiceId(voice_id="0940010125V")
print(voice_id.scene_id)
print(voice_id.scene_seq_id)
voice_empty = VoiceId(voice_id="")
print(voice_empty.scene_id)
print(voice_empty.scene_seq_id)
def test_remake_command():
NEW_ID_START=50001
with open("scena_data_jp_Command_sample.json", "r", encoding="utf-8") as f:
adapter = TypeAdapter(list[RemakeCommand])
lines = adapter.validate_json(f.read())
for line in lines:
print(line)
def test_remake_line():
NEW_ID_START=50001
with open("scena_data_jp_Command.json", "r", encoding="utf-8") as f:
commands: list[dict] = json.load(f)
for i, entry in enumerate(commands):
remake_line = RemakeLine(id=NEW_ID_START + i, **entry)
print(remake_line)
if __name__ == "__main__":
# test_lines()
# test_voice_id()
# test_remake_command()
# test_remake_line()
test_unscriptedline()