Spaces:
Running

nuage / index.html
masterseb's picture
Faire une application qui crée un nuage de mots en fonction de la fréquence de l’utilisation du mot. La source étant un texte collé suivant l’invite. - Follow Up Deployment
b1f7536 verified
Raw History Blame Contribute Delete
17.8 kB
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nuage de Mots - Analyseur de Texte</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
.word-cloud {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
gap: 10px;
padding: 20px;
min-height: 300px;
background-color: #f8fafc;
border-radius: 0.5rem;
box-shadow: inset 0 1px 3px 0 rgba(0, 0, 0, 0.1);
}
.word-item {
display: inline-block;
transition: all 0.3s ease;
cursor: default;
}
.word-item:hover {
transform: scale(1.1);
}
@keyframes float {
0% { transform: translateY(0px); }
50% { transform: translateY(-5px); }
100% { transform: translateY(0px); }
}
.floating {
animation: float 3s ease-in-out infinite;
}
</style>
</head>
<body class="bg-gray-50 min-h-screen">
<div class="container mx-auto px-4 py-8">
<header class="text-center mb-8">
<h1 class="text-3xl md:text-4xl font-bold text-indigo-700 mb-2">Nuage de Mots</h1>
<p class="text-gray-600">Analysez la fréquence des mots dans votre texte</p>
</header>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
<div class="bg-white rounded-lg shadow-md p-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4">Entrez votre texte</h2>
<textarea id="text-input" class="w-full h-64 p-4 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500" placeholder="Collez ou écrivez votre texte ici..."></textarea>
<div class="flex flex-wrap gap-3 mt-4">
<button id="generate-btn" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors">
Générer le nuage
</button>
<button id="clear-btn" class="px-6 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition-colors">
Effacer
</button>
<div class="flex items-center ml-auto">
<label for="max-words" class="mr-2 text-sm text-gray-600">Mots max:</label>
<input type="number" id="max-words" min="10" max="100" value="50" class="w-16 p-1 border border-gray-300 rounded">
</div>
</div>
<div class="mt-6">
<h3 class="text-lg font-medium text-gray-800 mb-2">Options</h3>
<div class="flex flex-wrap gap-4">
<label class="inline-flex items-center">
<input type="checkbox" id="ignore-case" checked class="rounded text-indigo-600">
<span class="ml-2 text-gray-700">Ignorer la casse</span>
</label>
<label class="inline-flex items-center">
<input type="checkbox" id="ignore-common" checked class="rounded text-indigo-600">
<span class="ml-2 text-gray-700">Ignorer mots courants</span>
</label>
<label class="inline-flex items-center">
<input type="checkbox" id="animate" checked class="rounded text-indigo-600">
<span class="ml-2 text-gray-700">Animation</span>
</label>
</div>
</div>
</div>
<div class="bg-white rounded-lg shadow-md p-6">
<h2 class="text-xl font-semibold text-gray-800 mb-4">Résultats</h2>
<div id="word-cloud-container" class="word-cloud">
<p class="text-gray-400">Le nuage de mots apparaîtra ici</p>
</div>
<div class="mt-6">
<h3 class="text-lg font-medium text-gray-800 mb-2">Statistiques</h3>
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
<div class="bg-indigo-50 p-3 rounded-lg">
<p class="text-sm text-indigo-600">Mots uniques</p>
<p id="unique-words" class="text-2xl font-bold text-indigo-800">0</p>
</div>
<div class="bg-blue-50 p-3 rounded-lg">
<p class="text-sm text-blue-600">Mots totaux</p>
<p id="total-words" class="text-2xl font-bold text-blue-800">0</p>
</div>
<div class="bg-purple-50 p-3 rounded-lg">
<p class="text-sm text-purple-600">Mots analysés</p>
<p id="analyzed-words" class="text-2xl font-bold text-purple-800">0</p>
</div>
<div class="bg-pink-50 p-3 rounded-lg">
<p class="text-sm text-pink-600">Densité</p>
<p id="density" class="text-2xl font-bold text-pink-800">0%</p>
</div>
</div>
</div>
<div class="mt-6">
<h3 class="text-lg font-medium text-gray-800 mb-2">Top 10 des mots</h3>
<div class="h-64">
<canvas id="chart"></canvas>
</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const textInput = document.getElementById('text-input');
const generateBtn = document.getElementById('generate-btn');
const clearBtn = document.getElementById('clear-btn');
const wordCloudContainer = document.getElementById('word-cloud-container');
const maxWordsInput = document.getElementById('max-words');
const ignoreCaseCheckbox = document.getElementById('ignore-case');
const ignoreCommonCheckbox = document.getElementById('ignore-common');
const animateCheckbox = document.getElementById('animate');
// Elements for statistics
const uniqueWordsEl = document.getElementById('unique-words');
const totalWordsEl = document.getElementById('total-words');
const analyzedWordsEl = document.getElementById('analyzed-words');
const densityEl = document.getElementById('density');
let chart = null;
// Common words to ignore (French)
const commonWords = new Set([
'le', 'la', 'les', 'de', 'des', 'un', 'une', 'et', 'est', 'que',
'qui', 'dans', 'en', 'au', 'aux', 'pour', 'par', 'sur', 'avec',
'il', 'elle', 'on', 'nous', 'vous', 'ils', 'elles', 'ce', 'cet',
'cette', 'ces', 'son', 'sa', 'ses', 'leur', 'leurs', 'du', 'pas',
'mais', 'ou', 'où', 'si', 'ne', 'se', 'sans', 'sous', 'vers', 'à'
]);
// Color palette for words
const colors = [
'text-indigo-600', 'text-blue-600', 'text-green-600',
'text-yellow-600', 'text-red-600', 'text-purple-600',
'text-pink-600', 'text-gray-600', 'text-teal-600',
'text-orange-600'
];
generateBtn.addEventListener('click', generateWordCloud);
clearBtn.addEventListener('click', clearAll);
function clearAll() {
textInput.value = '';
wordCloudContainer.innerHTML = '<p class="text-gray-400">Le nuage de mots apparaîtra ici</p>';
updateStatistics(0, 0, 0);
if (chart) {
chart.destroy();
chart = null;
}
}
function generateWordCloud() {
const text = textInput.value.trim();
if (!text) {
alert('Veuillez entrer du texte à analyser');
return;
}
// Process text and count words
const words = processText(text);
const wordCounts = countWords(words);
const sortedWords = sortWords(wordCounts);
// Limit to max words
const maxWords = parseInt(maxWordsInput.value) || 50;
const limitedWords = sortedWords.slice(0, maxWords);
// Update statistics
updateStatistics(
Object.keys(wordCounts).length,
words.length,
limitedWords.length
);
// Generate word cloud
generateWordCloudVisual(limitedWords);
// Generate chart
generateChart(limitedWords.slice(0, 10));
}
function processText(text) {
// Remove punctuation and split into words
const cleanedText = text.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()«»'"…]/g, '');
// Split into words
let words = cleanedText.split(/\s+/);
// Filter empty strings
words = words.filter(word => word.length > 0);
// Ignore case if needed
if (ignoreCaseCheckbox.checked) {
words = words.map(word => word.toLowerCase());
}
// Ignore common words if needed
if (ignoreCommonCheckbox.checked) {
words = words.filter(word => !commonWords.has(word.toLowerCase()));
}
return words;
}
function countWords(words) {
const counts = {};
for (const word of words) {
if (counts[word]) {
counts[word]++;
} else {
counts[word] = 1;
}
}
return counts;
}
function sortWords(wordCounts) {
return Object.entries(wordCounts)
.map(([word, count]) => ({ word, count }))
.sort((a, b) => b.count - a.count);
}
function generateWordCloudVisual(wordsData) {
if (wordsData.length === 0) {
wordCloudContainer.innerHTML = '<p class="text-gray-400">Aucun mot à afficher après filtrage</p>';
return;
}
// Find min and max counts for scaling
const counts = wordsData.map(item => item.count);
const maxCount = Math.max(...counts);
const minCount = Math.min(...counts);
wordCloudContainer.innerHTML = '';
wordsData.forEach(item => {
// Calculate font size based on frequency (logarithmic scale)
const scale = Math.log(item.count) / Math.log(maxCount);
const fontSize = 12 + (scale * 36); // Between 12px and 48px
// Random rotation between -30 and 30 degrees
const rotation = Math.floor(Math.random() * 60) - 30;
// Random color from palette
const color = colors[Math.floor(Math.random() * colors.length)];
// Create word element
const wordEl = document.createElement('div');
wordEl.className = `word-item ${color} ${animateCheckbox.checked ? 'floating' : ''}`;
wordEl.style.fontSize = `${fontSize}px`;
wordEl.style.transform = `rotate(${rotation}deg)`;
wordEl.style.opacity = 0.8 + (scale * 0.2); // More opaque for larger words
wordEl.textContent = item.word;
// Add tooltip with count
wordEl.title = `Apparaît ${item.count} fois`;
wordCloudContainer.appendChild(wordEl);
});
}
function updateStatistics(uniqueWords, totalWords, analyzedWords) {
uniqueWordsEl.textContent = uniqueWords;
totalWordsEl.textContent = totalWords;
analyzedWordsEl.textContent = analyzedWords;
// Calculate density (percentage of unique words)
const density = totalWords > 0 ? (uniqueWords / totalWords * 100).toFixed(1) : 0;
densityEl.textContent = `${density}%`;
}
function generateChart(topWords) {
const ctx = document.getElementById('chart').getContext('2d');
// Destroy previous chart if exists
if (chart) {
chart.destroy();
}
if (topWords.length === 0) {
return;
}
const labels = topWords.map(item => item.word);
const data = topWords.map(item => item.count);
chart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Fréquence',
data: data,
backgroundColor: [
'rgba(79, 70, 229, 0.7)',
'rgba(59, 130, 246, 0.7)',
'rgba(16, 185, 129, 0.7)',
'rgba(234, 179, 8, 0.7)',
'rgba(239, 68, 68, 0.7)',
'rgba(139, 92, 246, 0.7)',
'rgba(236, 72, 153, 0.7)',
'rgba(75, 85, 99, 0.7)',
'rgba(20, 184, 166, 0.7)',
'rgba(249, 115, 22, 0.7)'
],
borderColor: [
'rgba(79, 70, 229, 1)',
'rgba(59, 130, 246, 1)',
'rgba(16, 185, 129, 1)',
'rgba(234, 179, 8, 1)',
'rgba(239, 68, 68, 1)',
'rgba(139, 92, 246, 1)',
'rgba(236, 72, 153, 1)',
'rgba(75, 85, 99, 1)',
'rgba(20, 184, 166, 1)',
'rgba(249, 115, 22, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Nombre d\'occurrences'
}
},
x: {
title: {
display: true,
text: 'Mots'
}
}
},
plugins: {
legend: {
display: false
},
tooltip: {
callbacks: {
label: function(context) {
return `Fréquence: ${context.raw}`;
}
}
}
}
}
});
}
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=masterseb/nuage" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html>