Spaces:
Running
Running
Yurii Paniv
commited on
Commit
·
de21232
1
Parent(s):
dbebef0
Add autostress to single vowel words
Browse files
stress.py
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
from ukrainian_word_stress import Stressifier, StressSymbol
|
| 3 |
|
| 4 |
stressify = Stressifier(stress_symbol=StressSymbol.CombiningAcuteAccent)
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def stress_dict(sentence: str):
|
| 8 |
-
stressed = stressify(sentence.replace("+", "")).replace(
|
|
|
|
|
|
|
| 9 |
new_stressed = ""
|
| 10 |
start = 0
|
| 11 |
last = 0
|
| 12 |
|
|
|
|
| 13 |
while True:
|
| 14 |
plus_position = stressed.find("+", start)
|
| 15 |
if plus_position != -1:
|
|
@@ -34,7 +43,21 @@ def sentence_to_stress(sentence: str, stress_function=stress_dict) -> str:
|
|
| 34 |
|
| 35 |
# add stress before vowel
|
| 36 |
new_stressed = stress_function(sentence)
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# replace already stressed words
|
| 39 |
if len(all_stresses) > 0:
|
| 40 |
words = new_stressed.split(" ")
|
|
|
|
| 1 |
+
from typing import List
|
| 2 |
import numpy as np
|
| 3 |
from ukrainian_word_stress import Stressifier, StressSymbol
|
| 4 |
|
| 5 |
stressify = Stressifier(stress_symbol=StressSymbol.CombiningAcuteAccent)
|
| 6 |
|
| 7 |
+
vowels = "аеєиіїоуюя"
|
| 8 |
+
consonants = "бвгґджзйклмнпрстфхцчшщь"
|
| 9 |
+
special = "'"
|
| 10 |
+
alphabet = vowels + consonants + special
|
| 11 |
+
|
| 12 |
|
| 13 |
def stress_dict(sentence: str):
|
| 14 |
+
stressed = stressify(sentence.replace("+", "")).replace(
|
| 15 |
+
StressSymbol.CombiningAcuteAccent, "+"
|
| 16 |
+
)
|
| 17 |
new_stressed = ""
|
| 18 |
start = 0
|
| 19 |
last = 0
|
| 20 |
|
| 21 |
+
# shift stress symbol by one "при+віт" -> "пр+ивіт"
|
| 22 |
while True:
|
| 23 |
plus_position = stressed.find("+", start)
|
| 24 |
if plus_position != -1:
|
|
|
|
| 43 |
|
| 44 |
# add stress before vowel
|
| 45 |
new_stressed = stress_function(sentence)
|
| 46 |
+
|
| 47 |
+
# stress single vowel words
|
| 48 |
+
new_list: List[str] = new_stressed.split(" ")
|
| 49 |
+
for word_index in range(0, len(new_list)):
|
| 50 |
+
element = new_list[word_index]
|
| 51 |
+
vowels_in_words = list(map(lambda letter: letter in vowels, element.lower()))
|
| 52 |
+
if "+" in element:
|
| 53 |
+
continue
|
| 54 |
+
if vowels_in_words.count(True) == 0:
|
| 55 |
+
continue
|
| 56 |
+
elif vowels_in_words.count(True) == 1:
|
| 57 |
+
vowel_index = vowels_in_words.index(True)
|
| 58 |
+
new_list[word_index] = element[0:vowel_index] + "+" + element[vowel_index::]
|
| 59 |
+
new_stressed = " ".join(new_list)
|
| 60 |
+
|
| 61 |
# replace already stressed words
|
| 62 |
if len(all_stresses) > 0:
|
| 63 |
words = new_stressed.split(" ")
|