File size: 13,276 Bytes
9c737ff | 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | # Copyright 2026 Jakub Sykała
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Luna Tokenizer
#
# The 9 Features
#--------------------------------------------------------------------------
# 0: syllable_id - Unique syllable identifier
# 1: onset_id - Initial consonant cluster (e.g., "str" in "string")
# 2: nucleus_id - Vowel core (e.g., "i" in "string")
# 3: coda_id - Final consonants (e.g., "ng" in "string")
# 4: position - Position in word (0=mid, 1=start, 2=end, 3=both)
# 5: is_capitalized - Starts with uppercase? (0 or 1)
# 6: token_type - 0=syllable, 1=number, 2=punctuation, 3=special
# 7: has_space_after - Space follows this token? (0 or 1)
# 8: is_word_end - Last syllable of word? (0 or 1)
#--------------------------------------------------------------------------
import re
from typing import List, Dict, Tuple, Optional
try:
import pyphen
PYPHEN_AVAILABLE = True
except ImportError:
PYPHEN_AVAILABLE = False
print("Warning: pyphen not installed. Using basic syllabification.")
class LunaTokenizer:
"""
Luna Tokenizer - Phonetically-aware tokenization.
Converts text into 9-dimensional token representations based on
syllable structure (Onset-Nucleus-Coda) plus metadata features.
"""
# Regex patterns
WORD_PATTERN = re.compile(r"([a-zA-Z]+|[0-9]|[^\w\s]|\s+)")
def __init__(self):
# Initialize pyphen for syllabification
if PYPHEN_AVAILABLE:
self.syllabifier = pyphen.Pyphen(lang='en_US')
else:
self.syllabifier = None
# Vocabularies (built during encoding or loaded from vocab.json)
self.syllable_to_id: Dict[str, int] = {'<pad>': 0, '<unk>': 1}
self.id_to_syllable: Dict[int, str] = {0: '<pad>', 1:'<unk>'}
# Phonetic component vocabularies
self.onset_to_id: Dict[str, int] = {
'<pad>':0, '': 1, '<num>': 2, '<punct>': 3, '<special>': 4
}
self.nucleus_to_id: Dict[str, int] = {'<pad>': 0, '': 1}
self.coda_to_id: Dict[str, int] = {'<pad>': 0, '': 1}
def get_feature_names(self) -> List[str]:
"""Return ordered list of feature names (9 features)"""
return [
'syllable_id', # 0
'onset_id', # 1
'nucleus_id', # 2
'coda_id', # 3
'position', # 4
'is_capitalized', # 5
'token_type', # 6
'has_space_after', # 7
'is_word_end', # 8
]
def _syllabify(self, word:str) -> List[str]:
"""Split word into syllables."""
if not word:
return []
if self.syllabifier:
hyphenated = self.syllabifier.inserted(word.lower())
return hyphenated.split('-') if hyphenated else [word.lower()]
else:
# Basic fallback: Split on vowel boundries
return self._basic_syllabify(word.lower())
def _basic_syllabify(self, word: str) -> List[str]:
"""Basic syllabification fallback."""
vowels = set('aeiouy')
syllables = []
current = ""
for i, char in enumerate(word):
current += char
if char in vowels:
# Look ahead - if next char is consonant followed by vowel, split
if i + 2 < len(word) and word[i+1] not in vowels and word[i+2] in vowels:
syllables.append(current)
current = ""
if current:
if syllables:
syllables[-1] += current
else:
syllables.append(current)
return syllables if syllables else [word]
def _extract_onset_nucleus_coda(self, syllable: str) -> Tuple[str, str, str]:
"""
Extract Onset-Nucles-Coda from syllable.
Example: "string" -> onset="str", nucleus="i", coda="ng"
"""
syllable = syllable.lower()
vowels = set('aeiouy')
# Find nucleus (first vowel sequence)
nucleus_start = -1
nucleus_end = -1
for i, char in enumerate(syllable):
if char in vowels:
if nucleus_start == -1:
nucleus_start = i
nucleus_end = i + 1
elif nucleus_start != -1:
break
if nucleus_start == -1:
# No vowel - treat whole thing as onset
return syllable, '', ''
onset = syllable[:nucleus_start]
nucleus = syllable[nucleus_start:nucleus_end]
coda = syllable[nucleus_end:]
return onset, nucleus, coda
def _get_or_add_syllable(self, syllable:str) -> int:
"""Get syllable ID, adding to vocab if new."""
if syllable not in self.syllable_to_id:
idx = len(self.syllable_to_id)
self.syllable_to_id[syllable] = idx
self.id_to_syllable[idx] = syllable
return self.syllable_to_id[syllable]
def _get_or_add_onset(self, onset: str) -> int:
if onset not in self.onset_to_id:
self.onset_to_id[onset] = len(self.onset_to_id)
return self.onset_to_id[onset]
def _get_or_add_nucleus(self, nucleus: str) -> int:
if nucleus not in self.nucleus_to_id:
self.nucleus_to_id[nucleus] = len(self.nucleus_to_id)
return self.nucleus_to_id[nucleus]
def _get_or_add_coda(self, coda: str) -> int:
if coda not in self.coda_to_id:
self.coda_to_id[coda] = len(self.coda_to_id)
return self.coda_to_id[coda]
def _determine_token_type(self, text: str) -> int:
"""Determine token type: 0=syllable, 1=number, 2=punct, 3=special."""
if text.isdigit():
return 1
elif text in '.,!?;:\'"()-[]{}':
return 2
elif text.isalpha():
return 0
else:
return 3
def encode(self, text: str) -> List[Dict]:
"""
Encode text into list of 9-feature token dictionaries.
Return list of dicts with keys matching get_feature_names().
"""
if not text:
return[]
tokens = []
segments = self.WORD_PATTERN.findall(text)
for seg_idx, segment in enumerate(segments):
# Skip whitespace - encode as has_space_after on previous token
if segment.isspace():
if tokens:
tokens[-1]['has_space_after'] = 1
continue
# Check if next segment is whitespace
has_space = 0
if seg_idx + 1 < len(segments) and segments [seg_idx + 1].isspace():
has_space = 1
# Determine token type
token_type = self._determine_token_type(segment)
is_cap = 1 if segment and segment[0].isupper() else 0
if token_type == 0: # Regular word -> syllabify
syllables = self._syllabify(segment)
n_syls = len(syllables)
for i, syl in enumerate(syllables):
# Position encoding
if n_syls == 1:
position = 3 # both start and end
elif i == 0:
position = 1 # start
elif i == n_syls -1:
position = 2 # end
else:
postiion = 0 # middle
# Extract phonetic components
onset, nucleus, coda = self._extract_onset_nucleus_coda(syl)
# Get/create IDs
syl_id = self._get_or_add_syllable(syl.lower())
onset_id = self._get_or_add_onset(onset)
nucleus_id = self._get_or_add_nucleus(nucleus)
coda_id = self._get_or_add_coda(coda)
# Only first syllable inherits capitalization
syl_cap = is_cap if i == 0 else 0
# Space only after last syllable of word
syl_space = has_space if i == n_syls - 1 else 0
tokens.append({
'text': syl,
'syllable_id': syl_id,
'onset_id': onset_id,
'nucleus_id': nucleus_id,
'coda_id': coda_id,
'position': position,
'is_capitalized': syl_cap,
'token_type': token_type,
'has_space_after': syl_space,
'is_word_end': 1 if i == n_syls - 1 else 0,
})
elif token_type == 1: # Number
syl_key = f"<num_{segment}>"
syl_id = self._get_or_add_syllable(syl_key)
tokens.append({
'text': segment,
'syllable_id': syl_id,
'onset_id': self.onset_to_id['<num>'],
'nucleus_id': self.nucleus_to_id.get(segment, 1),
'coda_id': self.coda_to_id.get('', 1),
'position': 3,
'is_capitalized': 0,
'token_type': token_type,
'has_space_after': has_space,
'is_word_end': 1,
})
elif token_type == 2: # Punctuation
syl_key = f"<punct_{segment}>"
syl_id = self._get_or_add_syllable(syl_key)
tokens.append({
'text': segment,
'syllable_id': syl_id,
'onset_id': self.onset_to_id['<punct>'],
'nucleus_id': self.nucleus_to_id.get(segment, 1),
'coda_id': self.coda_to_id.get('', 1),
'position': 3,
'is_capitalized': 0,
'token_type': token_type,
'has_space_after': has_space,
'is_word_end': 1,
})
else: # Special characters
syl_key = f"<char_{segment}>"
syl_id = self._get_or_add_syllable(syl_key)
tokens.append({
'text': segment,
'syllable_id': syl_id,
'onset_id': self.onset_to_id['<special>'],
'nucleus_id': self.nucleus_to_id.get('', 1),
'coda_id': self.coda_to_id.get('', 1),
'position': 3,
'is_capitalized': 0,
'token_type': token_type,
'has_space_after': has_space,
'is_word_end': 1,
})
return tokens
def decode(self, tokens: List[Dict]) -> str:
"""Decode token list back to text."""
parts = []
for token in tokens:
syl_id = token.get('syllable_id', 0)
syl = self.id_to_syllable.get(syl_id, '<unk>')
# Handle special tokens
if syl.startswith('<punct_') and syl.endswith('>'):
text = syl[7:-1]
elif syl.startswith('<num_') and syl.endswith('>'):
text = syl[5:-1]
elif syl.startswith('<char_') and syl.endswith('>'):
text = syl[6:-1]
elif syl in ('<pad>', '<unk>'):
continue
else:
text = syl
# Apply capitalization
if token.get('is_capitalized', 0):
text = text[0].upper() + text[1:] if len(text) > 1 else text.upper()
parts.appent(text)
# Add space if has_space_after
if token.get('has_space_after', 0):
parts.append(' ')
return ''.join(parts)
#-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Little Test
if __name__ == "__main__":
print("=" * 70)
print("SyllableLM v4 - Tokenizer Test")
print("=" * 70)
tokenizer = LunaTokenizer()
tests = [
"Hello World!",
"The quick brown fox jumps over the lazy dog.",
"Artificial intelligence is fascinating.",
]
for text in tests:
print(f"\nInput: '{text}'")
encoded = tokenizer.encode(text)
decoded = tokenizer.decode(encoded)
print(f"Tokens: {len(encoded)}")
print(f"Decoded: '{decoded}'")
print(f"Match: {text == decoded}")
print(f"\nFeatures ({len(tokenizer.get_feature_names())}): {tokenizer.get_feature_names()}")
print(f"Vocab size: {len(tokenizer.syllable_to_id)}")
|