Spaces:
Build error
Build error
Upload datasets.ipynb
Browse files- datasets.ipynb +140 -12
datasets.ipynb
CHANGED
|
@@ -287,15 +287,28 @@
|
|
| 287 |
},
|
| 288 |
{
|
| 289 |
"cell_type": "code",
|
| 290 |
-
"execution_count":
|
| 291 |
"metadata": {},
|
| 292 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
"source": [
|
| 294 |
"from pymongo import MongoClient\n",
|
| 295 |
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
|
| 296 |
"from textblob import TextBlob as tb\n",
|
| 297 |
"import numpy as np\n",
|
| 298 |
"import math\n",
|
|
|
|
|
|
|
| 299 |
"\n",
|
| 300 |
"class Database:\n",
|
| 301 |
" @staticmethod\n",
|
|
@@ -311,7 +324,12 @@
|
|
| 311 |
" cursor = collection.find().limit(limit)\n",
|
| 312 |
" documents = [doc for doc in cursor]\n",
|
| 313 |
" document_count = len(documents)\n",
|
| 314 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
"\n",
|
| 316 |
"class Tf:\n",
|
| 317 |
" @staticmethod\n",
|
|
@@ -335,24 +353,134 @@
|
|
| 335 |
" return Database.get_input_documents(limit)\n",
|
| 336 |
"\n",
|
| 337 |
"# Kullanım örneği\n",
|
| 338 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
"\n",
|
| 340 |
"# Dokümanları işleyerek TF-IDF hesaplama\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
"\n",
|
| 342 |
-
"blobs = [tb(doc.get('text', '')) for doc in documents] # veya 'title' kullanarak başlıkları işleyebilirsiniz\n",
|
| 343 |
-
"all_words = set(word for blob in blobs for word in blob.words)\n",
|
| 344 |
"\n",
|
| 345 |
-
"
|
| 346 |
-
"for word in all_words:\n",
|
| 347 |
-
" tfidf_scores[word] = [Tf.tfidf(word, blob, blobs) for blob in blobs]\n",
|
| 348 |
"\n",
|
| 349 |
-
"print(\"TF-IDF Skorları:\")\n",
|
| 350 |
-
"for word, scores in tfidf_scores.items():\n",
|
| 351 |
-
" print(f\"Kelime: {word}, Skorlar: {scores}\")\n",
|
| 352 |
"\n",
|
| 353 |
"\n",
|
| 354 |
"\n",
|
| 355 |
"\n",
|
|
|
|
|
|
|
| 356 |
"\"\"\"turkish_stop_words = set([\n",
|
| 357 |
" 'ad', 'adım', 'ah', 'ama', 'an', 'ancak', 'araba', 'aralar', 'aslında', \n",
|
| 358 |
" 'b', 'bazı', 'belirli', 'ben', 'bence', 'bunu', 'burada', 'biz', 'bu', 'buna', 'çünkü', \n",
|
|
|
|
| 287 |
},
|
| 288 |
{
|
| 289 |
"cell_type": "code",
|
| 290 |
+
"execution_count": 20,
|
| 291 |
"metadata": {},
|
| 292 |
+
"outputs": [
|
| 293 |
+
{
|
| 294 |
+
"data": {
|
| 295 |
+
"text/plain": [
|
| 296 |
+
"\"turkish_stop_words = set([\\n 'ad', 'adım', 'ah', 'ama', 'an', 'ancak', 'araba', 'aralar', 'aslında', \\n 'b', 'bazı', 'belirli', 'ben', 'bence', 'bunu', 'burada', 'biz', 'bu', 'buna', 'çünkü', \\n 'da', 'de', 'demek', 'den', 'derken', 'değil', 'daha', 'dolayı', 'edilir', 'eğer', 'en', 'fakat', \\n 'genellikle', 'gibi', 'hem', 'her', 'herhangi', 'hiç', 'ise', 'işte', 'itibaren', 'iyi', 'kadar', \\n 'karşı', 'ki', 'kime', 'kısaca', 'mu', 'mü', 'nasıl', 'ne', 'neden', 'niye', 'o', 'olabilir', 'oluşur', \\n 'önce', 'şu', 'sadece', 'se', 'şey', 'şimdi', 'tabi', 'tüm', 've', 'ya', 'ya da', 'yani', 'yine'\\n])\\ndef calculate_tfidf(documents):\\n vectorizer = TfidfVectorizer(stop_words=turkish_stop_words, max_features=10000) # max_features ile özellik sayısını sınırlıyoruz\\n tfidf_matrix = vectorizer.fit_transform(documents)\\n feature_names = vectorizer.get_feature_names_out()\\n return tfidf_matrix, feature_names\\n\\n#feature_names lerin belirlenmesi grekir \\ntfidf_matrix, feature_names=calculate_tfidf(documents)\\n\\n\\n\\n# En yüksek TF-IDF skorlarına sahip anahtar kelimeleri çıkarın\\n#sıkışık format kullanmarak tf-ıdf matrisini işleme \\ndef get_top_n_keywords_sparse(n=10):\\n\\n # TF-IDF hesaplayıcı oluşturun\\n vectorizer = TfidfVectorizer()\\n\\n # Başlıklar ve metinler ile TF-IDF matrisini oluşturun\\n texts = Database.get_input_texts()\\n titles = Database.get_input_titles()\\n \\n\\n #title ve text değerlerini alarak vektörleştirdik.\\n tfidf_matrix = vectorizer.fit_transform(documents)\\n\\n # Özellik adlarını (kelimeleri) alın\\n\\n feature_names = vectorizer.get_feature_names_out()\\n\\n # TF-IDF sonuçlarını DataFrame'e dönüştürün\\n df = pd.DataFrame(tfidf_matrix.toarray(), columns=feature_names)\\n print(df)\\n keywords = {}\\n for i in range(tfidf_matrix.shape[0]):\\n row = tfidf_matrix[i].toarray().flatten() #list yapısından çıkarma \\n sorted_indices = row.argsort()[::-1] # Büyükten küçüğe sıralama\\n top_indices = sorted_indices[:n]\\n top_keywords = [feature_names[idx] for idx in top_indices]\\n keywords[i] = top_keywords\\n return keywords\""
|
| 297 |
+
]
|
| 298 |
+
},
|
| 299 |
+
"execution_count": 20,
|
| 300 |
+
"metadata": {},
|
| 301 |
+
"output_type": "execute_result"
|
| 302 |
+
}
|
| 303 |
+
],
|
| 304 |
"source": [
|
| 305 |
"from pymongo import MongoClient\n",
|
| 306 |
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
|
| 307 |
"from textblob import TextBlob as tb\n",
|
| 308 |
"import numpy as np\n",
|
| 309 |
"import math\n",
|
| 310 |
+
"import nltk \n",
|
| 311 |
+
"import matplotlib.pyplot as plt \n",
|
| 312 |
"\n",
|
| 313 |
"class Database:\n",
|
| 314 |
" @staticmethod\n",
|
|
|
|
| 324 |
" cursor = collection.find().limit(limit)\n",
|
| 325 |
" documents = [doc for doc in cursor]\n",
|
| 326 |
" document_count = len(documents)\n",
|
| 327 |
+
" \n",
|
| 328 |
+
" # Dökümanları isimlendir\n",
|
| 329 |
+
" named_documents = {f'döküman {i+1}': doc for i, doc in enumerate(documents)}\n",
|
| 330 |
+
" \n",
|
| 331 |
+
" return named_documents, document_count\n",
|
| 332 |
+
"\n",
|
| 333 |
"\n",
|
| 334 |
"class Tf:\n",
|
| 335 |
" @staticmethod\n",
|
|
|
|
| 353 |
" return Database.get_input_documents(limit)\n",
|
| 354 |
"\n",
|
| 355 |
"# Kullanım örneği\n",
|
| 356 |
+
"named_documents, document_count = Tf.get_input_documents(limit=3)\n",
|
| 357 |
+
"\n",
|
| 358 |
+
"#tf-ıdf ile döküman içerisinden kelime seçme \n",
|
| 359 |
+
"\n",
|
| 360 |
+
"def extract_keywords(tfidf_matrix, feature_names, top_n=10):\n",
|
| 361 |
+
" \"\"\"\n",
|
| 362 |
+
" Her döküman için anahtar kelimeleri seç.\n",
|
| 363 |
+
" :param tfidf_matrix: TF-IDF matris\n",
|
| 364 |
+
" :param feature_names: TF-IDF özellik isimleri\n",
|
| 365 |
+
" :param top_n: Her döküman için seçilecek anahtar kelime sayısı\n",
|
| 366 |
+
" :return: Anahtar kelimeler ve skorlari\n",
|
| 367 |
+
" \"\"\"\n",
|
| 368 |
+
" keywords = {}\n",
|
| 369 |
+
" for doc_idx, row in enumerate(tfidf_matrix):\n",
|
| 370 |
+
" # TF-IDF değerlerini ve özellik isimlerini al\n",
|
| 371 |
+
" scores = np.asarray(row.T.todense()).flatten()\n",
|
| 372 |
+
" sorted_indices = np.argsort(scores)[::-1] # Skorları azalan sırada\n",
|
| 373 |
+
" top_features = sorted_indices[:top_n]\n",
|
| 374 |
+
" \n",
|
| 375 |
+
" doc_keywords = [(feature_names[idx], scores[idx]) for idx in top_features]\n",
|
| 376 |
+
" keywords[f'document_{doc_idx+1}'] = doc_keywords\n",
|
| 377 |
+
" \n",
|
| 378 |
+
" return keywords\n",
|
| 379 |
"\n",
|
| 380 |
"# Dokümanları işleyerek TF-IDF hesaplama\n",
|
| 381 |
+
"#bloblist dökümanların bir listesi\n",
|
| 382 |
+
"bloblist = []\n",
|
| 383 |
+
"for i, blob in enumerate(bloblist):\n",
|
| 384 |
+
" print(\"Top words in document {}\".format(i + 1))\n",
|
| 385 |
+
" scores = {word: Tf.tfidf(word, blob, bloblist) for word in blob.words} #dökümanların içerisinde bulunan kelimeleri alır.\n",
|
| 386 |
+
" sorted_words = sorted(scores.items(), key=lambda x: x[1], reverse=True)\n",
|
| 387 |
+
" for word, score in sorted_words[:3]:\n",
|
| 388 |
+
" print(\"\\tWord: {}, TF-IDF: {}\".format(word, round(score, 5)))\n",
|
| 389 |
+
"\n",
|
| 390 |
+
"turkish_stop_words = [\n",
|
| 391 |
+
" 'ah', 'ama', 'an', 'ancak', 'araba', 'aralar', 'aslında', \n",
|
| 392 |
+
" 'b','başlayan','bağlı', 'bazı', 'belirli', 'ben', 'bence','birkaç','birlikte', 'bunu', 'burada','biten','biten' ,'biz', 'bu', 'buna', 'çünkü', \n",
|
| 393 |
+
" 'da', 'de', 'demek', 'den', 'derken', 'değil', 'daha', 'dolayı', 'edilir', 'eğer', 'en', 'fakat', \n",
|
| 394 |
+
" 'genellikle', 'gibi', 'hem', 'her', 'herhangi', 'hiç', 'ise', 'işte', 'itibaren', 'iyi', 'kadar', \n",
|
| 395 |
+
" 'karşı', 'ki', 'kime', 'kısaca', 'mu', 'mü', 'nasıl', 'ne', 'neden', 'niye', 'o', 'olasılıkla','olabilir', 'oluşur', \n",
|
| 396 |
+
" 'önce', 'şu', 'sadece', 'se', 'şey', 'şimdi', 'tabi', 'tüm', 've', 'ya', 'ya da','yanı' ,'yanı','yani','yılında','yılında','yetenekli', 'yine'\n",
|
| 397 |
+
"]\n",
|
| 398 |
+
"\n",
|
| 399 |
+
"#featuresların eklenmesi gerekir \n",
|
| 400 |
+
"def calculate_tfidf(documents, stop_words):\n",
|
| 401 |
+
" vectorizer = TfidfVectorizer(stop_words=stop_words, max_features=10000)\n",
|
| 402 |
+
" tfidf_matrix = vectorizer.fit_transform(documents)\n",
|
| 403 |
+
" feature_names = vectorizer.get_feature_names_out()\n",
|
| 404 |
+
" return tfidf_matrix, feature_names\n",
|
| 405 |
+
"\n",
|
| 406 |
+
"\n",
|
| 407 |
+
"#kelimelerin ortalama skorlarını hesaplama \n",
|
| 408 |
+
"def identify_low_tfidf_words(tfidf_matrix, feature_names, threshold=0.001):\n",
|
| 409 |
+
" # TF-IDF skorlarını toplayarak her kelimenin ortalama skorunu hesaplayın\n",
|
| 410 |
+
" avg_scores = np.mean(tfidf_matrix, axis=0).A1\n",
|
| 411 |
+
" low_tfidf_words = [feature_names[i] for i, score in enumerate(avg_scores) if score < threshold]\n",
|
| 412 |
+
" return low_tfidf_words\n",
|
| 413 |
+
"\n",
|
| 414 |
+
"#kelimelerin güncellenmesi \n",
|
| 415 |
+
"def update_stop_words(existing_stop_words, low_tfidf_words):\n",
|
| 416 |
+
" updated_stop_words = set(existing_stop_words) | set(low_tfidf_words)\n",
|
| 417 |
+
" return list(updated_stop_words)\n",
|
| 418 |
+
"\n",
|
| 419 |
+
"\n",
|
| 420 |
+
"def iterative_update(documents, initial_stop_words, iterations=5):\n",
|
| 421 |
+
" stop_words = set(initial_stop_words)\n",
|
| 422 |
+
" for _ in range(iterations):\n",
|
| 423 |
+
" tfidf_matrix, feature_names = calculate_tfidf(documents, stop_words)\n",
|
| 424 |
+
" low_tfidf_words = identify_low_tfidf_words(tfidf_matrix, feature_names)\n",
|
| 425 |
+
" stop_words = update_stop_words(stop_words, low_tfidf_words)\n",
|
| 426 |
+
" return list(stop_words)\n",
|
| 427 |
+
"stop_words= iterative_update\n",
|
| 428 |
+
"\n",
|
| 429 |
+
"\n",
|
| 430 |
+
"def main ():\n",
|
| 431 |
+
"\n",
|
| 432 |
+
"#anlam ilişkisini de kontrol edecek bir yapı oluşpturulacak title ile benzerlik kontrol ederek yüksek benzerlik içeren kelimler sıralnacak .\n",
|
| 433 |
+
"\n",
|
| 434 |
+
"# Dökümanları liste olarak al\n",
|
| 435 |
+
" documents_list = [doc.get('text', '') if isinstance(doc, dict) else doc for doc in list(named_documents.values())]\n",
|
| 436 |
+
"\n",
|
| 437 |
+
" #tf-ıdf hesaplama\n",
|
| 438 |
+
" tfidf_matrix, feature_names=calculate_tfidf(documents_list,stop_words)\n",
|
| 439 |
+
"\n",
|
| 440 |
+
"# Veritabanından dökümanları alın\n",
|
| 441 |
+
" named_documents, document_count = Database.get_input_documents(limit=3)\n",
|
| 442 |
+
"\n",
|
| 443 |
+
"#başalngıç stop değerleriyle yeni olanları arasında değişim yapma \n",
|
| 444 |
+
" initial_stop_words = turkish_stop_words\n",
|
| 445 |
+
"\n",
|
| 446 |
+
"# Stop-words listesini iteratif olarak güncelleyin\n",
|
| 447 |
+
" final_stop_words = iterative_update(documents_list, initial_stop_words)\n",
|
| 448 |
+
"\n",
|
| 449 |
+
" print(\"Güncellenmiş Stop-Words Listesi:\", final_stop_words)\n",
|
| 450 |
+
"\n",
|
| 451 |
+
"\n",
|
| 452 |
+
"# Sonuçları yazdır\n",
|
| 453 |
+
" print(\"İsimlendirilmiş Dökümanlar:\")\n",
|
| 454 |
+
" for name, doc in named_documents.items():\n",
|
| 455 |
+
" print(f\"{name}: {doc}\")\n",
|
| 456 |
+
"\n",
|
| 457 |
+
" print(\"\\nDökümanlar Listesi:\")\n",
|
| 458 |
+
" print(documents_list)\n",
|
| 459 |
+
"\n",
|
| 460 |
+
"#---------------------------------------------------------\n",
|
| 461 |
+
" blobs = [tb(doc) for doc in documents_list] # veya 'title' kullanarak başlıkları işleyebilirsiniz\n",
|
| 462 |
+
" all_words = set(word for blob in blobs for word in blob.words)\n",
|
| 463 |
+
"\n",
|
| 464 |
+
" tfidf_scores = {}\n",
|
| 465 |
+
" for word in all_words:\n",
|
| 466 |
+
" tfidf_scores[word] = [Tf.tfidf(word, blob, blobs) for blob in blobs]\n",
|
| 467 |
+
"\n",
|
| 468 |
+
" print(\"TF-IDF Skorları:\")\n",
|
| 469 |
+
" for word, scores in tfidf_scores.items():\n",
|
| 470 |
+
" print(f\"Kelime: {word}, Skorlar: {scores}\")\n",
|
| 471 |
+
"\n",
|
| 472 |
+
"\n",
|
| 473 |
+
"\n",
|
| 474 |
"\n",
|
|
|
|
|
|
|
| 475 |
"\n",
|
| 476 |
+
"#----------------------------------------------\n",
|
|
|
|
|
|
|
| 477 |
"\n",
|
|
|
|
|
|
|
|
|
|
| 478 |
"\n",
|
| 479 |
"\n",
|
| 480 |
"\n",
|
| 481 |
"\n",
|
| 482 |
+
"#alternatif keywordleri belirleme \n",
|
| 483 |
+
"#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n",
|
| 484 |
"\"\"\"turkish_stop_words = set([\n",
|
| 485 |
" 'ad', 'adım', 'ah', 'ama', 'an', 'ancak', 'araba', 'aralar', 'aslında', \n",
|
| 486 |
" 'b', 'bazı', 'belirli', 'ben', 'bence', 'bunu', 'burada', 'biz', 'bu', 'buna', 'çünkü', \n",
|