Please enter some text to check.
';
return;
}
let correctedText = originalText;
let spellingCorrections = 0;
let grammarCorrections = 0;
let capitalizationCorrections = 0;
// 1. Capitalization
correctedText = correctedText.replace(/([.?!])\s*([a-z])/g, (match, p1, p2) => {
capitalizationCorrections++;
return `${p1} ${p2.toUpperCase()}`;
});
// Capitalize the very first letter if it's lowercase
if (correctedText.length > 0 && correctedText[0] >= 'a' && correctedText[0] <= 'z') {
correctedText = correctedText[0].toUpperCase() + correctedText.slice(1);
capitalizationCorrections++;
}
// 2. Spelling and Grammar
const correctionKeys = Object.keys(corrections);
correctionKeys.forEach(key => {
const regex = new RegExp(`\\b${key}\\b`, 'gi');
if (regex.test(correctedText)) {
const count = (correctedText.match(regex) || []).length;
// Simple categorization for demo purposes
if (['your a', 'youre', 'its a', 'their is', 'there are alot'].includes(key)) {
grammarCorrections += count;
} else {
spellingCorrections += count;
}
correctedText = correctedText.replace(regex, corrections[key]);
}
});
// Display results with highlighting
displayWithDiff(originalText, correctedText);
// Update counts
spellingCountEl.textContent = spellingCorrections;
grammarCountEl.textContent = grammarCorrections;
capitalizationCountEl.textContent = capitalizationCorrections;
outputSection.classList.remove('hidden');
};
// Function to create a visual diff
const displayWithDiff = (original, corrected) => {
const originalWords = original.split(/(\s+)/);
const correctedWords = corrected.split(/(\s+)/);
let html = '';
// This is a simple diff, not perfect for complex changes
let i = 0, j = 0;
while(i < originalWords.length && j < correctedWords.length) {
if (originalWords[i] === correctedWords[j]) {
html += originalWords[i];
i++; j++;
} else {
// Find the next sync point
let nextSync = -1;
for(let k = j; k < correctedWords.length; k++) {
if (correctedWords[k] === originalWords[i+1]) {
nextSync = k;
break;
}
}
if (originalWords[i].trim() !== '') html += `
${originalWords[i]}`;
if (nextSync !== -1) {
for (let k = j; k < nextSync; k++) {
if (correctedWords[k].trim() !== '') html += `
${correctedWords[k]}`;
else html += correctedWords[k];
}
j = nextSync;
} else {
if (correctedWords[j].trim() !== '') html += `
${correctedWords[j]}`;
j++;
}
i++;
}
}
analysisOutput.innerHTML = html;
};
const downloadPdf = () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const original = textInput.value;
const correctedContainer = getEl('analysis-output');
const title = "Writing Analysis Report";
const genDate = new Date().toLocaleDateString('en-US');
const pageWidth = doc.internal.pageSize.getWidth();
const margin = 15;
let yPos = 0;
// --- PDF Template: Writing Analysis Report ---
doc.setFillColor(34, 197, 94); // Green-500
doc.rect(0, 0, pageWidth, 25, 'F');
doc.setFont('helvetica', 'bold');
doc.setFontSize(16);
doc.setTextColor(255, 255, 255);
doc.text(title, margin, 16);
yPos = 35;
// Summary
doc.setFont('helvetica', 'bold');
doc.setFontSize(12);
doc.setTextColor(30, 41, 59);
doc.text("Summary of Corrections", margin, yPos);
yPos += 8;
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
doc.text(`- Spelling Fixes: ${spellingCountEl.textContent}`, margin + 5, yPos);
doc.text(`- Grammar Fixes: ${grammarCountEl.textContent}`, margin + 60, yPos);
doc.text(`- Capitalization Fixes: ${capitalizationCountEl.textContent}`, margin + 115, yPos);
yPos += 15;
// Original Text
doc.setFont('helvetica', 'bold');
doc.setFontSize(12);
doc.text("Original Text", margin, yPos);
yPos += 8;
doc.setFont('helvetica', 'normal');
doc.setFontSize(10);
const originalLines = doc.splitTextToSize(original, pageWidth - margin * 2);
doc.text(originalLines, margin, yPos);
yPos += originalLines.length * 4 + 10;
// Corrected Text (using html2canvas for rich text)
doc.setFont('helvetica', 'bold');
doc.setFontSize(12);
doc.text("Corrected & Highlighted Text", margin, yPos);
yPos += 8;
html2canvas(correctedContainer, { scale: 2, useCORS: true }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const imgProps = doc.getImageProperties(imgData);
const pdfWidth = pageWidth - (margin * 2);
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;
if (yPos + pdfHeight > 280) { // Check if it fits on the page
doc.addPage();
yPos = 15;
}
doc.addImage(imgData, 'PNG', margin, yPos, pdfWidth, pdfHeight);
// Footer
const pageCount = doc.internal.getNumberOfPages();
for(let i = 1; i <= pageCount; i++) {
doc.setPage(i);
doc.setFontSize(8);
doc.setTextColor(100, 116, 139);
doc.text(`Report Generated: ${genDate}`, margin, 285);
doc.text(`Page ${i} of ${pageCount}`, pageWidth - margin, 285, { align: 'right' });
}
doc.save(`Writing_Analysis_Report.pdf`);
}).catch(err => {
console.error("Error generating PDF:", err);
alert("Could not generate PDF. See console for details.");
});
};
// --- Event Listeners ---
checkTextBtn.addEventListener('click', checkText);
downloadPdfBtn.addEventListener('click', downloadPdf);
});