86 lines
5.0 KiB
Python
86 lines
5.0 KiB
Python
"""Voice-only text fixes for the notes: pre() on the plain text, post() on the RUAccent '+' text.
|
||
|
||
The screen text and the subtitles never see these. Add a word here, re-run `segments`, and only the
|
||
segments whose text changed are rendered and checked again.
|
||
|
||
- spelling: Latin look-alikes, stray symbols, apostrophe names (Кра'Тау -> Кра Тау);
|
||
- STRESS: game names by their English stress, keyed by a stem regex; the value is the index of the
|
||
stressed vowel inside the word (0 = first). Existing '+' in a matched word is replaced;
|
||
- words with two '+' in one part (Фир+аж+а) keep the mark the lexicon gives or else the last one.
|
||
"""
|
||
import re
|
||
|
||
VOW = "аеёиоуыэюяАЕЁИОУЫЭЮЯ"
|
||
|
||
PRE = [ # plain text, before RUAccent
|
||
(r"(?<=[А-Яа-яЁё ])c(?=[ А-Яа-яЁё])", "с"), (r"(?<=[А-Яа-яЁё ])C(?=[ а-яё])", "С"),
|
||
(r"»>", "»"), (r">", ""),
|
||
(r"Пыш'Ка", "Пышка"), (r"Гр[уy]ул", "Грул"),
|
||
# abbreviations and the numbers around them (the only cases in the notes)
|
||
(r"ок\. одна тысяча двести мм", "около тысячи двухсот миллиметров"), (r"ок\. семьсот мм", "около семисот миллиметров"),
|
||
(r"ок\. триста пятьдесят мм", "около трёхсот пятидесяти миллиметров"), (r"ок\. три тысячи мм", "около трёх тысяч миллиметров"),
|
||
(r"от пятьсот мм", "от пятисот миллиметров"), (r"до две тысячи мм", "до двух тысяч миллиметров"),
|
||
(r"свыше одна тысяча лет", "свыше тысячи лет"), (r"\bиз сто\b", "из ста"), (r"\bот сто\b", "от ста"),
|
||
(r"\bмм\b", "миллиметров"), (r"\bв т\.\s?ч\.:?", "в том числе"), (r"\(см\. ", "(смотри "), (r"\bсм\. ниже", "смотри ниже"),
|
||
(r"([А-ЯЁа-яё]+)'([А-ЯЁа-яё]+)", lambda m: m.group(1) + " " + m.group(2).lower()
|
||
if m.group(1).lower().startswith("драк") else m.group(1) + " " + m.group(2)),
|
||
]
|
||
|
||
# stem regex (whole word, case-insensitive, may carry endings) -> stressed vowel index
|
||
STRESS = {
|
||
r"воргот\w*": 0, r"пайкмид\w*": 0, r"эмбервэйл\w*": 0, r"римгард\w*": 0, r"мелдор\w*": 0,
|
||
r"морго": 0, r"лэнсхарт\w*": 0, r"гленвудз": 0, r"констанцио": 1, r"мейерс\w*": 0,
|
||
r"лэпис\w*": 0, r"хэрриот\w*": 0, r"тау": 0, r"тул": 0, r"раш\w*": 0, r"ках\w*": 0,
|
||
r"конвэ\w*": 0, r"герит\w*": 0, r"эндрю": 0, r"лахлан\w*": 0, r"бриттлбуш\w*": 0,
|
||
r"лонгкип\w*": 0, r"аркрайт\w*": 0, r"вилли": 0, r"орвил\w*": 0, r"пышка": 0,
|
||
r"уайтуинд\w*": 0, r"харпер\w*": 0, r"везт\w*": 0, r"стилуотер": 0, r"голдрок": 0,
|
||
r"арлингтон\w*": 0, r"абрахам\w*": 0, r"дрифтвуд": 0, r"мильтон\w*": 0, r"мелвил\w*": 0,
|
||
r"итан\w*": 0, r"софи": 0, r"рьель": 1, r"фираж\w*": 1, r"грул": 0, r"рукмор\w*": 0,
|
||
r"фэйрберн\w*": 0, r"майлз\w*": 0, r"дэвис\w*": 0, r"келвин\w*": 0, r"честер\w*": 0,
|
||
r"сайрус\w*": 0, r"коннор\w*": 0, r"каспер\w*": 0, r"костнер\w*": 0, r"мейнард\w*": 0,
|
||
r"эксли": 0, r"пауэлл\w*": 0, r"брайтвич\w*": 0, r"димвельд\w*": 0, r"лорэйн\w*": 1,
|
||
r"ирветт\w*": 1, r"иоланд\w*": 1, r"рэттблик\w*": 0, r"кэйден\w*": 0, r"дэниэл\w*": 0,
|
||
}
|
||
_ST = [(re.compile(r"^" + k + r"$", re.I), v) for k, v in STRESS.items()]
|
||
|
||
|
||
def pre(text: str) -> str:
|
||
for a, b in PRE:
|
||
text = re.sub(a, b, text)
|
||
return text
|
||
|
||
|
||
def _mark(word: str, k: int) -> str:
|
||
out, n = [], 0
|
||
for ch in word:
|
||
if ch in VOW:
|
||
if n == k:
|
||
out.append("+")
|
||
n += 1
|
||
out.append(ch)
|
||
return "".join(out)
|
||
|
||
|
||
def post(plus: str) -> str:
|
||
"""Fix stresses in RUAccent '+' text: lexicon names, then double marks."""
|
||
def fix_part(p):
|
||
clean = p.replace("+", "")
|
||
for rx, k in _ST:
|
||
if rx.match(clean):
|
||
return _mark(clean, k)
|
||
if p.count("+") > 1:
|
||
return _mark(clean, sum(1 for ch in p[:p.rindex("+")] if ch in VOW))
|
||
return p
|
||
|
||
def fix_word(m):
|
||
return "-".join(fix_part(p) for p in m.group(0).split("-"))
|
||
return re.sub(r"[А-ЯЁа-яё+]+(?:-[А-ЯЁа-яё+]+)*", fix_word, plus)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
import sys
|
||
sys.stdout.reconfigure(encoding="utf-8")
|
||
for s in ["Кр+а'Т+ау идёт в Пайкм+идз-Рич", "Фир+аж+а и Л+онгк+ип", "Хэрри+от и Конв+эй", "Стилуот+ер-б+эй"]:
|
||
print(s, "->", post(s))
|
||
print(pre("Кра'Тау и драк'дал вернулся c разведки «Брэйлин»>, Пыш'Ка"))
|