File size: 1,614 Bytes
582aab9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Text-side pause control (pipeline step): 'many pauses' -> insert the model's trained [pause] tag at commas and
every ~5 words inside long phrases; 'few pauses' -> drop commas/semicolons and [pause] tags inside sentences."""
import re
TAGRE = re.compile(r"(<\w+>)(.*?)(</\w+>)", re.S)


def _many(s, every=5):
    out = []
    for sent in re.split(r"(?<=[.!?])\s+", s.strip()):
        parts = re.split(r"(,|;|:)", sent)
        buf = []
        for p in parts:
            if p in (",", ";", ":"):
                buf.append(p + " [pause]"); continue
            w = p.split(); chunk = []
            for i, x in enumerate(w):
                chunk.append(x)
                if (i + 1) % every == 0 and i + 1 < len(w) - 1:
                    chunk.append("[pause]")
            buf.append(" ".join(chunk))
        out.append(" ".join(b for b in buf if b).replace(" ,", ",").replace(" ;", ";"))
    return " [pause] ".join(out) if len(out) > 1 else out[0]


def _few(s):
    s = s.replace("[pause]", " ").replace("[breath]", " ")
    s = re.sub(r"\s*[,;:]\s*", " ", s)
    return " ".join(s.split())


def apply(text, mode):
    f = _many if mode == "many" else _few if mode == "few" else None
    if f is None:
        return text
    if TAGRE.search(text):
        return TAGRE.sub(lambda m: m.group(1) + f(m.group(2)) + m.group(3), text)
    return f(text)


if __name__ == "__main__":
    t = "<ibo>Ndewo nụ, ụmụnna m.</ibo> <eng>Tonight we will talk about the new market road and the school fees,</eng> <ibo>ma ọ bụrụ na ego ezughị.</ibo>"
    print(apply(t, "many")); print(apply(t, "few"))