Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash· GoogleHugging Face LMArena Human Preference 140K

Create HTML, CSS, JS code that calculate statistics from a chat history containing raw…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4.1-2025-04-14’s answer.
  1. User

    Create HTML, CSS, JS code that calculate statistics from a chat history containing raw messages between different contacts and groups.

  2. Gemini Assistant

    This project will create a web-based tool to analyze chat history. You'll paste your raw chat log, and it will calculate various statistics like total messages, words, activity per contact, and hourly activity.

    Key Features:

    • Total messages and words.
    • Average words per message.
    • Messages and words contributed by each contact/group.
    • Most active hour of the day.
    • Most active contacts/groups (by messages and words).

    Assumed Chat Log Format: For this code to work effectively, your raw chat history needs to follow a consistent line-by-line format. A common format looks like this:

    [HH:MM] Sender Name: Message content here [HH:MM] Another Sender: This is another message. [HH:MM] Group Name: And here's a message from a group. This line is a continuation of the previous message.

    Example Chat Log Data (to paste into the textarea):

    [09:01] Alice: Hey Bob, how are you doing today?
    [09:05] Bob: I'm good, thanks! How about you?
    [09:06] Alice: I'm great! Just working on a new project.
    [09:15] Group Chat: Don't forget about the meeting at 10 AM!
    [09:16] Alice: Oh, thanks for the reminder!
    [10:00] Bob: Hey everyone, the meeting is starting now.
    [10:05] Alice: Joining now!
    [10:30] Group Chat: The meeting went well. Action items are in the shared document.
    [11:00] Alice: Thanks for the update!
    [11:00] Bob: 👍
    [11:00] Charlie: Okay, got it.
    [11:01] Charlie: I will review the document.
    [12:00] Alice: Anyone up for lunch?
    [12:05] Bob: I'm in! Where do you want to go?
    [13:00] Alice: Back from lunch!
    [14:15] Group Chat: Reminder: Project deadline is Friday.
    [14:16] Charlie: Understood.
    [15:30] Bob: Testing new features.
    [15:35] Alice: Looks good so far!
    [16:00] Charlie: Finished my part.
    [16:00] Bob: Great work, Charlie!
    [16:01] Alice: Awesome!
    [17:00] Alice: See you all tomorrow!
    

    1. index.html (The Structure)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Chat Statistics Calculator</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div class="container">
            <h1>Chat Statistics Calculator</h1>
    
            <div class="input-section">
                <p>Paste your raw chat history below. Ensure each message starts with `[HH:MM] Sender Name: `</p>
                <p>Example: <code>[09:01] Alice: Hey there!</code></p>
                <textarea id="chatHistoryInput" placeholder="Paste chat history here..."></textarea>
                <button id="calculateBtn">Calculate Statistics</button>
            </div>
    
            <div class="stats-section">
                <h2 id="overallStatsTitle">Overall Statistics</h2>
                <div id="overallStats" class="stats-card">
                    <p><strong>Total Messages:</strong> <span id="totalMessages">0</span></p>
                    <p><strong>Total Words:</strong> <span id="totalWords">0</span></p>
                    <p><strong>Average Words per Message:</strong> <span id="avgWordsPerMessage">0</span></p>
                </div>
    
                <h2 id="senderStatsTitle">Activity Per Contact/Group</h2>
                <div class="sort-options">
                    Sort by:
                    <button class="sort-btn active" data-sort-by="messages">Messages</button>
                    <button class="sort-btn" data-sort-by="words">Words</button>
                </div>
                <div id="senderStats" class="stats-card">
                    <table id="senderStatsTable">
                        <thead>
                            <tr>
                                <th>Sender</th>
                                <th>Messages</th>
                                <th>Messages (%)</th>
                                <th>Words</th>
                                <th>Words (%)</th>
                            </tr>
                        </thead>
                        <tbody>
                            <!-- Data will be inserted here by JS -->
                        </tbody>
                    </table>
                </div>
    
                <h2 id="hourlyStatsTitle">Hourly Activity</h2>
                <div id="hourlyStats" class="stats-card">
                    <ul id="hourlyActivityList">
                        <!-- Data will be inserted here by JS -->
                    </ul>
                </div>
            </div>
        </div>
    
        <script src="script.js"></script>
    </body>
    </html>
    

    2. style.css (The Styling)

    body {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        margin: 0;
        padding: 20px;
        background-color: #f4f7f6;
        color: #333;
        line-height: 1.6;
    }
    
    .container {
        max-width: 900px;
        margin: 30px auto;
        background-color: #ffffff;
        border-radius: 10px;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
        padding: 30px;
    }
    
    h1, h2 {
        color: #2c3e50;
        text-align: center;
        margin-bottom: 25px;
    }
    
    h2 {
        border-bottom: 2px solid #e0e0e0;
        padding-bottom: 10px;
        margin-top: 40px;
    }
    
    .input-section {
        margin-bottom: 30px;
        text-align: center;
    }
    
    textarea {
        width: calc(100% - 20px);
        height: 250px;
        padding: 10px;
        margin-bottom: 15px;
        border: 1px solid #ccc;
        border-radius: 8px;
        font-size: 1rem;
        resize: vertical;
        box-sizing: border-box; /* Include padding in width calculation */
        font-family: monospace;
    }
    
    button {
        background-color: #3498db;
        color: white;
        border: none;
        padding: 12px 25px;
        border-radius: 8px;
        font-size: 1.1rem;
        cursor: pointer;
        transition: background-color 0.3s ease;
    }
    
    button:hover {
        background-color: #2980b9;
    }
    
    .stats-section {
        margin-top: 30px;
    }
    
    .stats-card {
        background-color: #fdfdfd;
        border: 1px solid #e9e9e9;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    }
    
    #overallStats p {
        font-size: 1.1rem;
        margin: 8px 0;
    }
    
    #overallStats strong {
        color: #555;
    }
    
    table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 15px;
    }
    
    table th, table td {
        padding: 12px 15px;
        border: 1px solid #ddd;
        text-align: left;
    }
    
    table th {
        background-color: #ecf0f1;
        font-weight: bold;
        color: #444;
    }
    
    table tr:nth-child(even) {
        background-color: #f9f9f9;
    }
    
    table tr:hover {
        background-color: #f1f1f1;
    }
    
    #hourlyActivityList {
        list-style: none;
        padding: 0;
    }
    
    #hourlyActivityList li {
        background-color: #eaf6fd;
        margin-bottom: 8px;
        padding: 10px 15px;
        border-radius: 5px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-left: 5px solid #3498db;
    }
    
    #hourlyActivityList li span.count {
        font-weight: bold;
        color: #2c3e50;
        background-color: #ffffff;
        padding: 5px 10px;
        border-radius: 3px;
        min-width: 40px;
        text-align: center;
    }
    
    .sort-options {
        text-align: center;
        margin-bottom: 15px;
    }
    
    .sort-options .sort-btn {
        background-color: #bdc3c7;
        color: #333;
        padding: 8px 15px;
        margin: 0 5px;
        font-size: 0.95rem;
        border-radius: 5px;
    }
    
    .sort-options .sort-btn.active {
        background-color: #3498db;
        color: white;
    }
    
    /* Hide stats sections by default until calculated */
    #overallStatsTitle, #overallStats,
    #senderStatsTitle, #senderStats,
    #hourlyStatsTitle, #hourlyStats {
        display: none;
    }
    

    3. script.js (The Logic)

    document.addEventListener('DOMContentLoaded', () => {
        const chatHistoryInput = document.getElementById('chatHistoryInput');
        const calculateBtn = document.getElementById('calculateBtn');
    
        const overallStatsTitle = document.getElementById('overallStatsTitle');
        const overallStatsDiv = document.getElementById('overallStats');
        const totalMessagesSpan = document.getElementById('totalMessages');
        const totalWordsSpan = document.getElementById('totalWords');
        const avgWordsPerMessageSpan = document.getElementById('avgWordsPerMessage');
    
        const senderStatsTitle = document.getElementById('senderStatsTitle');
        const senderStatsDiv = document.getElementById('senderStats');
        const senderStatsTableBody = document.querySelector('#senderStatsTable tbody');
        const sortButtons = document.querySelectorAll('.sort-btn');
    
        const hourlyStatsTitle = document.getElementById('hourlyStatsTitle');
        const hourlyStatsDiv = document.getElementById('hourlyStats');
        const hourlyActivityList = document.getElementById('hourlyActivityList');
    
        let parsedChatData = [];
        let senderStatistics = {};
        let hourlyActivity = new Array(24).fill(0); // 0-23 hours
        let currentSortBy = 'messages'; // Default sort
    
        calculateBtn.addEventListener('click', calculateStatistics);
    
        sortButtons.forEach(button => {
            button.addEventListener('click', () => {
                sortButtons.forEach(btn => btn.classList.remove('active'));
                button.classList.add('active');
                currentSortBy = button.dataset.sortBy;
                displaySenderStatistics(); // Re-display with new sort order
            });
        });
    
        function calculateStatistics() {
            const rawHistory = chatHistoryInput.value.trim();
            if (!rawHistory) {
                alert('Please paste some chat history to analyze.');
                resetDisplay();
                return;
            }
    
            parsedChatData = [];
            senderStatistics = {};
            hourlyActivity = new Array(24).fill(0);
    
            const lines = rawHistory.split('\n');
            let currentMessage = null;
    
            const messageRegex = /^\[(\d{2}:\d{2})\]\s*([^:]+):\s*(.*)$/; // [HH:MM] Sender: Message
    
            lines.forEach(line => {
                const match = line.match(messageRegex);
                if (match) {
                    // New message starts
                    const [_, time, sender, content] = match;
                    currentMessage = { time, sender: sender.trim(), message: content.trim() };
                    parsedChatData.push(currentMessage);
                } else if (currentMessage) {
                    // Continuation of the previous message
                    currentMessage.message += '\n' + line.trim();
                }
            });
    
            if (parsedChatData.length === 0) {
                alert('No messages found. Please check your chat history format. Expected: [HH:MM] Sender: Message');
                resetDisplay();
                return;
            }
    
            processParsedData();
            displayOverallStatistics();
            displaySenderStatistics();
            displayHourlyActivity();
            showStatsSections();
        }
    
        function processParsedData() {
            let totalMessages = parsedChatData.length;
            let totalWords = 0;
    
            parsedChatData.forEach(msg => {
                // Simple word count: split by spaces, filter empty strings
                const words = msg.message.split(/\s+/).filter(word => word.length > 0);
                const wordCount = words.length;
                totalWords += wordCount;
    
                // Update sender statistics
                if (!senderStatistics[msg.sender]) {
                    senderStatistics[msg.sender] = { messages: 0, words: 0 };
                }
                senderStatistics[msg.sender].messages++;
                senderStatistics[msg.sender].words += wordCount;
    
                // Update hourly activity
                const hour = parseInt(msg.time.substring(0, 2), 10);
                if (!isNaN(hour) && hour >= 0 && hour <= 23) {
                    hourlyActivity[hour]++;
                }
            });
    
            // Store totals for overall stats display
            Object.assign(senderStatistics, { _totalMessages: totalMessages, _totalWords: totalWords });
        }
    
        function displayOverallStatistics() {
            const totalMsgs = senderStatistics._totalMessages || 0;
            const totalWds = senderStatistics._totalWords || 0;
            const avgWords = totalMsgs > 0 ? (totalWds / totalMsgs).toFixed(2) : 0;
    
            totalMessagesSpan.textContent = totalMsgs;
            totalWordsSpan.textContent = totalWds;
            avgWordsPerMessageSpan.textContent = avgWords;
        }
    
        function displaySenderStatistics() {
            senderStatsTableBody.innerHTML = ''; // Clear previous entries
    
            const totalMsgs = senderStatistics._totalMessages;
            const totalWds = senderStatistics._totalWords;
    
            const sortedSenders = Object.keys(senderStatistics)
                .filter(key => !key.startsWith('_')) // Exclude internal total keys
                .sort((a, b) => {
                    const valA = senderStatistics[a][currentSortBy];
                    const valB = senderStatistics[b][currentSortBy];
                    return valB - valA; // Descending order
                });
    
            sortedSenders.forEach(sender => {
                const stats = senderStatistics[sender];
                const msgPercentage = totalMsgs > 0 ? ((stats.messages / totalMsgs) * 100).toFixed(2) : 0;
                const wordPercentage = totalWds > 0 ? ((stats.words / totalWds) * 100).toFixed(2) : 0;
    
                const row = senderStatsTableBody.insertRow();
                row.insertCell().textContent = sender;
                row.insertCell().textContent = stats.messages;
                row.insertCell().textContent = `${msgPercentage}%`;
                row.insertCell().textContent = stats.words;
                row.insertCell().textContent = `${wordPercentage}%`;
            });
        }
    
        function displayHourlyActivity() {
            hourlyActivityList.innerHTML = ''; // Clear previous entries
    
            // Create an array of {hour, count} objects
            const hourlyData = hourlyActivity.map((count, index) => ({
                hour: index,
                count: count
            }));
    
            // Sort by count descending
            hourlyData.sort((a, b) => b.count - a.count);
    
            hourlyData.forEach(data => {
                if (data.count > 0) { // Only show hours with activity
                    const listItem = document.createElement('li');
                    const hourFormatted = String(data.hour).padStart(2, '0'); // Format 0-9 as 00-09
                    listItem.innerHTML = `Hour ${hourFormatted}:00 - ${hourFormatted}:59 <span class="count">${data.count} messages</span>`;
                    hourlyActivityList.appendChild(listItem);
                }
            });
    
            if (hourlyActivityList.children.length === 0) {
                hourlyActivityList.innerHTML = '<li>No hourly activity found.</li>';
            }
        }
    
        function showStatsSections() {
            overallStatsTitle.style.display = 'block';
            overallStatsDiv.style.display = 'block';
            senderStatsTitle.style.display = 'block';
            senderStatsDiv.style.display = 'block';
            hourlyStatsTitle.style.display = 'block';
            hourlyStatsDiv.style.display = 'block';
        }
    
        function resetDisplay() {
            totalMessagesSpan.textContent = '0';
            totalWordsSpan.textContent = '0';
            avgWordsPerMessageSpan.textContent = '0';
            senderStatsTableBody.innerHTML = '';
            hourlyActivityList.innerHTML = '';
    
            overallStatsTitle.style.display = 'none';
            overallStatsDiv.style.display = 'none';
            senderStatsTitle.style.display = 'none';
            senderStatsDiv.style.display = 'none';
            hourlyStatsTitle.style.display = 'none';
            hourlyStatsDiv.style.display = 'none';
        }
    
        // Initialize display state
        resetDisplay();
    });
    

    How to Use:

    1. Save the files:
      • Save the HTML code as index.html.
      • Save the CSS code as style.css in the same directory.
      • Save the JavaScript code as script.js in the same directory.
    2. Open index.html: Double-click index.html in your file explorer, or open it with your web browser.
    3. Paste Chat History: Copy your raw chat history (following the specified format) and paste it into the large text area.
    4. Click "Calculate Statistics": The page will update with the calculated statistics.
    5. Sort Sender Stats: Use the "Sort by" buttons to change the order of the sender statistics table.

    This setup provides a robust and user-friendly tool for basic chat log analysis.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.