├── README.md └── TextProcessingPart1.ipynb /README.md: -------------------------------------------------------------------------------- 1 | #TextProcessing, NLP, BOW, TFxIDF, LDA, Word2Vec, CBOW, Skipgram 2 | 3 | -------------------------------------------------------------------------------- /TextProcessingPart1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "TextProcessingPart1.ipynb", 7 | "version": "0.3.2", 8 | "provenance": [], 9 | "collapsed_sections": [] 10 | }, 11 | "kernelspec": { 12 | "name": "python3", 13 | "display_name": "Python 3" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "id": "view-in-github", 21 | "colab_type": "text" 22 | }, 23 | "source": [ 24 | "[View in Colaboratory](https://colab.research.google.com/github/denopas/TextProcessing/blob/master/TextProcessingPart1.ipynb)" 25 | ] 26 | }, 27 | { 28 | "metadata": { 29 | "id": "AnrOP7iHAPbI", 30 | "colab_type": "code", 31 | "colab": { 32 | "base_uri": "https://localhost:8080/", 33 | "height": 68 34 | }, 35 | "outputId": "91a21d93-ec0a-4853-9b15-9e87660326fe" 36 | }, 37 | "cell_type": "code", 38 | "source": [ 39 | "import re\n", 40 | "import nltk\n", 41 | "import pandas as pd\n", 42 | "import numpy as np\n", 43 | "import matplotlib.pyplot as plt\n", 44 | "pd.options.display.max_colwidth = 8000\n", 45 | "nltk.download('stopwords')\n" 46 | ], 47 | "execution_count": 2, 48 | "outputs": [ 49 | { 50 | "output_type": "stream", 51 | "text": [ 52 | "[nltk_data] Downloading package stopwords to /content/nltk_data...\n", 53 | "[nltk_data] Unzipping corpora/stopwords.zip.\n" 54 | ], 55 | "name": "stdout" 56 | }, 57 | { 58 | "output_type": "execute_result", 59 | "data": { 60 | "text/plain": [ 61 | "True" 62 | ] 63 | }, 64 | "metadata": { 65 | "tags": [] 66 | }, 67 | "execution_count": 2 68 | } 69 | ] 70 | }, 71 | { 72 | "metadata": { 73 | "id": "C_5iVzJ5Ahhi", 74 | "colab_type": "code", 75 | "colab": { 76 | "base_uri": "https://localhost:8080/", 77 | "height": 297 78 | }, 79 | "outputId": "a7a23923-a3b6-4ef7-e13d-cc58e5da63b9" 80 | }, 81 | "cell_type": "code", 82 | "source": [ 83 | "# TR: Örnek Türkçe dokümanlar \n", 84 | "# EN: Sample documents in Turkish\n", 85 | "docs = ['Açıklama projenin ortaklarından Rus enerji devi Gazprom dan geldi. Yıllık 63 milyar metreküp enerji',\n", 86 | " 'ilk günündeki 20 yarış heyecanlıydı, 109 puan toplayan Türkiye, 12 ülke arasında 9. oldu ve yarış tamamlandı',\n", 87 | " 'Cortananın yeni işletim sistemi Windows 10 un önemli bir parçası olduğunu belirten Microsoft ; Google Android ve iOS cihazlarındaki Dijital',\n", 88 | " 'Teknoloji devi Google, Android in MMM sürümüyle birlikte bir çok sistemsel hatasının düzeltileceğini',\n", 89 | " 'Siroz hastalığı ile ilgili detaylara dikkat çekerek, sağlıklı bir karaciğere sahip olmak hastalık için',\n", 90 | " 'Hastalık çoğu kez yıllarca doğru tanı konmaması veya ciddiye alınmaması sebebi ile kısırlaştırıcı etki yapabiliyor, kronik ağrı,',\n", 91 | " 'ilk 4 etaptan galibiyetle ayrılan 18 yaşındaki Razgatlıoğlu, Almanya daki yarışta 3. sırayı alarak ',\n", 92 | " 'Helal gıda pazarı sanki 860 milyar doların üzerinde' \n", 93 | "]\n", 94 | "# TR: Dokümanlara ait sınıflar \n", 95 | "# EN: Classes of documents\n", 96 | "classes = ['ekonomi', 'spor', 'teknoloji', 'teknoloji', 'saglik', 'saglik', 'spor', 'ekonomi']\n", 97 | "docs = np.array(docs)\n", 98 | "df_docs = pd.DataFrame({'Document': docs, \n", 99 | " 'Class': classes})\n", 100 | "df_docs = df_docs[['Document', 'Class']]\n", 101 | "df_docs" 102 | ], 103 | "execution_count": 3, 104 | "outputs": [ 105 | { 106 | "output_type": "execute_result", 107 | "data": { 108 | "text/html": [ 109 | "
\n", 110 | "\n", 123 | "\n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | " \n", 164 | " \n", 165 | " \n", 166 | " \n", 167 | " \n", 168 | " \n", 169 | " \n", 170 | " \n", 171 | " \n", 172 | " \n", 173 | "
DocumentClass
0Açıklama projenin ortaklarından Rus enerji devi Gazprom dan geldi. Yıllık 63 milyar metreküp enerjiekonomi
1ilk günündeki 20 yarış heyecanlıydı, 109 puan toplayan Türkiye, 12 ülke arasında 9. oldu ve yarış tamamlandıspor
2Cortananın yeni işletim sistemi Windows 10 un önemli bir parçası olduğunu belirten Microsoft ; Google Android ve iOS cihazlarındaki Dijitalteknoloji
3Teknoloji devi Google, Android in MMM sürümüyle birlikte bir çok sistemsel hatasının düzeltileceğiniteknoloji
4Siroz hastalığı ile ilgili detaylara dikkat çekerek, sağlıklı bir karaciğere sahip olmak hastalık içinsaglik
5Hastalık çoğu kez yıllarca doğru tanı konmaması veya ciddiye alınmaması sebebi ile kısırlaştırıcı etki yapabiliyor, kronik ağrı,saglik
6ilk 4 etaptan galibiyetle ayrılan 18 yaşındaki Razgatlıoğlu, Almanya daki yarışta 3. sırayı alarakspor
7Helal gıda pazarı sanki 860 milyar doların üzerindeekonomi
\n", 174 | "
" 175 | ], 176 | "text/plain": [ 177 | " Document \\\n", 178 | "0 Açıklama projenin ortaklarından Rus enerji devi Gazprom dan geldi. Yıllık 63 milyar metreküp enerji \n", 179 | "1 ilk günündeki 20 yarış heyecanlıydı, 109 puan toplayan Türkiye, 12 ülke arasında 9. oldu ve yarış tamamlandı \n", 180 | "2 Cortananın yeni işletim sistemi Windows 10 un önemli bir parçası olduğunu belirten Microsoft ; Google Android ve iOS cihazlarındaki Dijital \n", 181 | "3 Teknoloji devi Google, Android in MMM sürümüyle birlikte bir çok sistemsel hatasının düzeltileceğini \n", 182 | "4 Siroz hastalığı ile ilgili detaylara dikkat çekerek, sağlıklı bir karaciğere sahip olmak hastalık için \n", 183 | "5 Hastalık çoğu kez yıllarca doğru tanı konmaması veya ciddiye alınmaması sebebi ile kısırlaştırıcı etki yapabiliyor, kronik ağrı, \n", 184 | "6 ilk 4 etaptan galibiyetle ayrılan 18 yaşındaki Razgatlıoğlu, Almanya daki yarışta 3. sırayı alarak \n", 185 | "7 Helal gıda pazarı sanki 860 milyar doların üzerinde \n", 186 | "\n", 187 | " Class \n", 188 | "0 ekonomi \n", 189 | "1 spor \n", 190 | "2 teknoloji \n", 191 | "3 teknoloji \n", 192 | "4 saglik \n", 193 | "5 saglik \n", 194 | "6 spor \n", 195 | "7 ekonomi " 196 | ] 197 | }, 198 | "metadata": { 199 | "tags": [] 200 | }, 201 | "execution_count": 3 202 | } 203 | ] 204 | }, 205 | { 206 | "metadata": { 207 | "id": "eJ4y8IIuA_kw", 208 | "colab_type": "code", 209 | "colab": { 210 | "base_uri": "https://localhost:8080/", 211 | "height": 153 212 | }, 213 | "outputId": "ba9d4647-a97a-4896-a96b-c25a284d3932" 214 | }, 215 | "cell_type": "code", 216 | "source": [ 217 | "#@title Default title text\n", 218 | "WPT = nltk.WordPunctTokenizer()\n", 219 | "stop_word_list = nltk.corpus.stopwords.words('turkish')\n", 220 | "\n", 221 | "def norm_doc(single_doc):\n", 222 | " # TR: Dokümandan belirlenen özel karakterleri ve sayıları at\n", 223 | " # EN: Remove special characters and numbers\n", 224 | " single_doc = re.sub(\" \\d+\", \" \", single_doc)\n", 225 | " pattern = r\"[{}]\".format(\",.;\") \n", 226 | " single_doc = re.sub(pattern, \"\", single_doc) \n", 227 | " # TR: Dokümanı küçük harflere çevir\n", 228 | " # EN: Convert document to lowercase\n", 229 | " single_doc = single_doc.lower()\n", 230 | " single_doc = single_doc.strip()\n", 231 | " # TR: Dokümanı token'larına ayır\n", 232 | " # EN: Tokenize documents\n", 233 | " tokens = WPT.tokenize(single_doc)\n", 234 | " # TR: Stop-word listesindeki kelimeler hariç al\n", 235 | " # EN: Filter out the stop-words \n", 236 | " filtered_tokens = [token for token in tokens if token not in stop_word_list]\n", 237 | " # TR: Dokümanı tekrar oluştur\n", 238 | " # EN: Reconstruct the document\n", 239 | " single_doc = ' '.join(filtered_tokens)\n", 240 | " return single_doc\n", 241 | "\n", 242 | "norm_docs = np.vectorize(norm_doc) #like magic :)\n", 243 | "normalized_documents = norm_docs(docs)\n", 244 | "print(normalized_documents)\n", 245 | "\n", 246 | "\n" 247 | ], 248 | "execution_count": 4, 249 | "outputs": [ 250 | { 251 | "output_type": "stream", 252 | "text": [ 253 | "['açıklama projenin ortaklarından rus enerji devi gazprom dan geldi yıllık milyar metreküp enerji'\n", 254 | " 'ilk günündeki yarış heyecanlıydı puan toplayan türkiye ülke arasında oldu yarış tamamlandı'\n", 255 | " 'cortananın yeni işletim sistemi windows un önemli bir parçası olduğunu belirten microsoft google android ios cihazlarındaki dijital'\n", 256 | " 'teknoloji devi google android in mmm sürümüyle birlikte bir sistemsel hatasının düzeltileceğini'\n", 257 | " 'siroz hastalığı ilgili detaylara dikkat çekerek sağlıklı bir karaciğere sahip olmak hastalık'\n", 258 | " 'hastalık çoğu yıllarca doğru tanı konmaması ciddiye alınmaması sebebi kısırlaştırıcı etki yapabiliyor kronik ağrı'\n", 259 | " 'ilk etaptan galibiyetle ayrılan yaşındaki razgatlıoğlu almanya daki yarışta sırayı alarak'\n", 260 | " 'helal gıda pazarı milyar doların üzerinde']\n" 261 | ], 262 | "name": "stdout" 263 | } 264 | ] 265 | }, 266 | { 267 | "metadata": { 268 | "id": "pbbYWJ-uJ0ds", 269 | "colab_type": "code", 270 | "colab": { 271 | "base_uri": "https://localhost:8080/", 272 | "height": 788 273 | }, 274 | "outputId": "c1992e20-f773-4597-a088-8cb0740a149b" 275 | }, 276 | "cell_type": "code", 277 | "source": [ 278 | "#@title Default title text\n", 279 | "# TR: 1.Terim Sayma Adımları\n", 280 | "# EN: 1.Term Counting Steps\n", 281 | "from sklearn.feature_extraction.text import CountVectorizer\n", 282 | "BoW_Vector = CountVectorizer(min_df = 0., max_df = 1.)\n", 283 | "BoW_Matrix = BoW_Vector.fit_transform(normalized_documents)\n", 284 | "#print (BoW_Matrix)\n", 285 | "\n", 286 | "# TR: BoW_Vector içerisindeki tüm öznitelikleri al\n", 287 | "# EN: Fetch al features in BoW_Vector\n", 288 | "features = BoW_Vector.get_feature_names()\n", 289 | "print (\"features[50]:\" + features[46])\n", 290 | "print (\"features[52]:\" +features[48])\n", 291 | "\n", 292 | "BoW_Matrix = BoW_Matrix.toarray()\n", 293 | "print(BoW_Matrix)\n", 294 | "# TR: Doküman - öznitelik matrisini göster\n", 295 | "# EN: Print document by term matrice\n", 296 | "BoW_df = pd.DataFrame(BoW_Matrix, columns = features)\n", 297 | "BoW_df\n", 298 | "#print(BoW_df.info())" 299 | ], 300 | "execution_count": 5, 301 | "outputs": [ 302 | { 303 | "output_type": "stream", 304 | "text": [ 305 | "features[50]:metreküp\n", 306 | "features[52]:milyar\n", 307 | "[[0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 2 0 0 0 1 1 0 0 0 0 0 0 0\n", 308 | " 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0\n", 309 | " 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0]\n", 310 | " [0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0\n", 311 | " 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1\n", 312 | " 1 0 0 0 2 0 0 0 0 0 0 0 0 1 0]\n", 313 | " [0 0 0 1 0 0 0 0 1 1 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0\n", 314 | " 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0\n", 315 | " 0 1 1 0 0 0 0 1 0 0 0 0 1 0 0]\n", 316 | " [0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0\n", 317 | " 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0\n", 318 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n", 319 | " [0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0\n", 320 | " 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0\n", 321 | " 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0]\n", 322 | " [0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0\n", 323 | " 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0\n", 324 | " 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0]\n", 325 | " [1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0\n", 326 | " 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0\n", 327 | " 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0]\n", 328 | " [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1\n", 329 | " 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", 330 | " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]]\n" 331 | ], 332 | "name": "stdout" 333 | }, 334 | { 335 | "output_type": "execute_result", 336 | "data": { 337 | "text/html": [ 338 | "
\n", 339 | "\n", 352 | "\n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | " \n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | " \n", 438 | " \n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | " \n", 464 | " \n", 465 | " \n", 466 | " \n", 467 | " \n", 468 | " \n", 469 | " \n", 470 | " \n", 471 | " \n", 472 | " \n", 473 | " \n", 474 | " \n", 475 | " \n", 476 | " \n", 477 | " \n", 478 | " \n", 479 | " \n", 480 | " \n", 481 | " \n", 482 | " \n", 483 | " \n", 484 | " \n", 485 | " \n", 486 | " \n", 487 | " \n", 488 | " \n", 489 | " \n", 490 | " \n", 491 | " \n", 492 | " \n", 493 | " \n", 494 | " \n", 495 | " \n", 496 | " \n", 497 | " \n", 498 | " \n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | " \n", 514 | " \n", 515 | " \n", 516 | " \n", 517 | " \n", 518 | " \n", 519 | " \n", 520 | " \n", 521 | " \n", 522 | " \n", 523 | " \n", 524 | " \n", 525 | " \n", 526 | " \n", 527 | " \n", 528 | " \n", 529 | " \n", 530 | " \n", 531 | " \n", 532 | " \n", 533 | " \n", 534 | " \n", 535 | " \n", 536 | " \n", 537 | " \n", 538 | " \n", 539 | " \n", 540 | " \n", 541 | " \n", 542 | " \n", 543 | " \n", 544 | " \n", 545 | " \n", 546 | " \n", 547 | " \n", 548 | " \n", 549 | " \n", 550 | " \n", 551 | " \n", 552 | " \n", 553 | " \n", 554 | " \n", 555 | " \n", 556 | " \n", 557 | " \n", 558 | " \n", 559 | " \n", 560 | " \n", 561 | " \n", 562 | " \n", 563 | " \n", 564 | " \n", 565 | " \n", 566 | " \n", 567 | " \n", 568 | " \n", 569 | " \n", 570 | " \n", 571 | " \n", 572 | " \n", 573 | "
alarakalmanyaalınmamasıandroidarasındaayrılanaçıklamaağrıbelirtenbir...yarıştayaşındakiyeniyıllarcayıllıkçekerekçoğuönemliülkeüzerinde
00000001000...0000100000
10000100000...0000000010
20001000011...0010000100
30001000001...0000000000
40000000001...0000010000
50010000100...0001001000
61100010000...1100000000
70000000000...0000000001
\n", 574 | "

8 rows × 87 columns

\n", 575 | "
" 576 | ], 577 | "text/plain": [ 578 | " alarak almanya alınmaması android arasında ayrılan açıklama ağrı \\\n", 579 | "0 0 0 0 0 0 0 1 0 \n", 580 | "1 0 0 0 0 1 0 0 0 \n", 581 | "2 0 0 0 1 0 0 0 0 \n", 582 | "3 0 0 0 1 0 0 0 0 \n", 583 | "4 0 0 0 0 0 0 0 0 \n", 584 | "5 0 0 1 0 0 0 0 1 \n", 585 | "6 1 1 0 0 0 1 0 0 \n", 586 | "7 0 0 0 0 0 0 0 0 \n", 587 | "\n", 588 | " belirten bir ... yarışta yaşındaki yeni yıllarca yıllık \\\n", 589 | "0 0 0 ... 0 0 0 0 1 \n", 590 | "1 0 0 ... 0 0 0 0 0 \n", 591 | "2 1 1 ... 0 0 1 0 0 \n", 592 | "3 0 1 ... 0 0 0 0 0 \n", 593 | "4 0 1 ... 0 0 0 0 0 \n", 594 | "5 0 0 ... 0 0 0 1 0 \n", 595 | "6 0 0 ... 1 1 0 0 0 \n", 596 | "7 0 0 ... 0 0 0 0 0 \n", 597 | "\n", 598 | " çekerek çoğu önemli ülke üzerinde \n", 599 | "0 0 0 0 0 0 \n", 600 | "1 0 0 0 1 0 \n", 601 | "2 0 0 1 0 0 \n", 602 | "3 0 0 0 0 0 \n", 603 | "4 1 0 0 0 0 \n", 604 | "5 0 1 0 0 0 \n", 605 | "6 0 0 0 0 0 \n", 606 | "7 0 0 0 0 1 \n", 607 | "\n", 608 | "[8 rows x 87 columns]" 609 | ] 610 | }, 611 | "metadata": { 612 | "tags": [] 613 | }, 614 | "execution_count": 5 615 | } 616 | ] 617 | }, 618 | { 619 | "metadata": { 620 | "id": "SJJgDizsKBGn", 621 | "colab_type": "code", 622 | "colab": { 623 | "base_uri": "https://localhost:8080/", 624 | "height": 1434 625 | }, 626 | "outputId": "8630a196-aa81-486b-e46b-490c216d6cf3" 627 | }, 628 | "cell_type": "code", 629 | "source": [ 630 | "# TR: 2.TFxIdf Hesaplama Adımları\n", 631 | "# EN: 2.TFxIdf Calculation Steps\n", 632 | "from sklearn.feature_extraction.text import TfidfVectorizer\n", 633 | "Tfidf_Vector = TfidfVectorizer(min_df = 0., max_df = 1., use_idf = True)\n", 634 | "Tfidf_Matrix = Tfidf_Vector.fit_transform(normalized_documents)\n", 635 | "Tfidf_Matrix = Tfidf_Matrix.toarray()\n", 636 | "print(np.round(Tfidf_Matrix, 3))\n", 637 | "# TR: Tfidf_Vector içerisindeki tüm öznitelikleri al\n", 638 | "# EN: Fetch al features in Tfidf_Vector\n", 639 | "features = Tfidf_Vector.get_feature_names()\n", 640 | "# TR: Doküman - öznitelik matrisini göster\n", 641 | "# EN: Print document by term matrice\n", 642 | "Tfidf_df = pd.DataFrame(np.round(Tfidf_Matrix, 3), columns = features)\n", 643 | "Tfidf_df" 644 | ], 645 | "execution_count": 6, 646 | "outputs": [ 647 | { 648 | "output_type": "stream", 649 | "text": [ 650 | "[[0. 0. 0. 0. 0. 0. 0.263 0. 0. 0. 0. 0.\n", 651 | " 0. 0. 0. 0.263 0. 0.221 0. 0. 0. 0. 0. 0.527\n", 652 | " 0. 0. 0. 0.263 0.263 0. 0. 0. 0. 0. 0. 0.\n", 653 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.263 0.\n", 654 | " 0.221 0. 0. 0. 0. 0.263 0. 0. 0.263 0. 0. 0.263\n", 655 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 656 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.263 0. 0.\n", 657 | " 0. 0. 0. ]\n", 658 | " [0. 0. 0. 0. 0.27 0. 0. 0. 0. 0. 0. 0.\n", 659 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 660 | " 0. 0. 0. 0. 0. 0. 0.27 0. 0. 0. 0. 0.\n", 661 | " 0.27 0. 0.226 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 662 | " 0. 0. 0.27 0. 0. 0. 0. 0. 0. 0.27 0. 0.\n", 663 | " 0. 0. 0. 0. 0. 0. 0. 0. 0.27 0. 0. 0.27\n", 664 | " 0.27 0. 0. 0. 0.54 0. 0. 0. 0. 0. 0. 0.\n", 665 | " 0. 0.27 0. ]\n", 666 | " [0. 0. 0. 0.21 0. 0. 0. 0. 0.251 0.181 0. 0.\n", 667 | " 0.251 0.251 0. 0. 0. 0. 0.251 0. 0. 0. 0. 0.\n", 668 | " 0. 0. 0. 0. 0. 0.21 0. 0. 0. 0. 0. 0.\n", 669 | " 0. 0. 0. 0. 0.251 0.251 0. 0. 0. 0. 0. 0.251\n", 670 | " 0. 0. 0. 0.251 0. 0. 0.251 0. 0. 0. 0. 0.\n", 671 | " 0. 0. 0. 0. 0.251 0. 0. 0. 0. 0. 0. 0.\n", 672 | " 0. 0.251 0.251 0. 0. 0. 0. 0.251 0. 0. 0. 0.\n", 673 | " 0.251 0. 0. ]\n", 674 | " [0. 0. 0. 0.257 0. 0. 0. 0. 0. 0.222 0.307 0.\n", 675 | " 0. 0. 0. 0. 0. 0.257 0. 0. 0. 0. 0.307 0.\n", 676 | " 0. 0. 0. 0. 0. 0.257 0. 0. 0. 0. 0.307 0.\n", 677 | " 0. 0. 0. 0.307 0. 0. 0. 0. 0. 0. 0. 0.\n", 678 | " 0. 0.307 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 679 | " 0. 0. 0. 0. 0. 0.307 0.307 0. 0. 0. 0.307 0.\n", 680 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 681 | " 0. 0. 0. ]\n", 682 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.216 0. 0.\n", 683 | " 0. 0. 0. 0. 0.298 0. 0. 0.298 0. 0. 0. 0.\n", 684 | " 0. 0. 0. 0. 0. 0. 0. 0. 0.25 0.298 0. 0.\n", 685 | " 0. 0.298 0. 0. 0. 0. 0.298 0. 0. 0. 0. 0.\n", 686 | " 0. 0. 0. 0. 0.298 0. 0. 0. 0. 0. 0. 0.\n", 687 | " 0.298 0.298 0. 0.298 0. 0. 0. 0. 0. 0. 0. 0.\n", 688 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.298 0.\n", 689 | " 0. 0. 0. ]\n", 690 | " [0. 0. 0.27 0. 0. 0. 0. 0.27 0. 0. 0. 0.27\n", 691 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.27 0. 0.\n", 692 | " 0. 0.27 0. 0. 0. 0. 0. 0. 0.226 0. 0. 0.\n", 693 | " 0. 0. 0. 0. 0. 0. 0. 0.27 0.27 0.27 0. 0.\n", 694 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 695 | " 0. 0. 0.27 0. 0. 0. 0. 0. 0. 0.27 0. 0.\n", 696 | " 0. 0. 0. 0.27 0. 0. 0. 0. 0.27 0. 0. 0.27\n", 697 | " 0. 0. 0. ]\n", 698 | " [0.306 0.306 0. 0. 0. 0.306 0. 0. 0. 0. 0. 0.\n", 699 | " 0. 0. 0.306 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 700 | " 0.306 0. 0.306 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 701 | " 0. 0. 0.256 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 702 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.306 0.\n", 703 | " 0. 0. 0. 0. 0. 0. 0. 0.306 0. 0. 0. 0.\n", 704 | " 0. 0. 0. 0. 0. 0.306 0.306 0. 0. 0. 0. 0.\n", 705 | " 0. 0. 0. ]\n", 706 | " [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 707 | " 0. 0. 0. 0. 0. 0. 0. 0. 0.419 0. 0. 0.\n", 708 | " 0. 0. 0. 0. 0. 0. 0. 0.419 0. 0. 0. 0.419\n", 709 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 710 | " 0.351 0. 0. 0. 0. 0. 0. 0.419 0. 0. 0. 0.\n", 711 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 712 | " 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", 713 | " 0. 0. 0.419]]\n" 714 | ], 715 | "name": "stdout" 716 | }, 717 | { 718 | "output_type": "execute_result", 719 | "data": { 720 | "text/html": [ 721 | "
\n", 722 | "\n", 735 | "\n", 736 | " \n", 737 | " \n", 738 | " \n", 739 | " \n", 740 | " \n", 741 | " \n", 742 | " \n", 743 | " \n", 744 | " \n", 745 | " \n", 746 | " \n", 747 | " \n", 748 | " \n", 749 | " \n", 750 | " \n", 751 | " \n", 752 | " \n", 753 | " \n", 754 | " \n", 755 | " \n", 756 | " \n", 757 | " \n", 758 | " \n", 759 | " \n", 760 | " \n", 761 | " \n", 762 | " \n", 763 | " \n", 764 | " \n", 765 | " \n", 766 | " \n", 767 | " \n", 768 | " \n", 769 | " \n", 770 | " \n", 771 | " \n", 772 | " \n", 773 | " \n", 774 | " \n", 775 | " \n", 776 | " \n", 777 | " \n", 778 | " \n", 779 | " \n", 780 | " \n", 781 | " \n", 782 | " \n", 783 | " \n", 784 | " \n", 785 | " \n", 786 | " \n", 787 | " \n", 788 | " \n", 789 | " \n", 790 | " \n", 791 | " \n", 792 | " \n", 793 | " \n", 794 | " \n", 795 | " \n", 796 | " \n", 797 | " \n", 798 | " \n", 799 | " \n", 800 | " \n", 801 | " \n", 802 | " \n", 803 | " \n", 804 | " \n", 805 | " \n", 806 | " \n", 807 | " \n", 808 | " \n", 809 | " \n", 810 | " \n", 811 | " \n", 812 | " \n", 813 | " \n", 814 | " \n", 815 | " \n", 816 | " \n", 817 | " \n", 818 | " \n", 819 | " \n", 820 | " \n", 821 | " \n", 822 | " \n", 823 | " \n", 824 | " \n", 825 | " \n", 826 | " \n", 827 | " \n", 828 | " \n", 829 | " \n", 830 | " \n", 831 | " \n", 832 | " \n", 833 | " \n", 834 | " \n", 835 | " \n", 836 | " \n", 837 | " \n", 838 | " \n", 839 | " \n", 840 | " \n", 841 | " \n", 842 | " \n", 843 | " \n", 844 | " \n", 845 | " \n", 846 | " \n", 847 | " \n", 848 | " \n", 849 | " \n", 850 | " \n", 851 | " \n", 852 | " \n", 853 | " \n", 854 | " \n", 855 | " \n", 856 | " \n", 857 | " \n", 858 | " \n", 859 | " \n", 860 | " \n", 861 | " \n", 862 | " \n", 863 | " \n", 864 | " \n", 865 | " \n", 866 | " \n", 867 | " \n", 868 | " \n", 869 | " \n", 870 | " \n", 871 | " \n", 872 | " \n", 873 | " \n", 874 | " \n", 875 | " \n", 876 | " \n", 877 | " \n", 878 | " \n", 879 | " \n", 880 | " \n", 881 | " \n", 882 | " \n", 883 | " \n", 884 | " \n", 885 | " \n", 886 | " \n", 887 | " \n", 888 | " \n", 889 | " \n", 890 | " \n", 891 | " \n", 892 | " \n", 893 | " \n", 894 | " \n", 895 | " \n", 896 | " \n", 897 | " \n", 898 | " \n", 899 | " \n", 900 | " \n", 901 | " \n", 902 | " \n", 903 | " \n", 904 | " \n", 905 | " \n", 906 | " \n", 907 | " \n", 908 | " \n", 909 | " \n", 910 | " \n", 911 | " \n", 912 | " \n", 913 | " \n", 914 | " \n", 915 | " \n", 916 | " \n", 917 | " \n", 918 | " \n", 919 | " \n", 920 | " \n", 921 | " \n", 922 | " \n", 923 | " \n", 924 | " \n", 925 | " \n", 926 | " \n", 927 | " \n", 928 | " \n", 929 | " \n", 930 | " \n", 931 | " \n", 932 | " \n", 933 | " \n", 934 | " \n", 935 | " \n", 936 | " \n", 937 | " \n", 938 | " \n", 939 | " \n", 940 | " \n", 941 | " \n", 942 | " \n", 943 | " \n", 944 | " \n", 945 | " \n", 946 | " \n", 947 | " \n", 948 | " \n", 949 | " \n", 950 | " \n", 951 | " \n", 952 | " \n", 953 | " \n", 954 | " \n", 955 | " \n", 956 | "
alarakalmanyaalınmamasıandroidarasındaayrılanaçıklamaağrıbelirtenbir...yarıştayaşındakiyeniyıllarcayıllıkçekerekçoğuönemliülkeüzerinde
00.0000.0000.000.0000.000.0000.2630.000.0000.000...0.0000.0000.0000.000.2630.0000.000.0000.000.000
10.0000.0000.000.0000.270.0000.0000.000.0000.000...0.0000.0000.0000.000.0000.0000.000.0000.270.000
20.0000.0000.000.2100.000.0000.0000.000.2510.181...0.0000.0000.2510.000.0000.0000.000.2510.000.000
30.0000.0000.000.2570.000.0000.0000.000.0000.222...0.0000.0000.0000.000.0000.0000.000.0000.000.000
40.0000.0000.000.0000.000.0000.0000.000.0000.216...0.0000.0000.0000.000.0000.2980.000.0000.000.000
50.0000.0000.270.0000.000.0000.0000.270.0000.000...0.0000.0000.0000.270.0000.0000.270.0000.000.000
60.3060.3060.000.0000.000.3060.0000.000.0000.000...0.3060.3060.0000.000.0000.0000.000.0000.000.000
70.0000.0000.000.0000.000.0000.0000.000.0000.000...0.0000.0000.0000.000.0000.0000.000.0000.000.419
\n", 957 | "

8 rows × 87 columns

\n", 958 | "
" 959 | ], 960 | "text/plain": [ 961 | " alarak almanya alınmaması android arasında ayrılan açıklama ağrı \\\n", 962 | "0 0.000 0.000 0.00 0.000 0.00 0.000 0.263 0.00 \n", 963 | "1 0.000 0.000 0.00 0.000 0.27 0.000 0.000 0.00 \n", 964 | "2 0.000 0.000 0.00 0.210 0.00 0.000 0.000 0.00 \n", 965 | "3 0.000 0.000 0.00 0.257 0.00 0.000 0.000 0.00 \n", 966 | "4 0.000 0.000 0.00 0.000 0.00 0.000 0.000 0.00 \n", 967 | "5 0.000 0.000 0.27 0.000 0.00 0.000 0.000 0.27 \n", 968 | "6 0.306 0.306 0.00 0.000 0.00 0.306 0.000 0.00 \n", 969 | "7 0.000 0.000 0.00 0.000 0.00 0.000 0.000 0.00 \n", 970 | "\n", 971 | " belirten bir ... yarışta yaşındaki yeni yıllarca yıllık \\\n", 972 | "0 0.000 0.000 ... 0.000 0.000 0.000 0.00 0.263 \n", 973 | "1 0.000 0.000 ... 0.000 0.000 0.000 0.00 0.000 \n", 974 | "2 0.251 0.181 ... 0.000 0.000 0.251 0.00 0.000 \n", 975 | "3 0.000 0.222 ... 0.000 0.000 0.000 0.00 0.000 \n", 976 | "4 0.000 0.216 ... 0.000 0.000 0.000 0.00 0.000 \n", 977 | "5 0.000 0.000 ... 0.000 0.000 0.000 0.27 0.000 \n", 978 | "6 0.000 0.000 ... 0.306 0.306 0.000 0.00 0.000 \n", 979 | "7 0.000 0.000 ... 0.000 0.000 0.000 0.00 0.000 \n", 980 | "\n", 981 | " çekerek çoğu önemli ülke üzerinde \n", 982 | "0 0.000 0.00 0.000 0.00 0.000 \n", 983 | "1 0.000 0.00 0.000 0.27 0.000 \n", 984 | "2 0.000 0.00 0.251 0.00 0.000 \n", 985 | "3 0.000 0.00 0.000 0.00 0.000 \n", 986 | "4 0.298 0.00 0.000 0.00 0.000 \n", 987 | "5 0.000 0.27 0.000 0.00 0.000 \n", 988 | "6 0.000 0.00 0.000 0.00 0.000 \n", 989 | "7 0.000 0.00 0.000 0.00 0.419 \n", 990 | "\n", 991 | "[8 rows x 87 columns]" 992 | ] 993 | }, 994 | "metadata": { 995 | "tags": [] 996 | }, 997 | "execution_count": 6 998 | } 999 | ] 1000 | }, 1001 | { 1002 | "metadata": { 1003 | "id": "P28E1GOJDp8Z", 1004 | "colab_type": "code", 1005 | "colab": { 1006 | "base_uri": "https://localhost:8080/", 1007 | "height": 153 1008 | }, 1009 | "outputId": "1a53a479-1683-4b6c-fae8-e5a5741d9e1e" 1010 | }, 1011 | "cell_type": "code", 1012 | "source": [ 1013 | "#LDA: Topic Modeling\n", 1014 | "from sklearn.decomposition import LatentDirichletAllocation\n", 1015 | "number_of_topics = 4\n", 1016 | "BoW_Matrix = BoW_Vector.fit_transform(normalized_documents)\n", 1017 | "LDA = LatentDirichletAllocation(n_components = number_of_topics, \n", 1018 | " max_iter = 10, \n", 1019 | " learning_offset = 50.,\n", 1020 | " random_state = 0,\n", 1021 | " learning_method = 'online').fit(BoW_Matrix)\n", 1022 | "features = BoW_Vector.get_feature_names()\n", 1023 | "for t_id, topic in enumerate(LDA.components_):\n", 1024 | " print (\"Topic %d:\" % (t_id))\n", 1025 | " print (\" \".join([features[i]\n", 1026 | " for i in topic.argsort()[:-number_of_topics - 1:-1]]))" 1027 | ], 1028 | "execution_count": 19, 1029 | "outputs": [ 1030 | { 1031 | "output_type": "stream", 1032 | "text": [ 1033 | "Topic 0:\n", 1034 | "daki alınmaması alarak çoğu\n", 1035 | "Topic 1:\n", 1036 | "enerji yarış yeni türkiye\n", 1037 | "Topic 2:\n", 1038 | "olmak sağlıklı detaylara siroz\n", 1039 | "Topic 3:\n", 1040 | "üzerinde gıda helal pazarı\n" 1041 | ], 1042 | "name": "stdout" 1043 | } 1044 | ] 1045 | } 1046 | ] 1047 | } --------------------------------------------------------------------------------