File size: 7,008 Bytes
9844a46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""

Text processing utilities for TTS inference

"""

import re
import numpy as np
from pathlib import Path
from typing import List, Dict


class TextProcessor:
    """Handles text processing operations"""
    
    def __init__(self, vocab_path: str):
        self.vocab_char_map = self._load_vocab(vocab_path)
        self.vocab_size = len(self.vocab_char_map)
    
    def _load_vocab(self, vocab_path: str) -> Dict[str, int]:
        """Load vocabulary mapping from file"""
        if not Path(vocab_path).exists():
            raise FileNotFoundError(f"Vocabulary file not found: {vocab_path}")
            
        vocab_char_map = {}
        with open(vocab_path, "r", encoding="utf-8") as f:
            for i, char in enumerate(f):
                vocab_char_map[char.rstrip('\n')] = i
        return vocab_char_map
    
    def text_to_indices(self, texts: List[List[str]]) -> np.ndarray:
        """Convert text to indices using vocabulary mapping"""
        get_idx = self.vocab_char_map.get
        list_idx_tensors = [
            np.array([get_idx(c, 0) for c in text], dtype=np.int32) 
            for text in texts
        ]
        return np.stack(list_idx_tensors, axis=0)
    
    def calculate_text_length(self, text: str, pause_punc: str) -> int:
        """Calculate text length including pause punctuation weighting"""
        return len(text.encode('utf-8')) + 3 * len(re.findall(pause_punc, text))
    
    def clean_text(self, text: str) -> str:
        """Clean text to keep only readable characters"""
        # only keep readable characters in alphabet, vietnamese characters, space, punctuation
        alphabet_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        vietnamese_chars = "àáảãạăằắẳẵặâầấẩẫậèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳỵỷỹýỳỵỷỹ"
        punctuation_chars = " .,!?'@$%&/:;()"
        all_valid_chars = alphabet_chars + alphabet_chars.upper() + vietnamese_chars + vietnamese_chars.upper() + punctuation_chars
        all_valid_chars = list(set(all_valid_chars))
        
        # replace all invalid characters with space using all_valid_chars and regex
        if "\n" in text:
            chunks = [chunk.strip() for chunk in text.split("\n") if chunk.strip()]
            for idx, chunk in enumerate(chunks):
                if not chunk.strip().endswith("."):
                    chunks[idx] = chunk.strip() + "."
            text = " ".join(chunks)

        text = re.sub(f"[^{''.join(all_valid_chars)}]", " ", text)
        text = text.strip()
        # replace ;:() with ,
        text = re.sub(r'[;:()]', ',', text)
        
        # make sure no duplicate ,.
        text = re.sub(r'\.+', '.', text)
        text = re.sub(r',+', ',', text)
        text = re.sub(r'\s+', ' ', text)
        
        # Append . at the end of the text if it doesn't end with . or ? or ! or ,
        if not text.endswith(('.', '?', '!', ',')):
            text += '.'
        
        return text
    
    def chunk_text(self, text: str, max_chars: int = 135) -> List[str]:
        """Split text into chunks with maximum character limit"""
        # Split text into sentences
        sentences = []
        
        # Split by .?!
        for s in re.split(r'(?<=[.!?]) +', text.strip()):
            s = s.strip()
            if s:            
                if len(s) < max_chars:
                    sentences.append(s)
                    continue
                
                # split by commas
                for part in s.split(', '):
                    part = part.strip()
                    if part:
                        if not part.endswith(','):
                            part += ','
                            
                        # Make sure sentences are not too long, cut them if necessary
                        if len(part) < max_chars:
                            sentences.append(part)
                        else:
                            print(f"Warning: Part too long ({len(part)} chars), splitting further: {part[:50]}...")
                            num_parts = len(part) // max_chars + 1
                            part_length = len(part) // num_parts
                            for i in range(num_parts):
                                start = i * part_length
                                end = (i + 1) * part_length if i < num_parts - 1 else len(part)
                                sub_part = part[start:end].strip()
                                if sub_part:
                                    sentences.append(sub_part)
        
        if not sentences:
            return []
        
        chunks = []
        current_chunk = ""
        
        for sentence in sentences:
            # If adding this sentence would exceed max_chars
            if current_chunk and len(current_chunk + " " + sentence) > max_chars:
                # Save current chunk and start new one
                chunks.append(current_chunk.strip())
                current_chunk = sentence
            else:
                # Add sentence to current chunk
                if current_chunk:
                    current_chunk += " " + sentence
                else:
                    current_chunk = sentence
        
        # Add the last chunk if it exists
        if current_chunk:
            chunks.append(current_chunk.strip())
        
        # Post-process: merge very short chunks with adjacent ones
        final_chunks = []
        i = 0
        while i < len(chunks):
            current = chunks[i]
            
            # If chunk is very short (less than 4 words), try to merge
            if len(current.split()) < 4 and len(chunks) > 1:
                if i < len(chunks) - 1:
                    # Merge with next chunk if total doesn't exceed max_chars
                    next_chunk = chunks[i + 1]
                    merged = current + " " + next_chunk
                    if len(merged) <= max_chars:
                        final_chunks.append(merged)
                        i += 2  # Skip next chunk as it's been merged
                        continue
                elif i > 0 and final_chunks:
                    # Merge with previous chunk if total doesn't exceed max_chars
                    prev_chunk = final_chunks[-1]
                    merged = prev_chunk + " " + current
                    if len(merged) <= max_chars:
                        final_chunks[-1] = merged
                        i += 1
                        continue
            
            final_chunks.append(current)
            i += 1
        
        print(f"chunk_text: {len(final_chunks)} chunks: {[len(text) for text in final_chunks]}. Max chars: {max_chars}")
        return final_chunks