Yurii Paniv commited on
Commit
1db5fd0
1 Parent(s): 36d9785

#14 Fix one-vowel stress

Browse files
Files changed (2) hide show
  1. tests/test_stress.py +3 -2
  2. ukrainian_tts/stress.py +19 -5
tests/test_stress.py CHANGED
@@ -3,8 +3,9 @@ from ukrainian_tts.stress import sentence_to_stress, stress_with_model
3
 
4
  def test_stress_table():
5
  examples = [
6
- #("Бабин біб розцвів у дощ — Буде бабі біб у борщ.\n\nБоронила", "Б+абин б+іб розцв+ів +у д+ощ — Б+уде б+абі б+іб +у б+орщ.\n\nБорон+ила"),
7
- #("Бабин біб розцвів у дощ — Буде бабі біб у борщ.,,Боронила", "Б+абин б+іб розцв+ів +у д+ощ — Б+уде б+абі б+іб +у б+орщ.,,Борон+ила"),
 
8
  ("Бобер на березі з бобренятами бублики пік.","Боб+ер н+а березі з бобрен+ятами б+ублики п+ік."),
9
  (
10
  "Кам'янець-Подільський - місто в Хмельницькій області України, центр Кам'янець-Подільської міської об'єднаної територіальної громади і Кам'янець-Подільського району.",
3
 
4
  def test_stress_table():
5
  examples = [
6
+ ("Бабин біб розцвів у дощ — Буде бабі біб у борщ.\n\nБоронила", "Б+абин б+іб розцв+ів +у д+ощ — Б+уде б+абі б+іб +у б+орщ.\n\nБорон+ила"),
7
+ ("Бабин біб розцвів у дощ — Буде бабі біб у борщ.,,Боронила", "Б+абин б+іб розцв+ів +у д+ощ — Б+уде б+абі б+іб +у б+орщ.,,Борон+ила"),
8
+ ("Бабин біб розцвів у дощ — Буде бабі біб у борщ.\n\n", "Б+абин б+іб розцв+ів +у д+ощ — Б+уде б+абі б+іб +у б+орщ.\n\n"),
9
  ("Бобер на березі з бобренятами бублики пік.","Боб+ер н+а березі з бобрен+ятами б+ублики п+ік."),
10
  (
11
  "Кам'янець-Подільський - місто в Хмельницькій області України, центр Кам'янець-Подільської міської об'єднаної територіальної громади і Кам'янець-Подільського району.",
ukrainian_tts/stress.py CHANGED
@@ -7,7 +7,7 @@ stressify = Stressifier(stress_symbol=StressSymbol.CombiningAcuteAccent)
7
  vowels = "аеєиіїоуюя"
8
  consonants = "бвгґджзйклмнпрстфхцчшщь"
9
  special = "'-"
10
- alphabet = vowels + consonants + special
11
 
12
 
13
  def _shift_stress(stressed):
@@ -46,7 +46,6 @@ def stress_dict(sentence: str):
46
  def sentence_to_stress(sentence: str, stress_function=stress_dict) -> str:
47
  # save custom stress positions
48
  all_stresses = []
49
- sentence = sentence.replace("\n", " ")
50
  orig_words = sentence.split(" ")
51
  for i in range(0, len(orig_words)):
52
  if "+" in orig_words[i]:
@@ -56,7 +55,22 @@ def sentence_to_stress(sentence: str, stress_function=stress_dict) -> str:
56
  new_stressed = stress_function(sentence)
57
 
58
  # stress single vowel words
59
- new_list: List[str] = new_stressed.split(" ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  for word_index in range(0, len(new_list)):
61
  element = new_list[word_index]
62
  vowels_in_words = list(map(lambda letter: letter in vowels, element.lower()))
@@ -67,7 +81,7 @@ def sentence_to_stress(sentence: str, stress_function=stress_dict) -> str:
67
  elif vowels_in_words.count(True) == 1:
68
  vowel_index = vowels_in_words.index(True)
69
  new_list[word_index] = element[0:vowel_index] + "+" + element[vowel_index::]
70
- new_stressed = " ".join(new_list)
71
 
72
  # replace already stressed words
73
  if len(all_stresses) > 0:
@@ -75,4 +89,4 @@ def sentence_to_stress(sentence: str, stress_function=stress_dict) -> str:
75
  for stressed in all_stresses:
76
  words[stressed] = orig_words[stressed]
77
  return " ".join(words)
78
- return new_stressed
7
  vowels = "аеєиіїоуюя"
8
  consonants = "бвгґджзйклмнпрстфхцчшщь"
9
  special = "'-"
10
+ alphabet = vowels + consonants + special + "+"
11
 
12
 
13
  def _shift_stress(stressed):
46
  def sentence_to_stress(sentence: str, stress_function=stress_dict) -> str:
47
  # save custom stress positions
48
  all_stresses = []
 
49
  orig_words = sentence.split(" ")
50
  for i in range(0, len(orig_words)):
51
  if "+" in orig_words[i]:
55
  new_stressed = stress_function(sentence)
56
 
57
  # stress single vowel words
58
+ new_list = []
59
+ # if letter is not in alphabet, then consider it an end of the word
60
+ previous = 0
61
+ for i, letter in enumerate(new_stressed):
62
+ if letter.lower() not in alphabet:
63
+ if previous == i:
64
+ new_list.append(new_stressed[i])
65
+ else:
66
+ new_list.append(new_stressed[previous:i])
67
+ new_list.append(new_stressed[i])
68
+ previous = i + 1
69
+ # add remainder
70
+ if previous != len(new_stressed):
71
+ new_list.append(new_stressed[previous:])
72
+
73
+ # add stress to single-vowel words
74
  for word_index in range(0, len(new_list)):
75
  element = new_list[word_index]
76
  vowels_in_words = list(map(lambda letter: letter in vowels, element.lower()))
81
  elif vowels_in_words.count(True) == 1:
82
  vowel_index = vowels_in_words.index(True)
83
  new_list[word_index] = element[0:vowel_index] + "+" + element[vowel_index::]
84
+ new_stressed = "".join(new_list)
85
 
86
  # replace already stressed words
87
  if len(all_stresses) > 0:
89
  for stressed in all_stresses:
90
  words[stressed] = orig_words[stressed]
91
  return " ".join(words)
92
+ return new_stressed