// Component-specific JavaScript for NLP Ultimate Tutorial // POS Tagging functionality class POSTagging { static highlightTokens(tokens, containerId) { const container = document.getElementById(containerId); if (!container) return; container.innerHTML = tokens.map(token => { const color = this.getPOSColor(token.pos); return `${token.text} (${token.pos})`; }).join(' '); } static getPOSColor(pos) { const colors = { 'NOUN': '#E3F2FD', 'PROPN': '#E3F2FD', 'VERB': '#E8F5E9', 'ADJ': '#FFF8E1', 'ADV': '#F3E5F5', 'ADP': '#EFEBE9', 'PRON': '#E8EAF6', 'DET': '#E0F7FA', 'CONJ': '#FBE9E7', 'NUM': '#FFEBEE', 'PART': '#F1F8E9', 'INTJ': '#FFF3E0', 'PUNCT': '#FAFAFA', 'SYM': '#FAFAFA', 'X': '#FAFAFA' }; return colors[pos] || '#FAFAFA'; } } // Named Entity Recognition functionality class NER { static highlightEntities(entities, containerId) { const container = document.getElementById(containerId); if (!container) return; container.innerHTML = entities.map(entity => { const color = this.getEntityColor(entity.type); return `${entity.text} (${entity.type})`; }).join(' '); } static getEntityColor(type) { const colors = { 'PERSON': '#E3F2FD', 'ORG': '#E8F5E9', 'GPE': '#FFF8E1', 'LOC': '#F3E5F5', 'PRODUCT': '#EFEBE9', 'EVENT': '#E8EAF6', 'WORK_OF_ART': '#E0F7FA', 'LAW': '#FBE9E7', 'LANGUAGE': '#FFEBEE', 'DATE': '#F1F8E9', 'TIME': '#FFF3E0', 'PERCENT': '#FAFAFA', 'MONEY': '#FAFAFA', 'QUANTITY': '#FAFAFA', 'ORDINAL': '#FAFAFA', 'CARDINAL': '#FAFAFA' }; return colors[type] || '#FAFAFA'; } } // Sentiment Analysis functionality class SentimentAnalysis { static createGauge(score, containerId) { const container = document.getElementById(containerId); if (!container) return; const color = this.getSentimentColor(score); const label = this.getSentimentLabel(score); container.innerHTML = `
${score.toFixed(3)}
${label}
`; } static getSentimentColor(score) { if (score > 0.1) return '#4CAF50'; if (score < -0.1) return '#F44336'; return '#FF9800'; } static getSentimentLabel(score) { if (score > 0.1) return 'Positive'; if (score < -0.1) return 'Negative'; return 'Neutral'; } } // Text Generation functionality class TextGeneration { static displayGeneratedText(prompt, generated, containerId) { const container = document.getElementById(containerId); if (!container) return; container.innerHTML = `
${prompt} ${generated}
`; } static createParameterControls(containerId) { const container = document.getElementById(containerId); if (!container) return; container.innerHTML = `
0.1 0.7 1.5
0.1 0.9 1.0
30 100 250
`; // Add event listeners for parameter updates ['temperature', 'top-p', 'max-length'].forEach(param => { const slider = document.getElementById(param); const valueDisplay = document.getElementById(`${param}-value`); if (slider && valueDisplay) { slider.addEventListener('input', () => { valueDisplay.textContent = slider.value; }); } }); } } // Translation functionality class Translation { static displayTranslationPair(sourceText, targetText, sourceLang, targetLang, containerId) { const container = document.getElementById(containerId); if (!container) return; container.innerHTML = `
${sourceLang}

${sourceText}

${targetLang}

${targetText}

`; } static createLanguageSelector(containerId) { const container = document.getElementById(containerId); if (!container) return; const languages = [ { code: 'en', name: 'English' }, { code: 'es', name: 'Spanish' }, { code: 'fr', name: 'French' }, { code: 'de', name: 'German' }, { code: 'ru', name: 'Russian' }, { code: 'zh', name: 'Chinese' }, { code: 'ar', name: 'Arabic' }, { code: 'hi', name: 'Hindi' }, { code: 'ja', name: 'Japanese' }, { code: 'pt', name: 'Portuguese' }, { code: 'it', name: 'Italian' } ]; container.innerHTML = `
`; } } // Classification functionality class Classification { static displayResults(results, containerId) { const container = document.getElementById(containerId); if (!container) return; container.innerHTML = results.map(result => `
${result.label}
${(result.score * 100).toFixed(1)}%
`).join(''); } static getScoreColor(score) { if (score > 0.7) return '#4CAF50'; if (score > 0.4) return '#FF9800'; return '#F44336'; } } // Vector Embeddings functionality class VectorEmbeddings { static displaySearchResults(results, containerId) { const container = document.getElementById(containerId); if (!container) return; container.innerHTML = results.map(result => `
${result.text}
Similarity: ${(result.score * 100).toFixed(1)}%
`).join(''); } static getScoreColor(score) { if (score > 0.7) return '#4CAF50'; if (score > 0.4) return '#FF9800'; return '#F44336'; } } // Chart utilities class ChartUtils { static createBarChart(canvasId, data, options = {}) { const ctx = document.getElementById(canvasId); if (!ctx) return null; const defaultOptions = { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'top', } }, scales: { y: { beginAtZero: true } } }; return new Chart(ctx, { type: 'bar', data: data, options: { ...defaultOptions, ...options } }); } static createPieChart(canvasId, data, options = {}) { const ctx = document.getElementById(canvasId); if (!ctx) return null; const defaultOptions = { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'bottom', } } }; return new Chart(ctx, { type: 'pie', data: data, options: { ...defaultOptions, ...options } }); } static createLineChart(canvasId, data, options = {}) { const ctx = document.getElementById(canvasId); if (!ctx) return null; const defaultOptions = { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: 'top', } }, scales: { y: { beginAtZero: true } } }; return new Chart(ctx, { type: 'line', data: data, options: { ...defaultOptions, ...options } }); } } // Animation utilities class AnimationUtils { static fadeIn(element, duration = 500) { element.style.opacity = '0'; element.style.transition = `opacity ${duration}ms ease-in`; setTimeout(() => { element.style.opacity = '1'; }, 10); } static slideIn(element, direction = 'left', duration = 500) { const transform = direction === 'left' ? 'translateX(-100%)' : 'translateX(100%)'; element.style.transform = transform; element.style.transition = `transform ${duration}ms ease-out`; setTimeout(() => { element.style.transform = 'translateX(0)'; }, 10); } static bounceIn(element, duration = 600) { element.style.transform = 'scale(0.3)'; element.style.opacity = '0'; element.style.transition = `all ${duration}ms ease-out`; setTimeout(() => { element.style.transform = 'scale(1)'; element.style.opacity = '1'; }, 10); } } // Export classes for global use window.NLPComponents = { POSTagging, NER, SentimentAnalysis, TextGeneration, Translation, Classification, VectorEmbeddings, ChartUtils, AnimationUtils };