"""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 = "Ndewo nụ, ụmụnna m. Tonight we will talk about the new market road and the school fees, ma ọ bụrụ na ego ezughị."
print(apply(t, "many")); print(apply(t, "few"))