File size: 7,219 Bytes
0beddd2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"id": "mSG4QdpNvio4"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"source": [
"!pip install kenlm"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "f3jTUbzZv442",
"outputId": "76eb7e32-227b-4cef-a20f-38ae936c4d8a"
},
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: kenlm in /usr/local/lib/python3.12/dist-packages (0.3.0)\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"from huggingface_hub import hf_hub_download\n",
"import kenlm\n",
"\n",
"# Step 1: Download the .arpa file from the dataset repository\n",
"print(\"Downloading KenLM ARPA file from Hugging Face Dataset...\")\n",
"model_path = hf_hub_download(\n",
" repo_id=\"BeitTigreAI/tigre-data-kenLM\",\n",
" filename=\"tigre-data-kenLM.arpa\",\n",
" repo_type=\"dataset\", # 🔑 This is critical! It's a dataset repo, not a model\n",
" local_dir=\"./kenlm_model\"\n",
")\n",
"\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Q8KTpqAqvj4d",
"outputId": "6fface0f-230d-49a6-ea02-02ab06850f46"
},
"execution_count": 17,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Downloading KenLM ARPA file from Hugging Face Dataset...\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import kenlm\n",
"\n",
"# Load the language model\n",
"model = kenlm.Model(model_path)\n",
"\n",
"# Example sentences\n",
"sentences = [\n",
" \"አነ ምሩር ህሌኮ ምን ኪደትለ አፌተ\",\n",
" \"እብ ረቢኩም ምን ተአምኖ፡ ለወለት ልብዬ ወዕንቼተ\",\n",
" \"ህተ ትብል ሑዬቱ ወአነ እብል ሕቼተ\"\n",
"]\n",
"\n",
"def test_sentence(sentence):\n",
" log_prob = model.score(sentence)\n",
" perplexity = model.perplexity(sentence)\n",
" print(f\"Sentence: {sentence}\")\n",
" print(f\"Log10 Probability: {log_prob:.4f}\")\n",
" print(f\"Perplexity: {perplexity:.4f}\")\n",
" print(\"-\" * 50)\n",
"\n",
"for sent in sentences:\n",
" test_sentence(sent)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UHem-Q4Kvslb",
"outputId": "d9a947a0-01ae-402f-e592-0a9c6951d962"
},
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Sentence: አነ ምሩር ህሌኮ ምን ኪደትለ አፌተ\n",
"Log10 Probability: -27.5345\n",
"Perplexity: 8580.2515\n",
"--------------------------------------------------\n",
"Sentence: እብ ረቢኩም ምን ተአምኖ፡ ለወለት ልብዬ ወዕንቼተ\n",
"Log10 Probability: -34.3204\n",
"Perplexity: 19500.8208\n",
"--------------------------------------------------\n",
"Sentence: ህተ ትብል ሑዬቱ ወአነ እብል ሕቼተ\n",
"Log10 Probability: -28.4631\n",
"Perplexity: 11645.5115\n",
"--------------------------------------------------\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"<!DOCTYPE html>\n",
"<html lang=\"en\">\n",
"<head>\n",
" <meta charset=\"UTF-8\" />\n",
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\n",
" <title>Tigre LM Output Explanation</title>\n",
" <style>\n",
" body {\n",
" font-family: Arial, sans-serif;\n",
" line-height: 1.6;\n",
" color: #333;\n",
" max-width: 600px;\n",
" margin: 2rem auto;\n",
" padding: 1rem;\n",
" }\n",
" h2 {\n",
" color: #2c3e50;\n",
" border-bottom: 2px solid #3498db;\n",
" padding-bottom: 0.3em;\n",
" }\n",
" table {\n",
" width: 100%;\n",
" border-collapse: collapse;\n",
" margin: 1.2em 0;\n",
" }\n",
" th, td {\n",
" padding: 0.8em;\n",
" text-align: left;\n",
" border-bottom: 1px solid #ddd;\n",
" }\n",
" th {\n",
" background-color: #f2f2f2;\n",
" font-weight: bold;\n",
" }\n",
" ul {\n",
" margin: 0.5em 0;\n",
" padding-left: 1.5em;\n",
" }\n",
" .checkmark {\n",
" color: #27ae60;\n",
" font-weight: bold;\n",
" }\n",
" </style>\n",
"</head>\n",
"<body>\n",
"\n",
" <h2>Understanding the Output</h2>\n",
"\n",
" <table>\n",
" <tr>\n",
" <th>Score</th>\n",
" <th>Meaning</th>\n",
" <th>Ideal</th>\n",
" </tr>\n",
" <tr>\n",
" <td><strong>Log10 Probability</strong></td>\n",
" <td>Log of sentence probability. Higher (less negative) = more fluent.</td>\n",
" <td>Closer to 0</td>\n",
" </tr>\n",
" <tr>\n",
" <td><strong>Perplexity</strong></td>\n",
" <td>Measures surprise. Lower = more natural.</td>\n",
" <td>As low as possible</td>\n",
" </tr>\n",
" <tr>\n",
" <td><strong>OOV</strong><br><em>(in word scores)</em></td>\n",
" <td><code>True</code> = word not in vocabulary. Affects confidence.<br>\n",
" <code>False</code> = word is known.</td>\n",
" <td><code>False</code></td>\n",
" </tr>\n",
" </table>\n",
"\n",
" <p><span class=\"checkmark\">✅ Use this to:</span></p>\n",
" <ul>\n",
" <li>Compare fluency of different sentences</li>\n",
" <li>Rescore ASR outputs</li>\n",
" <li>Detect unnatural or grammatically odd phrases</li>\n",
" </ul>\n",
"\n",
"</body>\n",
"</html>"
],
"metadata": {
"id": "AF6K4_3R0wlM"
}
}
]
} |