This commit is contained in:
justumen
2025-03-19 17:36:25 +01:00
parent 44d69e8907
commit 39dfb0220a
76 changed files with 3207 additions and 955 deletions

View File

@@ -5,8 +5,12 @@ import csv
from itertools import cycle
#{red|blue}
#{left|right|middle|group=LR}+{left|right|middle|group=LR}+{left|right|middle|group=LR}
#{left|right|middle|group=LMR}+{left|right|middle|group=LMR}+{left|right|middle|group=LMR}
#{A(80%)|B(15%)|C(5%)}
#2 {apple|orange|banana|static_group=FRUIT}s, one {apple|orange|banana|static_group=FRUIT} on the left, one {apple|orange|banana|static_group=FRUIT} on the right
#Double layer variable : <<CHAR>>
#CHAR = JESSICA
#JESSICA = en/jess.wav
class WriteTextAdvanced:
@classmethod
@@ -28,6 +32,7 @@ class WriteTextAdvanced:
CATEGORY = "Bjornulf"
def find_variables(self, text):
"""Identify nested curly brace sections in the text."""
stack = []
variables = []
for i, char in enumerate(text):
@@ -40,10 +45,12 @@ class WriteTextAdvanced:
'end': i + 1,
'nesting': nesting
})
# Sort by descending nesting level and position to process inner variables first
variables.sort(key=lambda x: (-x['nesting'], -x['end']))
return variables
def parse_option(self, part):
"""Parse options within curly braces, handling CSV and weighted choices."""
if part.startswith('%csv='):
try:
filename = part.split('=', 1)[1].strip()
@@ -56,49 +63,66 @@ class WriteTextAdvanced:
return (option.strip(), float(weight.split('%)')[0]))
return part.strip()
def process_content(self, content, seed):
random.seed(seed)
parts = []
def process_content(self, content, base_seed, position):
"""Process content within curly braces, handling groups and random choices."""
# Use position to vary the seed for independent random choices
random.seed(base_seed + position)
parts = [p.strip() for p in content.split('|')]
options = []
weights = []
group_defined = False
group_name = None
static_group = None
cycling_group = None
for p in content.split('|'):
p = p.strip()
if p.startswith('group='):
group_name = p.split('=', 1)[1].strip()
group_defined = True
continue
parsed = self.parse_option(p)
if isinstance(parsed, list): # CSV data
parts.extend(parsed)
weights.extend([1]*len(parsed))
elif isinstance(parsed, tuple): # Weighted option
parts.append(parsed[0])
weights.append(parsed[1])
for p in parts:
if p.startswith('static_group='):
static_group = p.split('=', 1)[1].strip()
elif p.startswith('group='):
cycling_group = p.split('=', 1)[1].strip()
else:
parts.append(parsed)
weights.append(1)
if group_defined:
return {'type': 'group', 'name': group_name, 'options': parts}
if any(w != 1 for w in weights):
total = sum(weights)
if total == 0: weights = [1]*len(parts)
return random.choices(parts, weights=[w/total for w in weights])[0]
return random.choice(parts) if parts else ''
parsed = self.parse_option(p)
if isinstance(parsed, list): # CSV data
options.extend(parsed)
weights.extend([1] * len(parsed))
elif isinstance(parsed, tuple): # Weighted option
options.append(parsed[0])
weights.append(parsed[1])
else:
options.append(parsed)
weights.append(1)
if static_group and cycling_group:
raise ValueError("Cannot specify both static_group and group in the same curly brace section.")
if static_group:
return {'type': 'static_group', 'name': static_group, 'options': options, 'weights': weights}
elif cycling_group:
return {'type': 'cycling_group', 'name': cycling_group, 'options': options, 'weights': weights}
else:
if options:
if any(w != 1 for w in weights):
total = sum(weights)
if total == 0:
weights = [1] * len(options)
return random.choices(options, weights=[w / total for w in weights])[0]
else:
return random.choice(options)
return ''
def write_text_special(self, text, variables="", seed=None):
"""Main function to process text with special syntax."""
if seed is None or seed == 0:
seed = int(time.time() * 1000)
random.seed(seed)
# Handle variables
# Process text: remove comments and empty lines
text_lines = [line.strip() for line in text.split('\n')
if line.strip() and not line.strip().startswith('#')]
text = '\n'.join(text_lines)
# Replace predefined variables
var_dict = {}
for line in variables.split('\n'):
var_lines = [line.strip() for line in variables.split('\n')
if line.strip() and not line.strip().startswith('#')]
for line in var_lines:
if '=' in line:
key, value = line.split('=', 1)
var_dict[key.strip()] = value.strip()
@@ -107,23 +131,36 @@ class WriteTextAdvanced:
# Process nested variables
variables = self.find_variables(text)
static_groups = {}
cycling_groups = {}
substitutions = []
groups = {}
for var in variables:
start, end = var['start'], var['end']
content = text[start+1:end-1]
processed = self.process_content(content, seed)
content = text[start + 1:end - 1]
# Pass the position (start) to vary the seed for non-group choices
processed = self.process_content(content, seed, start)
if isinstance(processed, dict):
if processed['type'] == 'group':
group_name = processed['name']
if group_name not in groups:
groups[group_name] = []
groups[group_name].append({
if processed['type'] == 'static_group':
name = processed['name']
if name not in static_groups:
static_groups[name] = []
static_groups[name].append({
'start': start,
'end': end,
'options': processed['options']
'options': processed['options'],
'weights': processed['weights']
})
elif processed['type'] == 'cycling_group':
name = processed['name']
if name not in cycling_groups:
cycling_groups[name] = []
cycling_groups[name].append({
'start': start,
'end': end,
'options': processed['options'],
'weights': processed['weights']
})
else:
substitutions.append({
@@ -132,15 +169,35 @@ class WriteTextAdvanced:
'sub': processed
})
# Handle groups
for group_name, matches in groups.items():
# Handle static groups: choose one value per group name
random.seed(seed) # Reset seed for consistent static group behavior
for name, matches in static_groups.items():
if not matches or not matches[0]['options']:
continue
options = matches[0]['options']
weights = matches[0]['weights']
if any(w != 1 for w in weights):
total = sum(weights)
if total == 0:
weights = [1] * len(options)
chosen = random.choices(options, weights=[w / total for w in weights])[0]
else:
chosen = random.choice(options) if options else ''
for m in matches:
substitutions.append({
'start': m['start'],
'end': m['end'],
'sub': chosen
})
# Handle cycling groups: cycle through shuffled options
random.seed(seed) # Reset seed for consistent cycling group behavior
for name, matches in cycling_groups.items():
if not matches or not matches[0]['options']:
continue
options = matches[0]['options']
permuted = random.sample(options, len(options))
perm_cycle = cycle(permuted)
for m in matches:
substitutions.append({
'start': m['start'],
@@ -148,7 +205,7 @@ class WriteTextAdvanced:
'sub': next(perm_cycle)
})
# Apply regular substitutions
# Apply substitutions in reverse order
substitutions.sort(key=lambda x: -x['start'])
result_text = text
for sub in substitutions:
@@ -158,4 +215,5 @@ class WriteTextAdvanced:
@classmethod
def IS_CHANGED(s, text, variables="", seed=None):
"""Check if inputs have changed."""
return (text, variables, seed)