File size: 13,025 Bytes
42472b3 |
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
import json
from copy import deepcopy
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Dict
from unittest import TestCase
from fastapi import HTTPException
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
from voicevox_engine.user_dict import (
apply_word,
create_word,
delete_word,
import_user_dict,
read_dict,
rewrite_word,
update_dict,
)
# jsonとして保存される正しい形式の辞書データ
valid_dict_dict_json = {
"aab7dda2-0d97-43c8-8cb7-3f440dab9b4e": {
"surface": "test",
"cost": part_of_speech_data[WordTypes.PROPER_NOUN].cost_candidates[5],
"part_of_speech": "名詞",
"part_of_speech_detail_1": "固有名詞",
"part_of_speech_detail_2": "一般",
"part_of_speech_detail_3": "*",
"inflectional_type": "*",
"inflectional_form": "*",
"stem": "*",
"yomi": "テスト",
"pronunciation": "テスト",
"accent_type": 1,
"accent_associative_rule": "*",
},
}
# APIでやり取りされる正しい形式の辞書データ
valid_dict_dict_api = deepcopy(valid_dict_dict_json)
del valid_dict_dict_api["aab7dda2-0d97-43c8-8cb7-3f440dab9b4e"]["cost"]
valid_dict_dict_api["aab7dda2-0d97-43c8-8cb7-3f440dab9b4e"]["priority"] = 5
import_word = UserDictWord(
surface="test2",
priority=5,
part_of_speech="名詞",
part_of_speech_detail_1="固有名詞",
part_of_speech_detail_2="一般",
part_of_speech_detail_3="*",
inflectional_type="*",
inflectional_form="*",
stem="*",
yomi="テストツー",
pronunciation="テストツー",
accent_type=1,
accent_associative_rule="*",
)
def get_new_word(user_dict: Dict[str, UserDictWord]):
assert len(user_dict) == 2 or (
len(user_dict) == 1 and "aab7dda2-0d97-43c8-8cb7-3f440dab9b4e" not in user_dict
)
for word_uuid in user_dict.keys():
if word_uuid == "aab7dda2-0d97-43c8-8cb7-3f440dab9b4e":
continue
return user_dict[word_uuid]
raise AssertionError
class TestUserDict(TestCase):
def setUp(self):
self.tmp_dir = TemporaryDirectory()
self.tmp_dir_path = Path(self.tmp_dir.name)
def tearDown(self):
unset_user_dict()
self.tmp_dir.cleanup()
def test_read_not_exist_json(self):
self.assertEqual(
read_dict(user_dict_path=(self.tmp_dir_path / "not_exist.json")),
{},
)
def test_create_word(self):
# 将来的に品詞などが追加された時にテストを増やす
self.assertEqual(
create_word(surface="test", pronunciation="テスト", accent_type=1),
UserDictWord(
surface="test",
priority=5,
part_of_speech="名詞",
part_of_speech_detail_1="固有名詞",
part_of_speech_detail_2="一般",
part_of_speech_detail_3="*",
inflectional_type="*",
inflectional_form="*",
stem="*",
yomi="テスト",
pronunciation="テスト",
accent_type=1,
accent_associative_rule="*",
),
)
def test_apply_word_without_json(self):
user_dict_path = self.tmp_dir_path / "test_apply_word_without_json.json"
apply_word(
surface="test",
pronunciation="テスト",
accent_type=1,
user_dict_path=user_dict_path,
compiled_dict_path=(self.tmp_dir_path / "test_apply_word_without_json.dic"),
)
res = read_dict(user_dict_path=user_dict_path)
self.assertEqual(len(res), 1)
new_word = get_new_word(res)
self.assertEqual(
(
new_word.surface,
new_word.pronunciation,
new_word.accent_type,
),
("test", "テスト", 1),
)
def test_apply_word_with_json(self):
user_dict_path = self.tmp_dir_path / "test_apply_word_with_json.json"
user_dict_path.write_text(
json.dumps(valid_dict_dict_json, ensure_ascii=False), encoding="utf-8"
)
apply_word(
surface="test2",
pronunciation="テストツー",
accent_type=3,
user_dict_path=user_dict_path,
compiled_dict_path=(self.tmp_dir_path / "test_apply_word_with_json.dic"),
)
res = read_dict(user_dict_path=user_dict_path)
self.assertEqual(len(res), 2)
new_word = get_new_word(res)
self.assertEqual(
(
new_word.surface,
new_word.pronunciation,
new_word.accent_type,
),
("test2", "テストツー", 3),
)
def test_rewrite_word_invalid_id(self):
user_dict_path = self.tmp_dir_path / "test_rewrite_word_invalid_id.json"
user_dict_path.write_text(
json.dumps(valid_dict_dict_json, ensure_ascii=False), encoding="utf-8"
)
self.assertRaises(
HTTPException,
rewrite_word,
word_uuid="c2be4dc5-d07d-4767-8be1-04a1bb3f05a9",
surface="test2",
pronunciation="テストツー",
accent_type=2,
user_dict_path=user_dict_path,
compiled_dict_path=(self.tmp_dir_path / "test_rewrite_word_invalid_id.dic"),
)
def test_rewrite_word_valid_id(self):
user_dict_path = self.tmp_dir_path / "test_rewrite_word_valid_id.json"
user_dict_path.write_text(
json.dumps(valid_dict_dict_json, ensure_ascii=False), encoding="utf-8"
)
rewrite_word(
word_uuid="aab7dda2-0d97-43c8-8cb7-3f440dab9b4e",
surface="test2",
pronunciation="テストツー",
accent_type=2,
user_dict_path=user_dict_path,
compiled_dict_path=(self.tmp_dir_path / "test_rewrite_word_valid_id.dic"),
)
new_word = read_dict(user_dict_path=user_dict_path)[
"aab7dda2-0d97-43c8-8cb7-3f440dab9b4e"
]
self.assertEqual(
(new_word.surface, new_word.pronunciation, new_word.accent_type),
("test2", "テストツー", 2),
)
def test_delete_word_invalid_id(self):
user_dict_path = self.tmp_dir_path / "test_delete_word_invalid_id.json"
user_dict_path.write_text(
json.dumps(valid_dict_dict_json, ensure_ascii=False), encoding="utf-8"
)
self.assertRaises(
HTTPException,
delete_word,
word_uuid="c2be4dc5-d07d-4767-8be1-04a1bb3f05a9",
user_dict_path=user_dict_path,
compiled_dict_path=(self.tmp_dir_path / "test_delete_word_invalid_id.dic"),
)
def test_delete_word_valid_id(self):
user_dict_path = self.tmp_dir_path / "test_delete_word_valid_id.json"
user_dict_path.write_text(
json.dumps(valid_dict_dict_json, ensure_ascii=False), encoding="utf-8"
)
delete_word(
word_uuid="aab7dda2-0d97-43c8-8cb7-3f440dab9b4e",
user_dict_path=user_dict_path,
compiled_dict_path=(self.tmp_dir_path / "test_delete_word_valid_id.dic"),
)
self.assertEqual(len(read_dict(user_dict_path=user_dict_path)), 0)
def test_priority(self):
for pos in part_of_speech_data:
for i in range(MAX_PRIORITY + 1):
self.assertEqual(
create_word(
surface="test",
pronunciation="テスト",
accent_type=1,
word_type=pos,
priority=i,
).priority,
i,
)
def test_import_dict(self):
user_dict_path = self.tmp_dir_path / "test_import_dict.json"
compiled_dict_path = self.tmp_dir_path / "test_import_dict.dic"
user_dict_path.write_text(
json.dumps(valid_dict_dict_json, ensure_ascii=False), encoding="utf-8"
)
import_user_dict(
{"b1affe2a-d5f0-4050-926c-f28e0c1d9a98": import_word},
override=False,
user_dict_path=user_dict_path,
compiled_dict_path=compiled_dict_path,
)
self.assertEqual(
read_dict(user_dict_path)["b1affe2a-d5f0-4050-926c-f28e0c1d9a98"],
import_word,
)
self.assertEqual(
read_dict(user_dict_path)["aab7dda2-0d97-43c8-8cb7-3f440dab9b4e"],
UserDictWord(**valid_dict_dict_api["aab7dda2-0d97-43c8-8cb7-3f440dab9b4e"]),
)
def test_import_dict_no_override(self):
user_dict_path = self.tmp_dir_path / "test_import_dict_no_override.json"
compiled_dict_path = self.tmp_dir_path / "test_import_dict_no_override.dic"
user_dict_path.write_text(
json.dumps(valid_dict_dict_json, ensure_ascii=False), encoding="utf-8"
)
import_user_dict(
{"aab7dda2-0d97-43c8-8cb7-3f440dab9b4e": import_word},
override=False,
user_dict_path=user_dict_path,
compiled_dict_path=compiled_dict_path,
)
self.assertEqual(
read_dict(user_dict_path)["aab7dda2-0d97-43c8-8cb7-3f440dab9b4e"],
UserDictWord(**valid_dict_dict_api["aab7dda2-0d97-43c8-8cb7-3f440dab9b4e"]),
)
def test_import_dict_override(self):
user_dict_path = self.tmp_dir_path / "test_import_dict_override.json"
compiled_dict_path = self.tmp_dir_path / "test_import_dict_override.dic"
user_dict_path.write_text(
json.dumps(valid_dict_dict_json, ensure_ascii=False), encoding="utf-8"
)
import_user_dict(
{"aab7dda2-0d97-43c8-8cb7-3f440dab9b4e": import_word},
override=True,
user_dict_path=user_dict_path,
compiled_dict_path=compiled_dict_path,
)
self.assertEqual(
read_dict(user_dict_path)["aab7dda2-0d97-43c8-8cb7-3f440dab9b4e"],
import_word,
)
def test_import_invalid_word(self):
user_dict_path = self.tmp_dir_path / "test_import_invalid_dict.json"
compiled_dict_path = self.tmp_dir_path / "test_import_invalid_dict.dic"
invalid_accent_associative_rule_word = deepcopy(import_word)
invalid_accent_associative_rule_word.accent_associative_rule = "invalid"
user_dict_path.write_text(
json.dumps(valid_dict_dict_json, ensure_ascii=False), encoding="utf-8"
)
self.assertRaises(
AssertionError,
import_user_dict,
{
"aab7dda2-0d97-43c8-8cb7-3f440dab9b4e": invalid_accent_associative_rule_word
},
override=True,
user_dict_path=user_dict_path,
compiled_dict_path=compiled_dict_path,
)
invalid_pos_word = deepcopy(import_word)
invalid_pos_word.context_id = 2
invalid_pos_word.part_of_speech = "フィラー"
invalid_pos_word.part_of_speech_detail_1 = "*"
invalid_pos_word.part_of_speech_detail_2 = "*"
invalid_pos_word.part_of_speech_detail_3 = "*"
self.assertRaises(
ValueError,
import_user_dict,
{"aab7dda2-0d97-43c8-8cb7-3f440dab9b4e": invalid_pos_word},
override=True,
user_dict_path=user_dict_path,
compiled_dict_path=compiled_dict_path,
)
def test_update_dict(self):
user_dict_path = self.tmp_dir_path / "test_update_dict.json"
compiled_dict_path = self.tmp_dir_path / "test_update_dict.dic"
update_dict(
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()
update_dict(
user_dict_path=user_dict_path, compiled_dict_path=compiled_dict_path
)
self.assertEqual(g2p(text=test_text, kana=True), success_pronunciation)
|