-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyllables.py
More file actions
47 lines (36 loc) · 1.33 KB
/
Copy pathsyllables.py
File metadata and controls
47 lines (36 loc) · 1.33 KB
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
from __future__ import annotations
"""Utilities for counting syllables in lyric lines."""
from typing import Set
# Vowel sets and detection helpers are defined here to keep syllable analysis
# independent from the UI layer. Для русского и украинского языков количество
# слогов равно количеству гласных. Для остальных языков пользователь должен
# размечать слоги вручную специальным символом ("|").
CYRILLIC_VOWELS: Set[str] = {
"а",
"е",
"ё",
"є",
"и",
"і",
"ї",
"о",
"у",
"ы",
"э",
"ю",
"я",
}
def normalize_text(value: str) -> str:
"""Return a normalized version of the input for syllable counting."""
return (value or "").strip().lower()
def count_vowels(text: str) -> int:
"""Count Russian/Ukrainian vowels in the provided text."""
return sum(1 for char in text if char in CYRILLIC_VOWELS)
def count_syllables(line: str) -> int:
"""Calculate syllable count for a line using automatic detection."""
raw = line or ""
if "|" in raw:
segments = [segment.strip() for segment in raw.split("|") if segment.strip()]
return len(segments)
cleaned = normalize_text(raw)
return count_vowels(cleaned)