Skip to content

Commit

Permalink
疑問文の仕様変更反映 VOICEVOX#272 (VOICEVOX#273)
Browse files Browse the repository at this point in the history
* 疑問文の仕様変更反映 VOICEVOX#272

- AccentPhraseにis_interrogativeをもたせる
- 疑問符Mora追加を調整前ではなく調整後に行うようにした VOICEVOX#272 (comment)

refs VOICEVOX#272

* create_kanaを疑問文に対応 VOICEVOX#253

/accent_phrases時にis_kanaがtrueだとInternal Server
errorが発生するようになったため、 VOICEVOX#255 での修正をこっちに統合した

refs VOICEVOX#253,VOICEVOX#255

* printが入っていたので消した

* 末尾がptich0でcreate_kanaすると?がつけられてしまうので修正

VOICEVOX#273 (comment)
  • Loading branch information
qwerty2501 authored Jan 10, 2022
1 parent bdf712f commit 5496881
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 155 deletions.
8 changes: 2 additions & 6 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ def accent_phrases(
"""
if is_kana:
try:
accent_phrases, interrogative_accent_phrase_marks = parse_kana(
text, enable_interrogative
)
accent_phrases = parse_kana(text, enable_interrogative)
except ParseKanaError as err:
raise HTTPException(
status_code=400,
Expand All @@ -196,9 +194,7 @@ def accent_phrases(
accent_phrases=accent_phrases, speaker_id=speaker
)

return adjust_interrogative_accent_phrases(
accent_phrases, interrogative_accent_phrase_marks, enable_interrogative
)
return adjust_interrogative_accent_phrases(accent_phrases)
else:
return engine.create_accent_phrases(
text,
Expand Down
180 changes: 139 additions & 41 deletions test/test_kana_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def parse_kana(text: str) -> List[AccentPhrase]:
accent_phrases, _ = kana_parser.parse_kana(text, False)
accent_phrases = kana_parser.parse_kana(text, False)
return accent_phrases


Expand Down Expand Up @@ -61,17 +61,9 @@ def _interrogative_accent_phrase_marks_base(
text: str,
enable_interrogative: bool,
expected_accent_phrases: List[AccentPhrase],
expected_interrogative_accent_phrase_marks: List[bool],
):
accent_phrases, interrogative_accent_phrase_marks = kana_parser.parse_kana(
text, enable_interrogative
)
self.assertEqual(len(accent_phrases), len(interrogative_accent_phrase_marks))
accent_phrases = kana_parser.parse_kana(text, enable_interrogative)
self.assertEqual(expected_accent_phrases, accent_phrases)
self.assertEqual(
interrogative_accent_phrase_marks,
expected_interrogative_accent_phrase_marks,
)

def test_interrogative_accent_phrase_marks(self):
def a_slash_a_accent_phrases():
Expand Down Expand Up @@ -111,15 +103,13 @@ def a_slash_a_accent_phrases():
text="ア'/ア'",
enable_interrogative=False,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False, False],
)

expected_accent_phrases = a_slash_a_accent_phrases()
self._interrogative_accent_phrase_marks_base(
text="ア'/ア'",
enable_interrogative=True,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False, False],
)

def a_jp_comma_a_accent_phrases():
Expand Down Expand Up @@ -166,15 +156,13 @@ def a_jp_comma_a_accent_phrases():
text="ア'、ア'",
enable_interrogative=False,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False, False],
)

expected_accent_phrases = a_jp_comma_a_accent_phrases()
self._interrogative_accent_phrase_marks_base(
text="ア'、ア'",
enable_interrogative=True,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False, False],
)

def a_slash_a_slash_a_slash_a_slash_a_accent_phrases():
Expand Down Expand Up @@ -256,26 +244,12 @@ def a_slash_a_slash_a_slash_a_slash_a_accent_phrases():
text="ア'/ア'/ア'/ア'/ア'",
enable_interrogative=False,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[
False,
False,
False,
False,
False,
],
)
expected_accent_phrases = a_slash_a_slash_a_slash_a_slash_a_accent_phrases()
self._interrogative_accent_phrase_marks_base(
text="ア'/ア'/ア'/ア'/ア'",
enable_interrogative=True,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[
False,
False,
False,
False,
False,
],
)

def su_accent_phrases():
Expand All @@ -301,14 +275,12 @@ def su_accent_phrases():
text="ス'",
enable_interrogative=False,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False],
)
expected_accent_phrases = su_accent_phrases()
self._interrogative_accent_phrase_marks_base(
text="ス'",
enable_interrogative=True,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False],
)

def under_score_su_accent_phrases():
Expand All @@ -334,15 +306,13 @@ def under_score_su_accent_phrases():
text="_ス'",
enable_interrogative=False,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False],
)

expected_accent_phrases = under_score_su_accent_phrases()
self._interrogative_accent_phrase_marks_base(
text="_ス'",
enable_interrogative=True,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False],
)

def gye_accent_phrases():
Expand All @@ -368,15 +338,13 @@ def gye_accent_phrases():
text="ギェ'",
enable_interrogative=False,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False],
)

expected_accent_phrases = gye_accent_phrases()
self._interrogative_accent_phrase_marks_base(
text="ギェ'",
enable_interrogative=True,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False],
)

def gye_gye_gye_accent_phrases():
Expand Down Expand Up @@ -437,15 +405,13 @@ def gye_gye_gye_accent_phrases():
text="ギェ'、ギェ'/ギェ'",
enable_interrogative=False,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False, False, False],
)

expected_accent_phrases = gye_gye_gye_accent_phrases()
self._interrogative_accent_phrase_marks_base(
text="ギェ'、ギェ'/ギェ'",
enable_interrogative=True,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False, False, False],
)

def a_question_mark_accent_phrases():
Expand All @@ -471,11 +437,11 @@ def a_question_mark_accent_phrases():
text="ア'?",
enable_interrogative=False,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False],
)

expected_accent_phrases = a_question_mark_accent_phrases()
expected_accent_phrases[0].moras.append(
expected_accent_phrases[-1].is_interrogative = True
expected_accent_phrases[-1].moras.append(
Mora(
text="ア",
consonant=None,
Expand All @@ -489,7 +455,6 @@ def a_question_mark_accent_phrases():
text="ア'?",
enable_interrogative=True,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[True],
)

def gye_gye_gye_question_mark_accent_phrases():
Expand Down Expand Up @@ -550,10 +515,10 @@ def gye_gye_gye_question_mark_accent_phrases():
text="ギェ'、ギェ'/ギェ'?",
enable_interrogative=False,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False, False, False],
)

expected_accent_phrases = gye_gye_gye_question_mark_accent_phrases()
expected_accent_phrases[-1].is_interrogative = True
expected_accent_phrases[-1].moras.append(
Mora(
text="エ",
Expand All @@ -568,7 +533,6 @@ def gye_gye_gye_question_mark_accent_phrases():
text="ギェ'、ギェ'/ギェ'?",
enable_interrogative=True,
expected_accent_phrases=expected_accent_phrases,
expected_interrogative_accent_phrase_marks=[False, False, True],
)


Expand Down Expand Up @@ -601,3 +565,137 @@ def test_exceptions(self):
with self.assertRaises(ParseKanaError) as err:
kana_parser.parse_kana("ア?ア'", True)
self.assertEqual(err.exception.errcode, ParseKanaErrorCode.UNKNOWN_TEXT)


class TestCreateKana(TestCase):
def test_create_kana_interrogative(self):
def koreha_arimasuka_accent_phrases():
return [
AccentPhrase(
moras=[
Mora(
text="コ",
consonant="k",
consonant_length=2.5,
vowel="o",
vowel_length=2.5,
pitch=2.5,
),
Mora(
text="レ",
consonant="r",
consonant_length=2.5,
vowel="e",
vowel_length=2.5,
pitch=2.5,
),
Mora(
text="ワ",
consonant="w",
consonant_length=2.5,
vowel="a",
vowel_length=2.5,
pitch=2.5,
),
],
accent=3,
pause_mora=None,
is_interrogative=False,
),
AccentPhrase(
moras=[
Mora(
text="ア",
consonant=None,
consonant_length=None,
vowel="a",
vowel_length=2.5,
pitch=2.5,
),
Mora(
text="リ",
consonant="r",
consonant_length=2.5,
vowel="i",
vowel_length=2.5,
pitch=2.5,
),
Mora(
text="マ",
consonant="m",
consonant_length=2.5,
vowel="a",
vowel_length=2.5,
pitch=2.5,
),
Mora(
text="ス",
consonant="s",
consonant_length=2.5,
vowel="U",
vowel_length=2.5,
pitch=2.5,
),
Mora(
text="カ",
consonant="k",
consonant_length=2.5,
vowel="a",
vowel_length=2.5,
pitch=2.5,
),
Mora(
text="ア",
consonant=None,
consonant_length=None,
vowel="a",
vowel_length=2.5,
pitch=2.5,
),
],
accent=3,
pause_mora=None,
is_interrogative=False,
),
]

accent_phrases = koreha_arimasuka_accent_phrases()
self.assertEqual(create_kana(accent_phrases), "コレワ'/アリマ'_スカア")

accent_phrases = koreha_arimasuka_accent_phrases()
accent_phrases[-1].is_interrogative = True
self.assertEqual(create_kana(accent_phrases), "コレワ'/アリマ'_スカ?")

def kya_accent_phrases():
return [
AccentPhrase(
moras=[
Mora(
text="キャ",
consonant="ky",
consonant_length=2.5,
vowel="a",
vowel_length=2.5,
pitch=2.5,
),
Mora(
text="ッ",
consonant=None,
consonant_length=None,
vowel="cl",
vowel_length=0.1,
pitch=0,
),
],
accent=1,
pause_mora=None,
is_interrogative=False,
),
]

accent_phrases = kya_accent_phrases()
self.assertEqual(create_kana(accent_phrases), "キャ'ッ")

accent_phrases = kya_accent_phrases()
accent_phrases[-1].is_interrogative = True
self.assertEqual(create_kana(accent_phrases), "キャ'ッ")
Loading

0 comments on commit 5496881

Please sign in to comment.