Doğal Dil İşlemeye Giriş

Doğal Dil İşleme (Natural Language Processing - NLP), bilgisayarların insan dilini anlama, yorumlama ve üretme yeteneğini geliştiren yapay zeka alanıdır. NLP, dilbilim, bilgisayar bilimi ve makine öğrenmesinin kesişiminde yer alır ve metin ve konuşma verilerini işlemek için çeşitli teknikler kullanır.
NLP'nin günlük hayattaki uygulamaları oldukça yaygındır:
Arama Motorları: Google, Bing gibi arama motorlarının sorgu anlama ve sonuç sıralama sistemleri
Sanal Asistanlar: Siri, Alexa, Google Assistant gibi sesli asistanlar
Çeviri Hizmetleri: Google Translate, DeepL gibi otomatik çeviri sistemleri
Sosyal Medya: Duygu analizi, spam tespiti, içerik moderasyonu
Müşteri Hizmetleri: Chatbot'lar ve otomatik yanıt sistemleri
Metin Madenciliği: Haber analizi, piyasa araştırmaları, akademik çalışmalar

NLP'nin Temel Zorlukları

İnsan dili, bilgisayarlar için işlenmesi zor bir veri türüdür çünkü:
1.Belirsizlik (Ambiguity): Aynı kelime farklı anlamlara gelebilir
2.Bağlam Bağımlılığı: Kelimelerin anlamı cümle içindeki konumuna göre değişir
3.Deyimler ve Mecazlar: Literal olmayan ifadeler
4.Dil Çeşitliliği: Farklı diller, lehçeler ve jargonlar
5.Yazım Hataları ve Kısaltmalar: Özellikle sosyal medya metinlerinde

NLP'nin Temel Bileşenleri

1. Tokenization (Belirteçleme)

Metni kelimeler, cümleler veya diğer anlamlı birimlere ayırma işlemidir.
import nltk from nltk.tokenize import word_tokenize, sent_tokenize import spacy # NLTK ile tokenization nltk.download('punkt') text = "Merhaba dünya! Bu bir NLP örneğidir. Python ile metin işleme yapıyoruz." # Kelime tokenization words = word_tokenize(text) print("Kelimeler:", words) # Cümle tokenization sentences = sent_tokenize(text) print("Cümleler:", sentences) # spaCy ile tokenization nlp = spacy.load("en_core_web_sm") # İngilizce model doc = nlp("Hello world! This is an NLP example.") print("spaCy tokens:") for token in doc: print(f"'{token.text}' - Lemma: {token.lemma_}, POS: {token.pos_}")

2. Stopword Removal (Durak Kelime Kaldırma)

"ve", "bir", "the", "is" gibi çok sık kullanılan ancak analiz için önemli olmayan kelimelerin kaldırılması.
from nltk.corpus import stopwords from nltk.tokenize import word_tokenize import string nltk.download('stopwords') def remove_stopwords(text, language='turkish'): # Tokenization tokens = word_tokenize(text.lower()) # Stopword'leri yükleme stop_words = set(stopwords.words(language)) # Noktalama işaretlerini ve stopword'leri kaldırma filtered_tokens = [word for word in tokens if word not in stop_words and word not in string.punctuation] return filtered_tokens # Örnek kullanım turkish_text = "Bu bir doğal dil işleme örneğidir ve çok faydalı bir tekniktir." filtered_words = remove_stopwords(turkish_text) print("Filtrelenmiş kelimeler:", filtered_words)

3. Stemming ve Lemmatization

Stemming: Kelimeleri kök formlarına indirgemek (hızlı ama bazen hatalı) Lemmatization: Kelimeleri sözlük formlarına çevirmek (daha doğru ama yavaş)
from nltk.stem import PorterStemmer, SnowballStemmer from nltk.stem import WordNetLemmatizer import nltk nltk.download('wordnet') # Stemming porter = PorterStemmer() snowball = SnowballStemmer('english') words = ["running", "runs", "ran", "easily", "fairly"] print("Orijinal kelimeler:", words) print("Porter Stemming:", [porter.stem(word) for word in words]) print("Snowball Stemming:", [snowball.stem(word) for word in words]) # Lemmatization lemmatizer = WordNetLemmatizer() print("Lemmatization:", [lemmatizer.lemmatize(word, pos='v') for word in words]) # Türkçe için zemberek kullanımı (alternatif) # pip install zemberek-python

4. Part-of-Speech (POS) Tagging

Kelimelerin dilbilgisel rollerini (isim, fiil, sıfat vb.) belirleme.
import nltk from nltk import pos_tag from nltk.tokenize import word_tokenize nltk.download('averaged_perceptron_tagger') def pos_tagging_demo(text): tokens = word_tokenize(text) pos_tags = pos_tag(tokens) print("POS Tagging Sonuçları:") for word, tag in pos_tags: print(f"{word:12} -> {tag}") return pos_tags # Örnek kullanım text = "The quick brown fox jumps over the lazy dog." pos_tagging_demo(text)

Temel NLP Kütüphaneleri

NLTK (Natural Language Toolkit)

NLTK, Python'da NLP için en eski ve kapsamlı kütüphanelerden biridir.
import nltk from nltk.sentiment import SentimentIntensityAnalyzer # Duygu analizi için VADER lexicon'ı indirme nltk.download('vader_lexicon') def sentiment_analysis_nltk(text): sia = SentimentIntensityAnalyzer() sentiment_scores = sia.polarity_scores(text) print(f"Metin: {text}") print(f"Pozitif: {sentiment_scores['pos']:.2f}") print(f"Nötr: {sentiment_scores['neu']:.2f}") print(f"Negatif: {sentiment_scores['neg']:.2f}") print(f"Bileşik Skor: {sentiment_scores['compound']:.2f}") # Genel duygu belirleme if sentiment_scores['compound'] >= 0.05: overall_sentiment = "Pozitif" elif sentiment_scores['compound'] <= -0.05: overall_sentiment = "Negatif" else: overall_sentiment = "Nötr" print(f"Genel Duygu: {overall_sentiment}\n") return overall_sentiment # Örnek kullanım texts = [ "I love this product! It's amazing!", "This is terrible. I hate it.", "The weather is okay today.", "Python is a great programming language for NLP." ] for text in texts: sentiment_analysis_nltk(text)

spaCy

spaCy, endüstriyel kullanım için optimize edilmiş, hızlı ve doğru bir NLP kütüphanesidir.
import spacy from spacy import displacy # spaCy modelini yükleme # python -m spacy download en_core_web_sm nlp = spacy.load("en_core_web_sm") def spacy_analysis_demo(text): doc = nlp(text) print(f"Metin: {text}\n") # Token analizi print("Token Analizi:") for token in doc: print(f"{token.text:12} | Lemma: {token.lemma_:12} | POS: {token.pos_:8} | " f"Tag: {token.tag_:8} | Dep: {token.dep_:12} | Shape: {token.shape_}") print("\nNamed Entity Recognition (NER):") for ent in doc.ents: print(f"{ent.text:20} -> {ent.label_:12} ({spacy.explain(ent.label_)})") print("\nCümle Segmentasyonu:") for sent in doc.sents: print(f"- {sent.text}") return doc # Örnek kullanım text = "Apple Inc. was founded by Steve Jobs in Cupertino, California on April 1, 1976." doc = spacy_analysis_demo(text) # Görselleştirme (Jupyter notebook'ta çalışır) # displacy.render(doc, style="ent", jupyter=True)

TextBlob

TextBlob, basit NLP görevleri için kullanıcı dostu bir API sunar.
from textblob import TextBlob def textblob_demo(text): blob = TextBlob(text) print(f"Orijinal Metin: {text}") print(f"Duygu Polaritesi: {blob.sentiment.polarity:.2f} (-1: Negatif, +1: Pozitif)") print(f"Öznel/Nesnel: {blob.sentiment.subjectivity:.2f} (0: Nesnel, 1: Öznel)") # Kelime frekansları print(f"Kelime Sayısı: {len(blob.words)}") print(f"Cümle Sayısı: {len(blob.sentences)}") # N-gram analizi print("Bigram'lar:", blob.ngrams(n=2)) # Dil tespiti (langdetect kütüphanesi gerekli) try: from langdetect import detect print(f"Tespit Edilen Dil: {detect(text)}") except: print("Dil tespiti için 'langdetect' kütüphanesi gerekli") return blob # Örnek kullanım texts = [ "I absolutely love this new smartphone! The camera quality is outstanding.", "The service was terrible and the food was cold.", "Python is a versatile programming language used in many applications." ] for text in texts: textblob_demo(text) print("-" * 50)

Gelişmiş NLP Uygulamaları

1. Metin Sınıflandırma

from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.naive_bayes import MultinomialNB from sklearn.pipeline import Pipeline from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report import pandas as pd def text_classification_demo(): # Örnek veri seti (gerçek uygulamada daha büyük veri seti kullanılır) data = { 'text': [ "I love this movie, it's fantastic!", "This film is terrible, waste of time.", "Great acting and amazing storyline.", "Boring and predictable plot.", "Outstanding cinematography and direction.", "Poor script and bad acting.", "Highly recommended for everyone.", "Don't watch this movie, it's awful.", "Brilliant performance by the lead actor.", "Disappointing ending and weak characters." ], 'sentiment': ['positive', 'negative', 'positive', 'negative', 'positive', 'negative', 'positive', 'negative', 'positive', 'negative'] } df = pd.DataFrame(data) # Veriyi eğitim ve test setlerine ayırma X_train, X_test, y_train, y_test = train_test_split( df['text'], df['sentiment'], test_size=0.3, random_state=42 ) # Pipeline oluşturma (TF-IDF + Naive Bayes) pipeline = Pipeline([ ('tfidf', TfidfVectorizer(stop_words='english', lowercase=True)), ('classifier', MultinomialNB()) ]) # Model eğitimi pipeline.fit(X_train, y_train) # Tahmin yapma y_pred = pipeline.predict(X_test) # Sonuçları değerlendirme print("Sınıflandırma Raporu:") print(classification_report(y_test, y_pred)) # Yeni metinler üzerinde tahmin new_texts = [ "This movie is absolutely amazing!", "I didn't like the story at all.", "The special effects were incredible." ] predictions = pipeline.predict(new_texts) probabilities = pipeline.predict_proba(new_texts) print("\nYeni Metinler için Tahminler:") for text, pred, prob in zip(new_texts, predictions, probabilities): confidence = max(prob) * 100 print(f"Metin: {text}") print(f"Tahmin: {pred} (Güven: {confidence:.1f}%)\n") text_classification_demo()

2. Anahtar Kelime Çıkarma

from sklearn.feature_extraction.text import TfidfVectorizer import numpy as np def keyword_extraction_tfidf(texts, top_k=5): """TF-IDF kullanarak anahtar kelime çıkarma""" # TF-IDF vektörizer vectorizer = TfidfVectorizer( max_features=1000, stop_words='english', ngram_range=(1, 2) # Unigram ve bigram ) # TF-IDF matrisini oluşturma tfidf_matrix = vectorizer.fit_transform(texts) feature_names = vectorizer.get_feature_names_out() # Her doküman için anahtar kelimeleri çıkarma for i, text in enumerate(texts): print(f"Doküman {i+1}:") print(f"Metin: {text[:100]}...") # Bu doküman için TF-IDF skorları tfidf_scores = tfidf_matrix[i].toarray()[0] # En yüksek skorlu kelimeleri bulma top_indices = np.argsort(tfidf_scores)[::-1][:top_k] print("Anahtar Kelimeler:") for idx in top_indices: if tfidf_scores[idx] > 0: print(f" {feature_names[idx]}: {tfidf_scores[idx]:.3f}") print("-" * 50) # Örnek kullanım documents = [ "Machine learning is a subset of artificial intelligence that focuses on algorithms and statistical models.", "Python is a popular programming language for data science and machine learning applications.", "Natural language processing enables computers to understand and process human language effectively.", "Deep learning uses neural networks with multiple layers to solve complex problems in AI." ] keyword_extraction_tfidf(documents)

3. Metin Özetleme

from textrank import TextRank4Keyword, TextRank4Sentence import nltk from nltk.tokenize import sent_tokenize nltk.download('punkt') def text_summarization_demo(text, num_sentences=3): """TextRank algoritması ile metin özetleme""" # Cümlelere ayırma sentences = sent_tokenize(text) if len(sentences) <= num_sentences: return text # TextRank ile cümle skorlama tr4s = TextRank4Sentence() tr4s.analyze(text, candidate_pos=['NOUN', 'PROPN'], window_size=4, lower=False) # En önemli cümleleri seçme summary_sentences = [] for item in tr4s.summarize(num_sentences): summary_sentences.append(item) # Özeti oluşturma summary = ' '.join(summary_sentences) return summary # Basit extractive summarization (frekans tabanlı) def simple_summarization(text, num_sentences=3): """Basit frekans tabanlı özetleme""" from collections import Counter import re # Cümlelere ayırma sentences = sent_tokenize(text) if len(sentences) <= num_sentences: return text # Kelimeleri çıkarma ve temizleme words = re.findall(r'\w+', text.lower()) # Stopword'leri kaldırma stop_words = set(stopwords.words('english')) words = [word for word in words if word not in stop_words] # Kelime frekansları word_freq = Counter(words) # Cümle skorlama sentence_scores = {} for sentence in sentences: sentence_words = re.findall(r'\w+', sentence.lower()) score = sum(word_freq[word] for word in sentence_words if word in word_freq) sentence_scores[sentence] = score # En yüksek skorlu cümleleri seçme top_sentences = sorted(sentence_scores.items(), key=lambda x: x[1], reverse=True)[:num_sentences] # Orijinal sıralamayı koruyarak özet oluşturma summary_sentences = [] for sentence in sentences: if any(sentence == sent[0] for sent in top_sentences): summary_sentences.append(sentence) return ' '.join(summary_sentences) # Örnek kullanım long_text = """ Artificial intelligence (AI) is intelligence demonstrated by machines, in contrast to the natural intelligence displayed by humans and animals. Leading AI textbooks define the field as the study of "intelligent agents": any device that perceives its environment and takes actions that maximize its chance of successfully achieving its goals. Colloquially, the term "artificial intelligence" is often used to describe machines that mimic "cognitive" functions that humans associate with the human mind, such as "learning" and "problem solving". The scope of AI is disputed: as machines become increasingly capable, tasks considered to require "intelligence" are often removed from the definition of AI, a phenomenon known as the AI effect. A quip in Tesler's Theorem says "AI is whatever hasn't been done yet." For instance, optical character recognition is frequently excluded from things considered to be AI, having become a routine technology. Modern machine capabilities generally classified as AI include successfully understanding human speech, competing at the highest level in strategic game systems, autonomously operating cars, intelligent routing in content delivery networks, and military simulations. Artificial intelligence was founded as an academic discipline in 1956, and in the years since has experienced several waves of optimism, followed by disappointment and the loss of funding (known as an "AI winter"), followed by new approaches, success and renewed funding. For most of its history, AI research has been divided into sub-fields that often fail to communicate with each other. These sub-fields are based on technical considerations, such as particular goals, the use of particular tools or the use of particular applications. """ print("Orijinal Metin Uzunluğu:", len(long_text.split())) print("\nBasit Özetleme:") summary = simple_summarization(long_text, 2) print(summary) print("\nÖzet Uzunluğu:", len(summary.split()))

Masaüstü Uygulamasında NLP Kullanımı

Tkinter ile NLP entegrasyonu örneği:
import tkinter as tk from tkinter import scrolledtext, messagebox, ttk from textblob import TextBlob import nltk from nltk.sentiment import SentimentIntensityAnalyzer from collections import Counter import re class NLPAnalyzerApp: def __init__(self, root): self.root = root self.root.title("NLP Metin Analiz Aracı") self.root.geometry("800x700") # NLTK verilerini indirme try: nltk.download('vader_lexicon', quiet=True) nltk.download('punkt', quiet=True) self.sia = SentimentIntensityAnalyzer() except: messagebox.showwarning("Uyarı", "NLTK verileri indirilemedi. Bazı özellikler çalışmayabilir.") self.setup_ui() def setup_ui(self): # Ana çerçeve main_frame = tk.Frame(self.root) main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) # Metin giriş alanı input_frame = tk.LabelFrame(main_frame, text="Metin Girişi", font=("Arial", 10, "bold")) input_frame.pack(fill=tk.BOTH, expand=True, pady=(0, 10)) self.text_input = scrolledtext.ScrolledText(input_frame, height=8, wrap=tk.WORD) self.text_input.pack(fill=tk.BOTH, expand=True, padx=5, pady=5) # Örnek metin ekleme butonu sample_btn = tk.Button(input_frame, text="Örnek Metin Ekle", command=self.add_sample_text) sample_btn.pack(pady=5) # Analiz butonları button_frame = tk.Frame(main_frame) button_frame.pack(fill=tk.X, pady=(0, 10)) tk.Button(button_frame, text="Duygu Analizi", command=self.sentiment_analysis, bg="#4CAF50", fg="white", font=("Arial", 9, "bold")).pack(side=tk.LEFT, padx=(0, 5)) tk.Button(button_frame, text="Kelime Frekansı", command=self.word_frequency, bg="#2196F3", fg="white", font=("Arial", 9, "bold")).pack(side=tk.LEFT, padx=5) tk.Button(button_frame, text="Temel İstatistikler", command=self.basic_stats, bg="#FF9800", fg="white", font=("Arial", 9, "bold")).pack(side=tk.LEFT, padx=5) tk.Button(button_frame, text="Temizle", command=self.clear_results, bg="#f44336", fg="white", font=("Arial", 9, "bold")).pack(side=tk.RIGHT) # Sonuç alanı result_frame = tk.LabelFrame(main_frame, text="Analiz Sonuçları", font=("Arial", 10, "bold")) result_frame.pack(fill=tk.BOTH, expand=True) self.result_text = scrolledtext.ScrolledText(result_frame, height=12, wrap=tk.WORD, state=tk.DISABLED) self.result_text.pack(fill=tk.BOTH, expand=True, padx=5, pady=5) def add_sample_text(self): sample = """Python is an amazing programming language for natural language processing. It has powerful libraries like NLTK, spaCy, and TextBlob that make text analysis easy and efficient. I really love working with Python for NLP projects because it's so versatile and user-friendly. The community support is fantastic, and there are tons of resources available online. However, sometimes the performance can be slower compared to other languages like C++ or Java.""" self.text_input.delete(1.0, tk.END) self.text_input.insert(1.0, sample) def get_text(self): return self.text_input.get(1.0, tk.END).strip() def append_result(self, text): self.result_text.config(state=tk.NORMAL) self.result_text.insert(tk.END, text + "\n") self.result_text.config(state=tk.DISABLED) self.result_text.see(tk.END) def clear_results(self): self.result_text.config(state=tk.NORMAL) self.result_text.delete(1.0, tk.END) self.result_text.config(state=tk.DISABLED) def sentiment_analysis(self): text = self.get_text() if not text: messagebox.showwarning("Uyarı", "Lütfen analiz edilecek metni girin.") return self.append_result("=" * 50) self.append_result("DUYGU ANALİZİ SONUÇLARI") self.append_result("=" * 50) # TextBlob ile duygu analizi blob = TextBlob(text) polarity = blob.sentiment.polarity subjectivity = blob.sentiment.subjectivity self.append_result(f"TextBlob Analizi:") self.append_result(f" Polarite: {polarity:.3f} (-1: Negatif, +1: Pozitif)") self.append_result(f" Öznellik: {subjectivity:.3f} (0: Nesnel, 1: Öznel)") if polarity > 0.1: sentiment = "Pozitif" elif polarity < -0.1: sentiment = "Negatif" else: sentiment = "Nötr" self.append_result(f" Genel Duygu: {sentiment}") # VADER ile duygu analizi if hasattr(self, 'sia'): scores = self.sia.polarity_scores(text) self.append_result(f"\nVADER Analizi:") self.append_result(f" Pozitif: {scores['pos']:.3f}") self.append_result(f" Nötr: {scores['neu']:.3f}") self.append_result(f" Negatif: {scores['neg']:.3f}") self.append_result(f" Bileşik: {scores['compound']:.3f}") self.append_result("") def word_frequency(self): text = self.get_text() if not text: messagebox.showwarning("Uyarı", "Lütfen analiz edilecek metni girin.") return self.append_result("=" * 50) self.append_result("KELİME FREKANSI ANALİZİ") self.append_result("=" * 50) # Metni temizleme ve kelimelere ayırma words = re.findall(r'\b[a-zA-Z]+\b', text.lower()) # Stopword'leri kaldırma (basit liste) stop_words = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'is', 'are', 'was', 'were', 'be', 'been', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could', 'should', 'it', 'its', 'this', 'that', 'these', 'those'} filtered_words = [word for word in words if word not in stop_words and len(word) > 2] # Frekans hesaplama word_freq = Counter(filtered_words) self.append_result(f"Toplam kelime sayısı: {len(words)}") self.append_result(f"Benzersiz kelime sayısı: {len(set(words))}") self.append_result(f"Stopword'ler çıkarıldıktan sonra: {len(filtered_words)}") self.append_result(f"\nEn sık kullanılan 10 kelime:") for word, freq in word_freq.most_common(10): self.append_result(f" {word}: {freq}") self.append_result("") def basic_stats(self): text = self.get_text() if not text: messagebox.showwarning("Uyarı", "Lütfen analiz edilecek metni girin.") return self.append_result("=" * 50) self.append_result("TEMEL İSTATİSTİKLER") self.append_result("=" * 50) # Temel istatistikler char_count = len(text) char_count_no_spaces = len(text.replace(' ', '')) word_count = len(text.split()) # Cümle sayısı (basit yaklaşım) sentence_count = len(re.findall(r'[.!?]+', text)) # Paragraf sayısı paragraph_count = len([p for p in text.split('\n\n') if p.strip()]) # Ortalama kelime uzunluğu words = text.split() avg_word_length = sum(len(word.strip('.,!?;:"()[]')) for word in words) / len(words) if words else 0 # Ortalama cümle uzunluğu avg_sentence_length = word_count / sentence_count if sentence_count > 0 else 0 self.append_result(f"Karakter sayısı (boşluklar dahil): {char_count}") self.append_result(f"Karakter sayısı (boşluklar hariç): {char_count_no_spaces}") self.append_result(f"Kelime sayısı: {word_count}") self.append_result(f"Cümle sayısı: {sentence_count}") self.append_result(f"Paragraf sayısı: {paragraph_count}") self.append_result(f"Ortalama kelime uzunluğu: {avg_word_length:.2f}") self.append_result(f"Ortalama cümle uzunluğu: {avg_sentence_length:.2f} kelime") # Okunabilirlik skoru (basit yaklaşım) if sentence_count > 0 and word_count > 0: flesch_score = 206.835 - (1.015 * avg_sentence_length) - (84.6 * (char_count_no_spaces / word_count)) self.append_result(f"Flesch Okunabilirlik Skoru: {flesch_score:.1f}") if flesch_score >= 90: readability = "Çok Kolay" elif flesch_score >= 80: readability = "Kolay" elif flesch_score >= 70: readability = "Oldukça Kolay" elif flesch_score >= 60: readability = "Standart" elif flesch_score >= 50: readability = "Oldukça Zor" elif flesch_score >= 30: readability = "Zor" else: readability = "Çok Zor" self.append_result(f"Okunabilirlik Seviyesi: {readability}") self.append_result("") if __name__ == "__main__": root = tk.Tk() app = NLPAnalyzerApp(root) root.mainloop()

Sonuç

Bu derste, doğal dil işlemenin temel kavramlarını ve Python'da NLP uygulamalarının nasıl geliştirileceğini öğrendiniz. NLP, masaüstü uygulamalarınıza metin analizi, duygu analizi, otomatik özetleme ve dil çevirisi gibi güçlü özellikler kazandırabilir.
Bir sonraki derste, geliştirdiğiniz yapay zeka destekli masaüstü uygulamalarını nasıl dağıtacağınızı ve son kullanıcılara nasıl sunacağınızı öğreneceğiz.