Spaces:
Running
Running
File size: 8,157 Bytes
c49cb47 dbda7b0 c49cb47 |
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 |
/**
* Text comparison utilities for OCR Text Explorer
* Provides character, word, and line-level diff visualization
*/
/**
* Create character-level diff with inline highlighting
*/
function createCharacterDiff(original, improved) {
if (!original || !improved) {
return '<p class="text-gray-500">No text to compare</p>';
}
const dp = computeLCS(original, improved);
const diff = buildDiff(original, improved, dp);
let html = '<div class="font-mono text-sm whitespace-pre-wrap text-gray-900 dark:text-gray-100">';
for (const part of diff) {
if (part.type === 'equal') {
html += escapeHtml(part.value);
} else if (part.type === 'delete') {
html += `<span class="bg-red-200 dark:bg-red-950 text-red-800 dark:text-red-300 line-through">${escapeHtml(part.value)}</span>`;
} else if (part.type === 'insert') {
html += `<span class="bg-green-200 dark:bg-green-950 text-green-800 dark:text-green-300">${escapeHtml(part.value)}</span>`;
}
}
html += '</div>';
return html;
}
/**
* Create word-level diff
*/
function createWordDiff(original, improved) {
if (!original || !improved) {
return '<p class="text-gray-500">No text to compare</p>';
}
// Split into words while preserving whitespace
const originalWords = splitIntoWords(original);
const improvedWords = splitIntoWords(improved);
const dp = computeLCS(originalWords, improvedWords);
const diff = buildDiff(originalWords, improvedWords, dp);
let html = '<div class="font-mono text-sm whitespace-pre-wrap text-gray-900 dark:text-gray-100">';
for (const part of diff) {
if (part.type === 'equal') {
html += escapeHtml(part.value.join(''));
} else if (part.type === 'delete') {
html += `<span class="bg-red-200 dark:bg-red-950 text-red-800 dark:text-red-300 line-through">${escapeHtml(part.value.join(''))}</span>`;
} else if (part.type === 'insert') {
html += `<span class="bg-green-200 dark:bg-green-950 text-green-800 dark:text-green-300">${escapeHtml(part.value.join(''))}</span>`;
}
}
html += '</div>';
return html;
}
/**
* Create line-level diff
*/
function createLineDiff(original, improved) {
if (!original || !improved) {
return '<p class="text-gray-500">No text to compare</p>';
}
const originalLines = original.split('\n');
const improvedLines = improved.split('\n');
const dp = computeLCS(originalLines, improvedLines);
const diff = buildDiff(originalLines, improvedLines, dp);
let html = '<div class="font-mono text-sm text-gray-900 dark:text-gray-100">';
for (const part of diff) {
if (part.type === 'equal') {
for (const line of part.value) {
html += `<div class="py-1">${escapeHtml(line)}</div>`;
}
} else if (part.type === 'delete') {
for (const line of part.value) {
html += `<div class="py-1 bg-red-200 dark:bg-red-950 text-red-800 dark:text-red-300 line-through">${escapeHtml(line)}</div>`;
}
} else if (part.type === 'insert') {
for (const line of part.value) {
html += `<div class="py-1 bg-green-200 dark:bg-green-950 text-green-800 dark:text-green-300">${escapeHtml(line)}</div>`;
}
}
}
html += '</div>';
return html;
}
/**
* Compute Longest Common Subsequence using dynamic programming
*/
function computeLCS(a, b) {
const m = a.length;
const n = b.length;
const dp = Array(m + 1).fill(null).map(() => Array(n + 1).fill(0));
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (a[i - 1] === b[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp;
}
/**
* Build diff from LCS table
*/
function buildDiff(a, b, dp) {
const diff = [];
let i = a.length;
let j = b.length;
while (i > 0 || j > 0) {
if (i > 0 && j > 0 && a[i - 1] === b[j - 1]) {
// Characters are equal
if (diff.length > 0 && diff[diff.length - 1].type === 'equal') {
diff[diff.length - 1].value.unshift(a[i - 1]);
} else {
diff.push({ type: 'equal', value: [a[i - 1]] });
}
i--;
j--;
} else if (j > 0 && (i === 0 || dp[i][j - 1] >= dp[i - 1][j])) {
// Character in b but not in a (insertion)
if (diff.length > 0 && diff[diff.length - 1].type === 'insert') {
diff[diff.length - 1].value.unshift(b[j - 1]);
} else {
diff.push({ type: 'insert', value: [b[j - 1]] });
}
j--;
} else {
// Character in a but not in b (deletion)
if (diff.length > 0 && diff[diff.length - 1].type === 'delete') {
diff[diff.length - 1].value.unshift(a[i - 1]);
} else {
diff.push({ type: 'delete', value: [a[i - 1]] });
}
i--;
}
}
diff.reverse();
// Convert arrays to strings for character diff
if (typeof a === 'string') {
diff.forEach(part => {
part.value = part.value.join('');
});
}
return diff;
}
/**
* Split text into words while preserving whitespace
*/
function splitIntoWords(text) {
const words = [];
let current = '';
let inWord = false;
for (const char of text) {
if (/\s/.test(char)) {
if (inWord && current) {
words.push(current);
current = '';
inWord = false;
}
words.push(char);
} else {
current += char;
inWord = true;
}
}
if (current) {
words.push(current);
}
return words;
}
/**
* Escape HTML special characters
*/
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
/**
* Calculate similarity percentage between two texts
*/
function calculateSimilarity(original, improved) {
if (!original || !improved) return 0;
const dp = computeLCS(original, improved);
const lcsLength = dp[original.length][improved.length];
const maxLength = Math.max(original.length, improved.length);
return Math.round((lcsLength / maxLength) * 100);
}
/**
* Create markdown-aware diff showing original text vs rendered markdown
*/
function createMarkdownDiff(original, improved, renderFunction) {
if (!original || !improved) {
return '<p class="text-gray-500">No text to compare</p>';
}
let html = '<div class="grid grid-cols-2 gap-6">';
// Original text (plain)
html += '<div>';
html += '<h4 class="text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">Original (Plain Text)</h4>';
html += '<div class="font-mono text-xs bg-gray-50 dark:bg-gray-800 text-gray-900 dark:text-gray-100 p-4 rounded-lg overflow-x-auto">';
html += '<pre class="whitespace-pre-wrap">' + escapeHtml(original) + '</pre>';
html += '</div>';
html += '</div>';
// Improved text (rendered markdown)
html += '<div>';
html += '<h4 class="text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">Improved (Rendered Markdown)</h4>';
html += '<div class="bg-gray-50 dark:bg-gray-800 p-4 rounded-lg overflow-x-auto">';
// Render the markdown using the provided function
if (renderFunction && typeof renderFunction === 'function') {
html += renderFunction(improved);
} else {
// Fallback to plain text if no render function provided
html += '<pre class="whitespace-pre-wrap font-mono text-xs">' + escapeHtml(improved) + '</pre>';
}
html += '</div>';
html += '</div>';
html += '</div>';
return html;
} |