-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit_text_manager.py
51 lines (40 loc) · 1.5 KB
/
split_text_manager.py
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
44
45
46
47
48
49
50
51
max_silero_text_length = 140
def split_text_into_chunks(text, delimiters):
output = []
current_sentence = ""
for n in text:
current_sentence += n
if n in delimiters:
output.append(current_sentence.strip())
current_sentence = ""
if len(current_sentence) > 0:
output.append(current_sentence.strip())
output = [n for n in output if len(n) > 0]
return output
def split_text_array(text_array, delimiters):
output_array = []
for n in text_array:
if len(n) > max_silero_text_length:
output_array.extend(split_text_into_chunks(n, delimiters))
else:
output_array.append(n)
output_array = [n for n in output_array if len(n) > 0]
return output_array
def split_text(text):
if len(text) < max_silero_text_length:
return [text]
text_array = split_text_into_chunks(text, [".", "?", "!", "\n"])
text_array = split_text_array(text_array, [",", "-", "—", ":"])
text_array = split_text_array(text_array, [" "])
output_array = []
current_text = ""
for n in text_array:
if len(" ".join([current_text, n])) < max_silero_text_length:
current_text = " ".join([current_text, n])
else:
output_array.append(current_text)
current_text = n
if current_text:
output_array.append(current_text)
output_array = [x.strip() for x in output_array]
return output_array