Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: user_dict_startup_processingのテストを追加。 #483

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion test/test_user_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from unittest import TestCase

from fastapi import HTTPException
from pyopenjtalk import unset_user_dict
from pyopenjtalk import g2p, unset_user_dict

from voicevox_engine.model import UserDictWord, WordTypes
from voicevox_engine.part_of_speech_data import MAX_PRIORITY, part_of_speech_data
Expand All @@ -17,6 +17,7 @@
import_user_dict,
read_dict,
rewrite_word,
user_dict_startup_processing,
)

# jsonとして保存される正しい形式の辞書データ
Expand Down Expand Up @@ -315,3 +316,33 @@ def test_import_invalid_word(self):
user_dict_path=user_dict_path,
compiled_dict_path=compiled_dict_path,
)

def test_startup_processing(self):
user_dict_path = self.tmp_dir_path / "test_startup_processing_dict.json"
compiled_dict_path = self.tmp_dir_path / "test_startup_processing_dict.dic"
user_dict_startup_processing(
user_dict_path=user_dict_path, compiled_dict_path=compiled_dict_path
)
test_text = "テスト用の文字列"
success_pronunciation = "デフォルトノジショデハゼッタイニセイセイサレナイヨミ"

# 既に辞書に登録されていないか確認する
self.assertNotEqual(g2p(text=test_text, kana=True), success_pronunciation)

apply_word(
surface=test_text,
pronunciation=success_pronunciation,
accent_type=1,
priority=10,
user_dict_path=user_dict_path,
compiled_dict_path=compiled_dict_path,
)
self.assertEqual(g2p(text=test_text, kana=True), success_pronunciation)

# 疑似的にエンジンを再起動する
unset_user_dict()
user_dict_startup_processing(
user_dict_path=user_dict_path, compiled_dict_path=compiled_dict_path
)

self.assertEqual(g2p(text=test_text, kana=True), success_pronunciation)
18 changes: 12 additions & 6 deletions voicevox_engine/user_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def write_to_json(user_dict: Dict[str, UserDictWord], user_dict_path: Path):

def user_dict_startup_processing(
default_dict_path: Path = default_dict_path,
user_dict_path: Path = user_dict_path,
compiled_dict_path: Path = compiled_dict_path,
):
pyopenjtalk.create_user_dict(
Expand All @@ -51,12 +52,15 @@ def user_dict_startup_processing(
pyopenjtalk.set_user_dict(str(compiled_dict_path.resolve(strict=True)))
if user_dict_path.is_file():
update_dict(
default_dict_path=default_dict_path, compiled_dict_path=compiled_dict_path
default_dict_path=default_dict_path,
user_dict_path=user_dict_path,
compiled_dict_path=compiled_dict_path,
)


def update_dict(
default_dict_path: Path = default_dict_path,
user_dict_path: Path = user_dict_path,
compiled_dict_path: Path = compiled_dict_path,
):
with NamedTemporaryFile(encoding="utf-8", mode="w", delete=False) as f:
Expand All @@ -67,7 +71,7 @@ def update_dict(
if default_dict == default_dict.rstrip():
default_dict += "\n"
f.write(default_dict)
user_dict = read_dict()
user_dict = read_dict(user_dict_path=user_dict_path)
for word_uuid in user_dict:
word = user_dict[word_uuid]
f.write(
Expand Down Expand Up @@ -185,7 +189,7 @@ def apply_word(
word_uuid = str(uuid4())
user_dict[word_uuid] = word
write_to_json(user_dict, user_dict_path)
update_dict(compiled_dict_path=compiled_dict_path)
update_dict(user_dict_path=user_dict_path, compiled_dict_path=compiled_dict_path)
return word_uuid


Expand All @@ -211,7 +215,7 @@ def rewrite_word(
raise HTTPException(status_code=422, detail="UUIDに該当するワードが見つかりませんでした")
user_dict[word_uuid] = word
write_to_json(user_dict, user_dict_path)
update_dict(compiled_dict_path=compiled_dict_path)
update_dict(user_dict_path=user_dict_path, compiled_dict_path=compiled_dict_path)


def delete_word(
Expand All @@ -224,7 +228,7 @@ def delete_word(
raise HTTPException(status_code=422, detail="IDに該当するワードが見つかりませんでした")
del user_dict[word_uuid]
write_to_json(user_dict, user_dict_path)
update_dict(compiled_dict_path=compiled_dict_path)
update_dict(user_dict_path=user_dict_path, compiled_dict_path=compiled_dict_path)


def import_user_dict(
Expand Down Expand Up @@ -263,7 +267,9 @@ def import_user_dict(
new_dict = {**dict_data, **old_dict}
write_to_json(user_dict=new_dict, user_dict_path=user_dict_path)
update_dict(
default_dict_path=default_dict_path, compiled_dict_path=compiled_dict_path
default_dict_path=default_dict_path,
user_dict_path=user_dict_path,
compiled_dict_path=compiled_dict_path,
)


Expand Down