Online Historical Text Analyzer

Online Historical Text Analyzer

Analyze word frequency, readability, and sentiment of historical texts.

Analysis Results

Your analysis results will appear here.

${analysis.error}

`; downloadPdfBtn.classList.add('hidden'); return; } // Generate HTML for results const resultsHTML = `

General Statistics

  • Word Count: ${analysis.wordCount}
  • Sentence Count: ${analysis.sentenceCount}
  • Avg. Words per Sentence: ${analysis.averageWordsPerSentence}

Readability

  • Flesch Reading Ease: ${analysis.fleschScore} (Higher is easier)

Sentiment Analysis

  • Overall Tone: ${analysis.sentiment.label}
  • Positive Words Found: ${analysis.sentiment.positiveWords}
  • Negative Words Found: ${analysis.sentiment.negativeWords}

Top 15 Most Frequent Words

${analysis.sortedFrequencies.map(item => ``).join('')}
WordCount
${item[0]}${item[1]}
`; resultsOutput.innerHTML = resultsHTML; downloadPdfBtn.classList.remove('hidden'); }); compareBtn.addEventListener('click', () => { const text1 = compareText1.value; const text2 = compareText2.value; const analysis1 = performAnalysis(text1); const analysis2 = performAnalysis(text2); comparisonOutput.innerHTML = ''; // Clear previous if (analysis1.error || analysis2.error) { comparisonOutput.innerHTML = `

${analysis1.error || ''} ${analysis2.error || ''}

`; downloadComparisonPdfBtn.classList.add('hidden'); return; } // Jaccard similarity const set1 = new Set(text1.toLowerCase().match(/\b[\w']+\b/g)); const set2 = new Set(text2.toLowerCase().match(/\b[\w']+\b/g)); const intersection = new Set([...set1].filter(x => set2.has(x))); const union = new Set([...set1, ...set2]); const jaccardIndex = (intersection.size / union.size).toFixed(3); const comparisonHTML = `

Vocabulary Similarity

Jaccard Index: ${jaccardIndex}

Measures vocabulary overlap. 1 is identical, 0 is no overlap.

Text 1 Analysis

  • Word Count: ${analysis1.wordCount}
  • Sentence Count: ${analysis1.sentenceCount}
  • Flesch Score: ${analysis1.fleschScore}
  • Sentiment: ${analysis1.sentiment.label}

Text 2 Analysis

  • Word Count: ${analysis2.wordCount}
  • Sentence Count: ${analysis2.sentenceCount}
  • Flesch Score: ${analysis2.fleschScore}
  • Sentiment: ${analysis2.sentiment.label}
`; comparisonOutput.innerHTML = comparisonHTML; downloadComparisonPdfBtn.classList.remove('hidden'); }); // --- PDF DOWNLOAD LOGIC --- const generatePdf = (title, contentElementId, tableElementIds) => { const { jsPDF } = window.jspdf; const doc = new jsPDF(); let finalY = 20; // Y position for the next element doc.setFontSize(18); doc.text(title, 14, finalY); finalY += 10; const contentElement = document.getElementById(contentElementId); if (!contentElement) return; const sections = contentElement.querySelectorAll('h3'); sections.forEach(header => { doc.setFontSize(14); doc.setFont(undefined, 'bold'); doc.text(header.innerText, 14, finalY); finalY += 6; const list = header.nextElementSibling; if (list && list.tagName === 'UL') { const items = list.querySelectorAll('li'); doc.setFontSize(11); doc.setFont(undefined, 'normal'); items.forEach(item => { // Clean up the text for PDF const itemText = item.innerText.replace(/\s+/g, ' ').trim(); doc.text(itemText, 18, finalY); finalY += 6; }); finalY += 4; // Extra space after a list } else if (list && list.tagName === 'TABLE') { const tableId = list.id; if (tableId && tableElementIds.includes(`#${tableId}`)) { doc.autoTable({ html: `#${tableId}`, startY: finalY, theme: 'grid', headStyles: { fillColor: [75, 85, 99] }, // slate-600 styles: { cellPadding: 2, fontSize: 10 }, }); finalY = doc.lastAutoTable.finalY + 10; } } else if (list && list.classList.contains('text-center')) { // For Jaccard Index doc.setFontSize(11); doc.setFont(undefined, 'normal'); list.querySelectorAll('p').forEach(p => { doc.text(p.innerText, 14, finalY); finalY += 6; }); finalY += 4; } }); // For comparison columns if (contentElementId === 'comparison-content-for-pdf') { const col1 = document.getElementById('comp-col-1'); const col2 = document.getElementById('comp-col-2'); if(col1 && col2) { doc.autoTable({ body: [ [ {content: col1.innerText, styles: {halign: 'left'}}, {content: col2.innerText, styles: {halign: 'left'}} ] ], startY: finalY, theme: 'plain' }); } } doc.save(`${title.toLowerCase().replace(/\s/g, '-')}.pdf`); }; downloadPdfBtn.addEventListener('click', () => { generatePdf('Text Analysis Report', 'analysis-content-for-pdf', ['#freq-table']); }); downloadComparisonPdfBtn.addEventListener('click', () => { generatePdf('Text Comparison Report', 'comparison-content-for-pdf', []); }); // Initialize first tab updateNavButtons(); });
Scroll to Top