/**
* Utility functions for creating visual representations of character analysis
* that help LLMs understand and verify the results.
*/
/**
* Creates a visual representation showing character positions and highlights
* @param text The input text
* @param highlightPositions Positions to highlight
* @param highlightChar The character being highlighted (for display)
*/
export function createVisualBreakdown(
text: string,
highlightPositions: number[],
highlightChar: string
): string {
const chars = [...text];
const indices = chars.map((_, i) => i.toString().padStart(2, ' ')).join(' ');
const charLine = chars.map(c => c.padStart(2, ' ')).join(' ');
const highlightLine = chars
.map((_, i) => (highlightPositions.includes(i) ? ' ^' : ' '))
.join(' ');
const markerLine = chars
.map((_, i) => (highlightPositions.includes(i) ? ` ${highlightChar}` : ' '))
.join(' ');
return [
`Characters: ${charLine}`,
`Indices: ${indices}`,
`Found: ${highlightLine}`,
` ${markerLine}`,
].join('\n');
}
/**
* Creates a simple indexed character list
*/
export function createIndexedList(text: string): Array<{ index: number; char: string }> {
return [...text].map((char, index) => ({ index, char }));
}
/**
* Creates a density/percentage summary
*/
export function createDensitySummary(
count: number,
total: number,
char: string
): string {
const percentage = total > 0 ? ((count / total) * 100).toFixed(1) : '0.0';
return `${count} out of ${total} characters (${percentage}%) are '${char}'`;
}
/**
* Format a frequency map as a readable table
*/
export function formatFrequencyTable(
frequency: Record<string, number>,
sortBy: 'alpha' | 'count' = 'count'
): string {
const entries = Object.entries(frequency);
if (sortBy === 'count') {
entries.sort((a, b) => b[1] - a[1]);
} else {
entries.sort((a, b) => a[0].localeCompare(b[0]));
}
const lines = entries.map(([char, count]) => {
const displayChar = char === ' ' ? '(space)' : char;
const bar = '█'.repeat(Math.min(count, 20));
return `'${displayChar}': ${count.toString().padStart(3)} ${bar}`;
});
return lines.join('\n');
}