Thursday, September 12, 2024

Quick Bytes: A HTML And Javascript Readability Analyzer

 
This was a fun quick app that does an analysis on reading and grade levels and takes some input and then outputs the score and some custom grade level information.

===

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Readability Analyzer</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f9;
            color: #333;
            margin: 0;
            padding: 0;
        }
        .container {
            max-width: 800px;
            margin: 50px auto;
            background-color: white;
            padding: 20px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
        }
        h1 {
            text-align: center;
            color: #007bff;
        }
        textarea {
            width: 100%;
            height: 200px;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ddd;
            margin-bottom: 20px;
            font-size: 16px;
        }
        button {
            width: 48%;
            padding: 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 18px;
            cursor: pointer;
            margin-right: 2%;
        }
        button.clear {
            background-color: #dc3545;
        }
        button:hover {
            background-color: #0056b3;
        }
        button.clear:hover {
            background-color: #c82333;
        }
        .result {
            margin-top: 20px;
            padding: 20px;
            background-color: #e9ecef;
            border-radius: 5px;
        }
        .result p {
            font-size: 18px;
            margin: 10px 0;
        }
        footer {
            text-align: center;
            margin-top: 50px;
            font-size: 14px;
            color: #666;
        }
        @media (max-width: 600px) {
            .container {
                margin: 20px;
                padding: 15px;
            }
            button {
                padding: 10px;
                font-size: 16px;
            }
            textarea {
                height: 150px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>

   <div class="container">
    <h1>Readability Analyzer</h1>
    <textarea id="inputText" placeholder="Paste your text here..."></textarea>
    <div>
        <button onclick="calculateReadability()">Analyze Readability</button>
        <button class="clear" onclick="clearText()">Clear</button>
    </div>

    <div id="result" class="result" style="display: none;">
        <p><strong>Scores:</strong></p>
        <p id="fleschScore"></p>
        <p id="fogScore"></p>
        <p id="daleChallScore"></p>
    </div>
  </div>



    <footer>
        Readability Analyzer
    </footer>

    <script>
    function countSyllables(word) {
        word = word.toLowerCase();
        if(word.length <= 3) return 1;
        word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');
        word = word.replace(/^y/, '');
        const syllableMatch = word.match(/[aeiouy]{1,2}/g);
        return syllableMatch ? syllableMatch.length : 1;
    }

    function calculateReadability() {
        const text = document.getElementById('inputText').value;

        const sentences = text.split(/[.!?]+/).filter(Boolean).length;
        const words = text.split(/\s+/).filter(Boolean);
        const wordCount = words.length;
        const syllableCount = words.reduce((acc, word) => acc + countSyllables(word), 0);
        const difficultWordCount = words.filter(word => countSyllables(word) > 2).length;

        // Flesch-Kincaid Grade Level
        const fleschKincaid = 0.39 * (wordCount / sentences) + 11.8 * (syllableCount / wordCount) - 15.59;
        let fleschDescription = getGradeDescription(fleschKincaid);
        
        // Gunning Fog Index
        const gunningFog = 0.4 * ((wordCount / sentences) + 100 * (difficultWordCount / wordCount));
        let fogDescription = getGradeDescription(gunningFog);

        // Dale-Chall Readability
        const difficultWordPercentage = (difficultWordCount / wordCount) * 100;
        const daleChall = 0.1579 * difficultWordPercentage + 0.0496 * (wordCount / sentences) + (difficultWordPercentage > 5 ? 3.6365 : 0);
        let daleChallDescription = getGradeDescription(daleChall);

        // Display individual results
        document.getElementById('fleschScore').textContent = `Flesch-Kincaid: ${fleschKincaid.toFixed(2)} (${fleschDescription})`;
        document.getElementById('fogScore').textContent = `Gunning Fog: ${gunningFog.toFixed(2)} (${fogDescription})`;
        document.getElementById('daleChallScore').textContent = `Dale-Chall: ${daleChall.toFixed(2)} (${daleChallDescription})`;
        document.getElementById('result').style.display = 'block';
    }

    function getGradeDescription(score) {
        if (score <= 4) {
            return "K - 4th Grade: Very easy to read, suitable for young readers.";
        } else if (score <= 7) {
            return "5th - 7th Grade: Easy to read, suitable for children.";
        } else if (score <= 10) {
            return "8th - 10th Grade: Moderately challenging, suitable for teens.";
        } else {
            return "11th Grade and Above: Challenging, suitable for advanced readers or adults.";
        }
    }

    function clearText() {
        // Clear the input text area and hide the results
        document.getElementById('inputText').value = '';
        document.getElementById('result').style.display = 'none';
    }

</script>


</body>
</html>

Don't Forget To Update That Azure Key Vault URL When Creating A New Version...Wait...Just Get The Latest


 
Sometimes you need to update a Secret in an Azure Key Vault and you may be referencing that from an Environment Variable. 

If you use ADF when you reference a Key Vault in a Linked Service it goes to the latest version automatically. 

When adding it as an environment variable so a Function could access it (securely and the MS way), I've used:

@Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/mysecret/ec96f02080254f109c51a1f14cdb1931)

If the value was changed, I would often forget to update the version reference.

After some errors I've moved it to just be:

@Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/mysecret/)
 
In this way it does not need to reference the version so it will always get the latest value and there's nothing to remember for the specific reference. Just change it in Key Vault and that's it!