""" Use HorizontalLevel for horizontal newline levels Use VerticalLevel for vertical bar levels I still need to finish this """ import numpy as np def extract_level_representation(llm_output, model_type="llama-3", orientation="horizontal", separator="\n"): if isinstance(llm_output, list): llm_output = llm_output[0] level_content = "" if model_type == "llama-3": parts = llm_output.split("<|start_header_id|>assistant<|end_header_id|>") if len(parts) > 1: assistant_section = parts[-1] if "<|eot_id|>" in assistant_section: level_content = assistant_section.split("<|eot_id|>")[0].strip() else: level_content = assistant_section.strip() else: level_content = llm_output.strip() elif model_type == "gemma-3": parts = llm_output.split("model") if len(parts) > 1: model_section = parts[-1] if "" in model_section: level_content = model_section.split("")[0].strip() else: level_content = model_section.strip() else: level_content = llm_output.strip() elif model_type == "qwen-2.5": parts = llm_output.split("<|im_start|>assistant") if len(parts) > 1: assistant_block = parts[-1] if "<|im_end|>" in assistant_block: level_content = assistant_block.split("<|im_end|>")[0].strip() else: level_content = assistant_block.strip() else: level_content = llm_output.strip() else: raise ValueError(f"Unsupported model type: {model_type}") if orientation == "vertical": level_content = VerticalLevel.reconstruct_level_from_vertical_bar(level_content, separator) return level_content def adjust_horizontal_size(level_str, target_length=50, orientation="horizontal", separator="\n"): if orientation == "vertical": level_str = VerticalLevel.reconstruct_level_from_vertical_bar(level_str, separator) lines = level_str.split('\n') adjusted_lines = [] for line in lines: current_length = len(line) if current_length < target_length: line += '-' * (target_length - current_length) elif current_length > target_length: line = line[:target_length] adjusted_lines.append(line) return '\n'.join(adjusted_lines) def fix_level_format(level_str, orientation="horizontal", separator=None, line_quantity=14, target_length=50): # Auto-detect separator if not provided if separator is None: has_newline = '\n' in level_str has_pipe = '|' in level_str if has_newline and has_pipe: raise ValueError("Level string contains both '\\n' and '|' separators. Cannot auto-detect separator.") elif has_newline: separator = '\n' elif has_pipe: separator = '|' else: separator = None # No separator found # If separator is None, we can't process vertical orientation or split lines # For now, we'll handle it as a single line if separator is None: # Single line - just ensure it matches target_length if len(level_str) > target_length: level_str = level_str[:target_length] elif len(level_str) < target_length: padding_needed = target_length - len(level_str) padding_char = 'X' if level_str and level_str[-1] == 'X' else '-' level_str = level_str + (padding_char * padding_needed) # For single line, we need to create line_quantity lines lines = [level_str] if level_str else [] while len(lines) < line_quantity: lines.insert(0, '-' * target_length) return '\n'.join(lines) if orientation == "vertical": level_str = VerticalLevel.reconstruct_level_from_vertical_bar(level_str, separator) lines = level_str.split(separator) line_lengths = [len(line) for line in lines] if len(set(line_lengths)) > 1 or max(line_lengths) > target_length: changed = True while changed: changed = False max_length = max(line_lengths) if max_length <= target_length: break longest_lines_indices = [i for i, length in enumerate(line_lengths) if length == max_length] lines_trimmed = False for idx in longest_lines_indices: line = lines[idx] if line and line[-1] == '-': lines[idx] = line[:-1] line_lengths[idx] -= 1 lines_trimmed = True changed = True if not lines_trimmed: break for i in range(len(lines)): if len(lines[i]) > target_length: lines[i] = lines[i][:target_length] line_lengths[i] = target_length for i in range(len(lines)): current_length = len(lines[i]) if current_length < target_length: padding_needed = target_length - current_length if lines[i] and lines[i][-1] == 'X': padding_char = 'X' else: padding_char = '-' lines[i] = lines[i] + (padding_char * padding_needed) line_lengths[i] = target_length final_length = target_length # Step 4: Enforce exact line count if len(lines) > line_quantity: lines = lines[-line_quantity:] while len(lines) < line_quantity: lines.insert(0, '-' * final_length) return separator.join(lines) def fix_level_format_lax( level_str, orientation="horizontal", separator=None, line_quantity=14, target_length=50, ): # Auto-detect separator if not provided if separator is None: has_newline = "\n" in level_str has_pipe = "|" in level_str if has_newline and has_pipe: raise ValueError( "Level string contains both '\\n' and '|' separators. Cannot auto-detect separator." ) elif has_newline: separator = "\n" elif has_pipe: separator = "|" else: separator = None # Single-line case (no separator found) if separator is None: lines = [level_str] if level_str else [] if target_length is not None: if lines: line = lines[0] if len(line) > target_length: line = line[:target_length] elif len(line) < target_length: pad = target_length - len(line) pad_char = "X" if line and line[-1] == "X" else "-" line = line + pad_char * pad lines[0] = line final_length = target_length else: final_length = len(lines[0]) if lines else 0 while len(lines) < line_quantity: lines.insert(0, "-" * final_length if final_length else "") return "\n".join(lines) # Vertical reconstruction if needed if orientation == "vertical": level_str = VerticalLevel.reconstruct_level_from_vertical_bar( level_str, separator ) lines = level_str.split(separator) # Width normalization only if target_length is set if target_length is not None: line_lengths = [len(line) for line in lines] # Trim overly long lines if line_lengths: if len(set(line_lengths)) > 1 or max(line_lengths) > target_length: changed = True while changed: changed = False max_length = max(line_lengths) if max_length <= target_length: break longest_idxs = [ i for i, l in enumerate(line_lengths) if l == max_length ] trimmed = False for idx in longest_idxs: line = lines[idx] if line and line[-1] == "-": lines[idx] = line[:-1] line_lengths[idx] -= 1 trimmed = True changed = True if not trimmed: break # Hard truncate if still too long for i in range(len(lines)): if len(lines[i]) > target_length: lines[i] = lines[i][:target_length] # Pad shorter lines for i in range(len(lines)): if len(lines[i]) < target_length: pad = target_length - len(lines[i]) pad_char = "X" if lines[i] and lines[i][-1] == "X" else "-" lines[i] = lines[i] + pad_char * pad final_length = target_length else: # Preserve original widths final_length = max((len(line) for line in lines), default=0) # Enforce exact number of lines if len(lines) > line_quantity: lines = lines[-line_quantity:] while len(lines) < line_quantity: if target_length is not None: lines.insert(0, "-" * final_length) else: lines.insert(0, "") return separator.join(lines) def is_level_correctly_formatted(level, num_lines, chars_per_line, separator=None): """ Check if a level string is correctly formatted. Args: level: The level string to check num_lines: Expected number of lines chars_per_line: Expected characters per line separator: Separator character (e.g., '\n', '|'). If None/empty, only checks total length = num_lines * chars_per_line Returns: bool: True if correctly formatted, False otherwise """ if separator is None or separator == "": # Simple length check: total chars should equal num_lines * chars_per_line expected_length = num_lines * chars_per_line return len(level) == expected_length # Split by separator and check each line lines = level.split(separator) # Check correct number of lines if len(lines) != num_lines: return False # Check each line has correct number of characters for line in lines: if len(line) != chars_per_line: return False return True def extend_level(normalized_level_str, orientation="horizontal", separator="\n"): if orientation == "vertical": normalized_level_str = VerticalLevel.reconstruct_level_from_vertical_bar(normalized_level_str, separator) lines = normalized_level_str.split('\n') extended_lines = [] bottom_two_has_x_ending = False if len(lines) >= 2: if lines[-1].endswith('X') or lines[-2].endswith('X'): bottom_two_has_x_ending = True for i, line in enumerate(lines): if len(set(line)) == 1 and '-' not in set(line): single_char = line[0] prefix = single_char * 5 elif i >= len(lines) - 2: # Last two lines prefix = 'X' * 5 else: prefix = '-' * 5 if not bottom_two_has_x_ending: if len(set(line)) == 1 and '-' not in set(line): single_char = line[0] suffix = single_char * 3 elif i >= len(lines) - 2: suffix = 'X' * 3 else: suffix = '-' * 3 # Add both prefix and suffix extended_lines.append(prefix + line + suffix) else: extended_lines.append(prefix + line) return '\n'.join(extended_lines) class VerticalLevel: @staticmethod def reconstruct_level_from_vertical_bar(vertical_bar_str, separator="\n"): if not vertical_bar_str: return None vertical_columns = vertical_bar_str.split(separator) num_cols = len(vertical_columns) if num_cols == 0 or not vertical_columns[0]: return None num_rows = len(vertical_columns[0]) if num_rows == 0: return None reconstructed_rows = [] for i in range(num_rows): current_row_chars = [] for j in range(num_cols): vertical_char_index = num_rows - 1 - i if j < len(vertical_columns) and vertical_char_index < len(vertical_columns[j]): char = vertical_columns[j][vertical_char_index] current_row_chars.append(char) reconstructed_rows.append("".join(current_row_chars)) return "\n".join(reconstructed_rows)