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