MCPcopy Create free account
hub / github.com/FireRedTeam/FireRedTTS / preprocess_text

Function preprocess_text

fireredtts/modules/text_normalizer/normalize.py:15–65  ·  view source on GitHub ↗
(sentence)

Source from the content-addressed store, hash-verified

13
14
15def preprocess_text(sentence):
16 # preprocessing
17 sentence = bytes(sentence, "utf-8").decode("utf-8", "ignore")
18 sentence = regex.sub("[\p{Cf}--[\u200d]]", "", sentence, flags=regex.V1)
19 sentence = regex.sub("\p{Co}", "", sentence)
20 sentence = sentence.replace("\u00a0", " ")
21 sentence = sentence.replace("\ufffd", "")
22 sentence = regex.sub("\p{Zl}", "\n", sentence)
23 sentence = regex.sub("\p{Zp}", "\n", sentence)
24
25 sentence = unicode(sentence)
26 sentence = "".join(
27 char
28 for char in unicodedata.normalize("NFD", sentence)
29 if unicodedata.category(char) != "Mn"
30 ) # Strip accents
31
32 sentence = strip_kaomoji(sentence)
33 # full to half with exemption (to be converted after number TN): 。,:
34 sentence = f2b(sentence, exemption="。,:")
35
36 # clean spaces
37 sentence = sentence.replace("\n", ",")
38 sentence = sentence.replace("\t", ",")
39 sentence = sentence.replace("\r", ",")
40 sentence = re.sub(r"[。.]{3,}", "…", sentence)
41 sentence = re.sub(r"[…⋯]{1,}", "…", sentence)
42 sentence = re.sub(r"[ ]+", " ", sentence)
43 sentence = sentence.strip()
44
45 # punctuation reduction
46 result = ""
47 for idx, char in enumerate(sentence):
48 if char in symbol_reduction:
49 char = symbol_reduction[char]
50
51 if char == " ":
52 if idx == 0:
53 continue
54 if is_chinese(sentence[idx + 1]) and (
55 is_chinese(sentence[idx - 1]) or sentence[idx - 1] in '") '
56 ):
57 result += ","
58 else:
59 result += " "
60 continue
61
62 if is_valid_char(char):
63 result += char
64 result = re.sub(r"[ ]+", " ", result)
65 return result
66
67
68def rettt(sentence):

Callers 1

tnMethod · 0.85

Calls 5

strip_kaomojiFunction · 0.85
f2bFunction · 0.85
is_chineseFunction · 0.85
is_valid_charFunction · 0.85
decodeMethod · 0.45

Tested by

no test coverage detected