Untitled

mail@pastecode.io avatar
unknown
javascript
7 months ago
3.0 kB
1
Indexable
Never
// Function to calculate Jaro-Winkler distance between two strings
function calculateJaroWinklerDistance(str1, str2) {
    const matchWindow = Math.floor(Math.max(str1.length, str2.length) / 2) - 1;
    const matches1 = Array(str1.length).fill(false);
    const matches2 = Array(str2.length).fill(false);

    let matches = 0;
    let transpositions = 0;

    // Count matching characters
    for (let i = 0; i < str1.length; i++) {
        const start = Math.max(0, i - matchWindow);
        const end = Math.min(i + matchWindow + 1, str2.length);

        for (let j = start; j < end; j++) {
            if (!matches2[j] && str1[i] === str2[j]) {
                matches1[i] = true;
                matches2[j] = true;
                matches++;
                break;
            }
        }
    }

    // Count transpositions
    let k = 0;
    for (let i = 0; i < str1.length; i++) {
        if (matches1[i]) {
            while (!matches2[k]) {
                k++;
            }
            if (str1[i] !== str2[k]) {
                transpositions++;
            }
            k++;
        }
    }

    // Calculate Jaro similarity
    const jaroSimilarity = matches / str1.length;

    // Calculate Jaro-Winkler similarity
    const jaroWinklerSimilarity = jaroSimilarity + (0.1 * transpositions * (1 - jaroSimilarity));

    return jaroWinklerSimilarity;
}

// Get the messages container element
const messagesContainer = document.getElementById('chat-content');

// Get all the chat message elements from the messages container
const chatMessages = messagesContainer.querySelectorAll('.messages-content div p');

// Array to store hidden messages
const hiddenMessages = [];

// Initialize the index of the last visible message
let lastVisibleMessageIndex = 0;

// Iterate through each chat message
for (let i = 0; i < chatMessages.length; i++) {
    const currentMessage = chatMessages[i];
    const currentText = currentMessage.textContent.trim();

    // Check against the last visible message
    const lastVisibleMessage = chatMessages[lastVisibleMessageIndex];
    const lastVisibleText = lastVisibleMessage.textContent.trim();

    // Calculate Jaro-Winkler distance and use a similarity threshold
    const similarity = calculateJaroWinklerDistance(currentText, lastVisibleText);
    const similarityThreshold = 0.80; // Adjust the threshold as needed

    // If the current message is not similar to the last visible message, update lastVisibleMessageIndex
    if (similarity < similarityThreshold) {
        lastVisibleMessageIndex = i;
    }

    // Hide or log the message based on similarity
    if (i !== lastVisibleMessageIndex) {
        currentMessage.style.display = 'none';
        hiddenMessages.push({ index: i, text: currentText });
    } else {
        console.log('Not Hidden:', currentText);
    }
}

// Log the hidden messages
console.log('Hidden Messages:', hiddenMessages);
Leave a Comment