Skip to content

Commit

Permalink
修正: 暗示的 UserDictWord.mora_count=None (#1070)
Browse files Browse the repository at this point in the history
* fix: 暗示的None

* fix: lint
  • Loading branch information
tarepan authored Feb 25, 2024
1 parent f181411 commit 418f495
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
2 changes: 2 additions & 0 deletions test/user_dict/test_user_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
yomi="テストツー",
pronunciation="テストツー",
accent_type=1,
mora_count=None,
accent_associative_rule="*",
)

Expand Down Expand Up @@ -106,6 +107,7 @@ def test_create_word(self):
yomi="テスト",
pronunciation="テスト",
accent_type=1,
mora_count=None,
accent_associative_rule="*",
),
)
Expand Down
59 changes: 32 additions & 27 deletions test/user_dict/test_user_dict_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from copy import deepcopy
from typing import TypedDict
from unittest import TestCase

Expand All @@ -21,45 +20,51 @@ class TestModel(TypedDict):
yomi: str
pronunciation: str
accent_type: int
mora_count: int | None
accent_associative_rule: str


def generate_model() -> TestModel:
return {
"surface": "テスト",
"priority": 0,
"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": 0,
"mora_count": None,
"accent_associative_rule": "*",
}


class TestUserDictWords(TestCase):
def setUp(self):
self.test_model: TestModel = {
"surface": "テスト",
"priority": 0,
"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": 0,
"accent_associative_rule": "*",
}
pass

def test_valid_word(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
try:
UserDictWord(**test_value)
except ValidationError as e:
self.fail(f"Unexpected Validation Error\n{str(e)}")

def test_convert_to_zenkaku(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
test_value["surface"] = "test"
self.assertEqual(UserDictWord(**test_value).surface, "test")

def test_count_mora(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
self.assertEqual(UserDictWord(**test_value).mora_count, 3)

def test_count_mora_x(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
for s in [chr(i) for i in range(12449, 12533)]:
if s in ["ァ", "ィ", "ゥ", "ェ", "ォ", "ッ", "ャ", "ュ", "ョ", "ヮ"]:
continue
Expand All @@ -77,7 +82,7 @@ def test_count_mora_x(self):
)

def test_count_mora_xwa(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
test_value["pronunciation"] = "クヮンセイ"
expected_count = 0
for accent_phrase in parse_kana(
Expand All @@ -90,36 +95,36 @@ def test_count_mora_xwa(self):
)

def test_invalid_pronunciation_not_katakana(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
test_value["pronunciation"] = "ぼいぼ"
with self.assertRaises(ValidationError):
UserDictWord(**test_value)

def test_invalid_pronunciation_invalid_sutegana(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
test_value["pronunciation"] = "アィウェォ"
with self.assertRaises(ValidationError):
UserDictWord(**test_value)

def test_invalid_pronunciation_invalid_xwa(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
test_value["pronunciation"] = "アヮ"
with self.assertRaises(ValidationError):
UserDictWord(**test_value)

def test_count_mora_voiced_sound(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
test_value["pronunciation"] = "ボイボ"
self.assertEqual(UserDictWord(**test_value).mora_count, 3)

def test_invalid_accent_type(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
test_value["accent_type"] = 4
with self.assertRaises(ValidationError):
UserDictWord(**test_value)

def test_invalid_accent_type_2(self):
test_value = deepcopy(self.test_model)
test_value = generate_model()
test_value["accent_type"] = -1
with self.assertRaises(ValidationError):
UserDictWord(**test_value)
1 change: 1 addition & 0 deletions voicevox_engine/user_dict/user_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def _create_word(
yomi=pronunciation,
pronunciation=pronunciation,
accent_type=accent_type,
mora_count=None,
accent_associative_rule="*",
)

Expand Down

0 comments on commit 418f495

Please sign in to comment.