nenekochan
commited on
Commit
·
a7d2126
1
Parent(s):
ce39b38
parser support for voice file name
Browse files- ks_parse/__main__.py +9 -7
- ks_parse/chapter.py +11 -3
- ks_parse/chapter_adv.py +7 -3
- ks_parse/chapter_novel.py +9 -3
- ks_parse/ks.py +2 -2
- script/ogg2wav.zsh +10 -0
ks_parse/__main__.py
CHANGED
@@ -7,26 +7,28 @@ from pathlib import Path
|
|
7 |
import json
|
8 |
|
9 |
|
10 |
-
def parse_file(ks_path: Path, stype: str):
|
11 |
ks_name = f"{ks_path.parent.name}/{ks_path.stem}"
|
12 |
|
13 |
-
output_base = Path("./scenario")
|
14 |
output_base.mkdir(exist_ok=True)
|
15 |
(output_base / ks_path.parent.name).mkdir(exist_ok=True)
|
16 |
|
17 |
with ks_path.open("r") as fi:
|
18 |
-
chapters = parse(fi, stype)
|
19 |
for i, ch in enumerate(chapters):
|
20 |
with (output_base / f"{ks_name}-{i:02d}.jsonl").open("w") as fo:
|
21 |
json.dump({"title": ch.title}, fo, ensure_ascii=False)
|
22 |
print(file=fo)
|
23 |
for utt in ch.content:
|
24 |
-
|
|
|
25 |
print(file=fo)
|
26 |
|
27 |
|
28 |
if __name__ == "__main__":
|
29 |
-
if len(argv) !=
|
30 |
-
print(
|
|
|
|
|
31 |
exit(1)
|
32 |
-
parse_file(Path(argv[1]), argv[2])
|
|
|
7 |
import json
|
8 |
|
9 |
|
10 |
+
def parse_file(ks_path: Path, output_base: Path, stype: str, voice: bool = False):
|
11 |
ks_name = f"{ks_path.parent.name}/{ks_path.stem}"
|
12 |
|
|
|
13 |
output_base.mkdir(exist_ok=True)
|
14 |
(output_base / ks_path.parent.name).mkdir(exist_ok=True)
|
15 |
|
16 |
with ks_path.open("r") as fi:
|
17 |
+
chapters = parse(fi, stype, voice)
|
18 |
for i, ch in enumerate(chapters):
|
19 |
with (output_base / f"{ks_name}-{i:02d}.jsonl").open("w") as fo:
|
20 |
json.dump({"title": ch.title}, fo, ensure_ascii=False)
|
21 |
print(file=fo)
|
22 |
for utt in ch.content:
|
23 |
+
uttd = asdict(utt, filter=lambda _, v: v is not None)
|
24 |
+
json.dump(uttd, fo, ensure_ascii=False)
|
25 |
print(file=fo)
|
26 |
|
27 |
|
28 |
if __name__ == "__main__":
|
29 |
+
if len(argv) != 4:
|
30 |
+
print(
|
31 |
+
f"Usage: python -m {__package__} path/to/script.ks path/to/output/base novel|adv"
|
32 |
+
)
|
33 |
exit(1)
|
34 |
+
parse_file(Path(argv[1]), Path(argv[2]), argv[3])
|
ks_parse/chapter.py
CHANGED
@@ -4,14 +4,15 @@ from attrs import define
|
|
4 |
import re
|
5 |
from typing import Iterable, Callable
|
6 |
|
7 |
-
TAG = re.compile(r"\[(.+?)\]")
|
8 |
-
|
9 |
|
10 |
|
11 |
@define
|
12 |
class Utterance:
|
13 |
speaker: str
|
14 |
text: str
|
|
|
15 |
|
16 |
|
17 |
@define
|
@@ -20,10 +21,17 @@ class Chapter:
|
|
20 |
content: Iterable[Utterance]
|
21 |
|
22 |
|
23 |
-
type ChapterParser = Callable[[Iterable[str]], Iterable[Utterance]]
|
24 |
|
25 |
|
26 |
def clean_tag(line: str) -> str:
|
27 |
line = TAG.sub("", line.strip())
|
28 |
line = line.replace(" ", "") # Remove full-width spaces
|
29 |
return line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import re
|
5 |
from typing import Iterable, Callable
|
6 |
|
7 |
+
TAG = re.compile(r"\[(.+?)\]") # I hope there are no nested tags in the script...
|
8 |
+
TAG_FILENAME = re.compile(r"file=(.+)\b")
|
9 |
|
10 |
|
11 |
@define
|
12 |
class Utterance:
|
13 |
speaker: str
|
14 |
text: str
|
15 |
+
voice: str | None = None
|
16 |
|
17 |
|
18 |
@define
|
|
|
21 |
content: Iterable[Utterance]
|
22 |
|
23 |
|
24 |
+
type ChapterParser = Callable[[Iterable[str], bool], Iterable[Utterance]]
|
25 |
|
26 |
|
27 |
def clean_tag(line: str) -> str:
|
28 |
line = TAG.sub("", line.strip())
|
29 |
line = line.replace(" ", "") # Remove full-width spaces
|
30 |
return line
|
31 |
+
|
32 |
+
|
33 |
+
def get_tag_filename(tag: str) -> str:
|
34 |
+
match = TAG_FILENAME.search(tag)
|
35 |
+
if match:
|
36 |
+
return match.group(1)
|
37 |
+
raise ValueError(f"Tag line {tag} does not contain a filename")
|
ks_parse/chapter_adv.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from .chapter import Utterance, clean_tag
|
2 |
|
3 |
import re
|
4 |
from typing import Iterable
|
@@ -6,7 +6,7 @@ from typing import Iterable
|
|
6 |
TAG_SPEAKER = re.compile(r"\[speaker name=\"(.+)\"\]")
|
7 |
|
8 |
|
9 |
-
def parse(lines: Iterable[str]) -> Iterable[Utterance]:
|
10 |
"""Parsing Kirikiri2 script chapters of newer format,
|
11 |
which are distinguished by the presence of a `[startadv]` tag.
|
12 |
|
@@ -20,9 +20,12 @@ def parse(lines: Iterable[str]) -> Iterable[Utterance]:
|
|
20 |
|
21 |
speaker = None
|
22 |
scene_change = False
|
|
|
23 |
for line in lines:
|
24 |
if line.startswith(";"):
|
25 |
continue
|
|
|
|
|
26 |
if line.startswith("[speaker"):
|
27 |
match = TAG_SPEAKER.search(line)
|
28 |
if match is None:
|
@@ -42,8 +45,9 @@ def parse(lines: Iterable[str]) -> Iterable[Utterance]:
|
|
42 |
line += "」"
|
43 |
|
44 |
if speaker is not None:
|
45 |
-
yield Utterance(speaker=speaker, text=line)
|
46 |
speaker = None
|
|
|
47 |
else:
|
48 |
yield Utterance(speaker="narrator", text=line)
|
49 |
if scene_change:
|
|
|
1 |
+
from .chapter import Utterance, clean_tag, get_tag_filename
|
2 |
|
3 |
import re
|
4 |
from typing import Iterable
|
|
|
6 |
TAG_SPEAKER = re.compile(r"\[speaker name=\"(.+)\"\]")
|
7 |
|
8 |
|
9 |
+
def parse(lines: Iterable[str], voice: bool = False) -> Iterable[Utterance]:
|
10 |
"""Parsing Kirikiri2 script chapters of newer format,
|
11 |
which are distinguished by the presence of a `[startadv]` tag.
|
12 |
|
|
|
20 |
|
21 |
speaker = None
|
22 |
scene_change = False
|
23 |
+
voice_fname = None
|
24 |
for line in lines:
|
25 |
if line.startswith(";"):
|
26 |
continue
|
27 |
+
if voice and "[voice" in line:
|
28 |
+
voice_fname = get_tag_filename(line)
|
29 |
if line.startswith("[speaker"):
|
30 |
match = TAG_SPEAKER.search(line)
|
31 |
if match is None:
|
|
|
45 |
line += "」"
|
46 |
|
47 |
if speaker is not None:
|
48 |
+
yield Utterance(speaker=speaker, text=line, voice=voice_fname)
|
49 |
speaker = None
|
50 |
+
voice_fname = None
|
51 |
else:
|
52 |
yield Utterance(speaker="narrator", text=line)
|
53 |
if scene_change:
|
ks_parse/chapter_novel.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
-
from .chapter import Utterance, clean_tag
|
2 |
|
3 |
from typing import Iterable
|
4 |
|
5 |
|
6 |
-
def parse(lines: Iterable[str]) -> Iterable[Utterance]:
|
7 |
"""Parsing Kirikiri2 script chapters of older format,
|
8 |
which are distinguished by the presence of a `[startnovel]` tag.
|
9 |
|
@@ -16,11 +16,14 @@ def parse(lines: Iterable[str]) -> Iterable[Utterance]:
|
|
16 |
|
17 |
heroine_line = False
|
18 |
scene_change = False
|
|
|
19 |
for line in lines:
|
20 |
if line.startswith(";"):
|
21 |
continue
|
22 |
if "[voice" in line:
|
23 |
heroine_line = True
|
|
|
|
|
24 |
continue
|
25 |
scene_change |= "[msgv]" in line
|
26 |
|
@@ -32,9 +35,12 @@ def parse(lines: Iterable[str]) -> Iterable[Utterance]:
|
|
32 |
|
33 |
if line.startswith("「"):
|
34 |
yield Utterance(
|
35 |
-
speaker="heroine" if heroine_line else "protagonist",
|
|
|
|
|
36 |
)
|
37 |
heroine_line = False
|
|
|
38 |
else:
|
39 |
yield Utterance(speaker="narrator", text=line)
|
40 |
if scene_change:
|
|
|
1 |
+
from .chapter import Utterance, clean_tag, get_tag_filename
|
2 |
|
3 |
from typing import Iterable
|
4 |
|
5 |
|
6 |
+
def parse(lines: Iterable[str], voice: bool = False) -> Iterable[Utterance]:
|
7 |
"""Parsing Kirikiri2 script chapters of older format,
|
8 |
which are distinguished by the presence of a `[startnovel]` tag.
|
9 |
|
|
|
16 |
|
17 |
heroine_line = False
|
18 |
scene_change = False
|
19 |
+
voice_fname = None
|
20 |
for line in lines:
|
21 |
if line.startswith(";"):
|
22 |
continue
|
23 |
if "[voice" in line:
|
24 |
heroine_line = True
|
25 |
+
if voice:
|
26 |
+
voice_fname = get_tag_filename(line)
|
27 |
continue
|
28 |
scene_change |= "[msgv]" in line
|
29 |
|
|
|
35 |
|
36 |
if line.startswith("「"):
|
37 |
yield Utterance(
|
38 |
+
speaker="heroine" if heroine_line else "protagonist",
|
39 |
+
text=line,
|
40 |
+
voice=voice_fname,
|
41 |
)
|
42 |
heroine_line = False
|
43 |
+
voice_fname = None
|
44 |
else:
|
45 |
yield Utterance(speaker="narrator", text=line)
|
46 |
if scene_change:
|
ks_parse/ks.py
CHANGED
@@ -5,7 +5,7 @@ from .chapter_adv import parse as parse_chapter_adv
|
|
5 |
from typing import Iterable
|
6 |
|
7 |
|
8 |
-
def parse(lines: Iterable[str], stype: str) -> Iterable[Chapter]:
|
9 |
"""Entry point for parsing Kirikiri2 scripts"""
|
10 |
|
11 |
cparser: ChapterParser
|
@@ -31,6 +31,6 @@ def parse(lines: Iterable[str], stype: str) -> Iterable[Chapter]:
|
|
31 |
chunk.append(line)
|
32 |
else:
|
33 |
eof = True
|
34 |
-
ch.content = cparser(chunk)
|
35 |
chunk = []
|
36 |
yield ch
|
|
|
5 |
from typing import Iterable
|
6 |
|
7 |
|
8 |
+
def parse(lines: Iterable[str], stype: str, voice: bool = False) -> Iterable[Chapter]:
|
9 |
"""Entry point for parsing Kirikiri2 scripts"""
|
10 |
|
11 |
cparser: ChapterParser
|
|
|
31 |
chunk.append(line)
|
32 |
else:
|
33 |
eof = True
|
34 |
+
ch.content = cparser(chunk, voice)
|
35 |
chunk = []
|
36 |
yield ch
|
script/ogg2wav.zsh
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env zsh
|
2 |
+
|
3 |
+
BASE=$1
|
4 |
+
DEST=$2
|
5 |
+
|
6 |
+
mkdir -p $DEST
|
7 |
+
|
8 |
+
for f in $BASE/*.ogg; do
|
9 |
+
ffmpeg -hide_banner -loglevel error -nostats -acodec libvorbis -i $f -acodec pcm_s16le "$DEST/$f:t:r".wav
|
10 |
+
done
|