Views
No views yet
cotovia_0.5_amd64.deb and cotovia-lang-gl_0.5_all.deb, which can be installed using the following commands:1sudo dpkg -i cotovia_0.5_amd64.deb
2sudo dpkg -i cotovia-lang-gl_0.5_all.debpip install matcha-tts1import argparse
2import random
3import re
4import string
5import subprocess
6
7PUNCLIST = [';', '?', '¿', ',', ':', '.', '!', '¡']
8
9def sanitize_filename(filename):
10 """Remove or replace any characters that are not allowed in file names."""
11 return ''.join(c for c in filename if c.isalnum() or c in (' ', '_', '-')).rstrip()
12
13def canBeNumber(n):
14 try:
15 int(n)
16 return True
17 except ValueError:
18 # Not a number
19 return False
20
21def is_number(index, text):
22 if index == 0:
23 return False
24 elif index == len(text) - 1:
25 return False
26 else:
27 return canBeNumber(text[index - 1]) and canBeNumber(text[index + 1])
28
29def split_punc(text):
30 segments = []
31 puncs = []
32 curr_seg = ""
33 previous_punc = False
34
35 for i, c in enumerate(text):
36 if c in PUNCLIST and not previous_punc and not is_number(i, text):
37 segments.append(curr_seg.strip())
38 puncs.append(c)
39 curr_seg = ""
40 previous_punc = True
41 elif c in PUNCLIST and previous_punc:
42 puncs[-1] += c
43 else:
44 curr_seg += c
45 previous_punc = False
46
47 segments.append(curr_seg.strip())
48
49 #Remove empty segments in the list
50 segments = filter(None, segments)
51
52 # store segments as a list
53 segments = list(segments)
54
55 return segments, puncs
56
57def remove_tra3_tags(phontrans):
58 s = re.sub(r'#(.+?)#', r'', phontrans)
59 s = re.sub(r'%(.+?)%', r'', s)
60 s = re.sub(' +',' ',s)
61 s = re.sub('-','',s)
62 return s.strip()
63
64def to_cotovia(text_segments):
65 # Input and output Cotovía files
66 res = ''.join(random.choices(string.ascii_lowercase + string.digits, k=5))
67 COTOVIA_IN_TXT_PATH = res + '.txt'
68 COTOVIA_IN_TXT_PATH_ISO = 'iso8859-1' + res + '.txt'
69 COTOVIA_OUT_PRE_PATH = 'iso8859-1' + res + '.tra'
70 COTOVIA_OUT_PRE_PATH_UTF8 = 'utf8' + res + '.tra'
71
72 with open(COTOVIA_IN_TXT_PATH, 'w') as f:
73 for seg in text_segments:
74 if seg:
75 f.write(seg + '\n')
76 else:
77 f.write(',' + '\n')
78
79 # utf-8 to iso8859-1
80 subprocess.run(["iconv", "-f", "utf-8", "-t", "iso8859-1", COTOVIA_IN_TXT_PATH, "-o", COTOVIA_IN_TXT_PATH_ISO], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
81 # call cotovia with -t3 option
82 subprocess.run(["cotovia", "-i", COTOVIA_IN_TXT_PATH_ISO, "-t3", "-n"], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
83 # iso8859-1 to utf-8
84 subprocess.run(["iconv", "-f", "iso8859-1", "-t", "utf-8", COTOVIA_OUT_PRE_PATH, "-o", COTOVIA_OUT_PRE_PATH_UTF8], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
85
86 segs = []
87 try:
88 with open(COTOVIA_OUT_PRE_PATH_UTF8, 'r') as f:
89 segs = [line.rstrip() for line in f]
90 segs = [remove_tra3_tags(line) for line in segs]
91 except:
92 print("ERROR: Couldn't read cotovia output")
93
94 subprocess.run(["rm", COTOVIA_IN_TXT_PATH, COTOVIA_IN_TXT_PATH_ISO, COTOVIA_OUT_PRE_PATH, COTOVIA_OUT_PRE_PATH_UTF8], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
95
96 return segs
97
98def merge_punc(text_segs, puncs):
99 merged_str = ""
100
101 for i, seg in enumerate(text_segs):
102 merged_str += seg + " "
103 if i < len(puncs):
104 merged_str += puncs[i] + " "
105
106 # remove spaces before , . ! ? ; : ) ] of the merged string
107 merged_str = re.sub(r"\s+([.,!?;:)\]])", r"\1", merged_str)
108
109 # remove spaces after ( [ ¡ ¿ of the merged string
110 merged_str = re.sub(r"([\(\[¡¿])\s+", r"\1", merged_str)
111
112 return merged_str.strip()
113
114
115def accent_convert(phontrans):
116 transcript = re.sub('a\^','á',phontrans)
117 transcript = re.sub('e\^','é',transcript)
118 transcript = re.sub('i\^','í',transcript)
119 transcript = re.sub('o\^','ó',transcript)
120 transcript = re.sub('u\^','ú',transcript)
121 transcript = re.sub('E\^','É',transcript)
122 transcript = re.sub('O\^','Ó',transcript)
123 return transcript
124
125
126def text_preprocess(text):
127 #Split from punc
128 text_segments, puncs = split_punc(text)
129 cotovia_phon_segs = to_cotovia(text_segments)
130 cotovia_phon_str = merge_punc(cotovia_phon_segs, puncs)
131 phon_str = accent_convert(cotovia_phon_str)
132 return phon_str
133
134
135def main():
136 parser = argparse.ArgumentParser(description='Cotovia phoneme transcription.')
137 parser.add_argument('text', type=str, help='Text to synthetize')
138 parser.add_argument('model_path', type=str, help='Absolute path to the model checkpoint.pth')
139 parser.add_argument('config_path', type=str, help='Absolute path to the model config.json')
140
141 args = parser.parse_args()
142
143 print("Text before preprocessing: ", args.text)
144 text = text_preprocess(args.text)
145 print("Text after preprocessing: ", text)
146
147if __name__ == "__main__":
148 main()normalize.py, avaliable in this repository. You can use this script to obtain the preprocessed text from an input text as follows:python normalize.py text| Hyperparameter | Value |
|---|---|
| Model | matcha |
| Batch Size | 32 |
| Mixed Precision | true |
| Hop Length | 256 |
| Optimizer | adam |
| Learning Rate Gen | 0.0001 |