Paragraph Cohesion Analyzer

Paragraph Cohesion Analyzer

Analyze the flow and connection between your sentences.

Please enter some text to analyze.

'; return; } const sentences = text.match(/[^.?!]+[.?!]+(\s|$)/g) || [text]; let repetitionCount = 0; let transitionOpportunities = 0; let outputHTML = ''; let lastFirstWord = ''; sentences.forEach((sentenceStr, index) => { let sentence = sentenceStr.trim(); const words = sentence.split(/\s+/); const firstWord = words[0].toLowerCase().replace(/[^a-z]/gi, ''); let classes = []; let title = ''; // 1. Check for repetitive starts if (index > 0 && firstWord === lastFirstWord && firstWord.length > 2) { repetitionCount++; classes.push('highlight-repetition'); title = 'Repetitive Start: This sentence starts with the same word as the previous one, which can make the writing sound monotonous.'; } // 2. Check for lack of transition words if (index > 0 && !transitionWords.has(firstWord)) { transitionOpportunities++; classes.push('highlight-no-transition'); title = title ? title + '\n\n' : ''; // Add separator if another highlight exists title += 'Transition Opportunity: Consider adding a transition word (e.g., "However," "Therefore," "In addition") to improve the flow from the previous sentence.'; } if (classes.length > 0) { outputHTML += `${sentence} `; } else { outputHTML += `${sentence} `; } lastFirstWord = firstWord; }); analysisOutput.innerHTML = outputHTML; // Update score repetitionCountEl.textContent = repetitionCount; transitionCountEl.textContent = transitionOpportunities; let score = 'Excellent'; if (repetitionCount > 2 || transitionOpportunities > sentences.length / 2) { score = 'Good'; } if (repetitionCount > 4 || transitionOpportunities > sentences.length * 0.75) { score = 'Needs Improvement'; } scoreEl.textContent = score; outputSection.classList.remove('hidden'); }; const downloadPdf = () => { const { jsPDF } = window.jspdf; const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const reportTitle = "Paragraph Cohesion Report"; const genDate = new Date().toLocaleDateString('en-US'); const pageWidth = doc.internal.pageSize.getWidth(); const margin = 15; let yPos = 0; // --- PDF Template: Cohesion Report --- doc.setFillColor(109, 40, 217); // violet-600 doc.rect(0, 0, pageWidth, 28, 'F'); doc.setFont('helvetica', 'bold'); doc.setFontSize(16); doc.setTextColor(255, 255, 255); doc.text(reportTitle, margin, 18); yPos = 40; // Summary Section doc.setFont('helvetica', 'bold'); doc.setFontSize(12); doc.setTextColor(30, 41, 59); doc.text("Analysis Summary", margin, yPos); yPos += 8; doc.autoTable({ startY: yPos, head: [['Metric', 'Result']], body: [ ['Overall Flow', scoreEl.textContent], ['Repetitive Sentence Starts', repetitionCountEl.textContent], ['Transition Opportunities', transitionCountEl.textContent], ], theme: 'grid', headStyles: { fillColor: [91, 33, 182] }, // violet-700 margin: { left: margin, right: margin } }); yPos = doc.autoTable.previous.finalY + 15; // Annotated Text doc.setFont('helvetica', 'bold'); doc.setFontSize(12); doc.setTextColor(30, 41, 59); doc.text("Annotated Text", margin, yPos); yPos += 8; html2canvas(analysisOutput, { 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) { doc.addPage(); yPos = 20; } doc.addImage(imgData, 'PNG', margin, yPos, pdfWidth, pdfHeight); // Footer const pageCount = doc.internal.getNumberOfPages(); for(let i = 1; i <= pageCount; i++) { doc.setPage(i); const footerY = doc.internal.pageSize.getHeight() - 15; doc.setLineWidth(0.2); doc.setDrawColor(167, 139, 250); // violet-400 doc.line(margin, footerY, pageWidth - margin, footerY); doc.setFontSize(8); doc.setTextColor(100, 116, 139); doc.text(`Report Generated on: ${genDate}`, margin, footerY + 8); doc.text(`Page ${i} of ${pageCount}`, pageWidth - margin, footerY + 8, { align: 'right' }); } doc.save(`Paragraph_Cohesion_Report.pdf`); }).catch(err => { console.error("Error generating PDF:", err); alert("Could not generate PDF. See console for details."); }); }; // --- External libraries for PDF --- // This is a workaround to make autoTable available to jsPDF if (typeof window.jspdf.jsPDF.autoTable !== 'function') { const script = document.createElement('script'); script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.23/jspdf.plugin.autotable.min.js'; document.head.appendChild(script); } // --- Event Listeners --- analyzeBtn.addEventListener('click', analyzeCohesion); downloadPdfBtn.addEventListener('click', downloadPdf); });
Scroll to Top