Natural Language Processing
Understand how computers can process, analyze and understand human language. From basic text processing techniques to modern transformer models.
What you will learn
- •Fundamentals of text processing and tokenization
- •Sentiment analysis and named entity recognition
- •Modern transformer models such as BERT and GPT
- •Practical implementation with Python and Hugging Face
What is Natural Language Processing?
Natural Language Processing (NLP) is a subfield of artificial intelligence that deals with the interaction between computers and human language. NLP enables machines to understand, interpret and respond to human language.
From simple text analysis tools to complex chatbots and translation systems - NLP is the foundation for many modern AI applications that we use every day.
Grundlegende NLP-Konzepte
1. Tokenization
The process of splitting text into individual words, sentences or other meaningful units.
import nltk from nltk.tokenize import word_tokenize, sent_tokenize text = "Hallo Welt! Wie geht es dir heute?" # Word tokenization words = word_tokenize(text) print(words) # ['Hallo', 'Welt', '!', 'Wie', 'geht', 'es', 'dir', 'heute', '?'] # Sentence tokenization sentences = sent_tokenize(text) print(sentences) # ['Hallo Welt!', 'Wie geht es dir heute?']
2. Part-of-Speech Tagging
Identification of parts of speech (nouns, verbs, adjectives, etc.) for grammatical analysis.
import spacy
# Load German language model
nlp = spacy.load("de_core_news_sm")
text = "The quick brown fox jumps over the lazy dog."
doc = nlp(text)
for token in doc:
print(f"{token.text}: {token.pos_} - {token.tag_}")
# The: DET - ART
# schnelle: ADJ - ADJA
# braune: ADJ - ADJA
# Fuchs: NOUN - NN
# springt: VERB - VVFIN3. Named Entity Recognition (NER)
Detection and classification of entities such as people, places and organizations in text.
import spacy
nlp = spacy.load("de_core_news_sm")
text = """Apple Inc. was founded in 1976 by Steve Jobs in Cupertino.
Today the company is headquartered in California."""
doc = nlp(text)
for ent in doc.ents:
print(f"{ent.text}: {ent.label_}")
# Apple Inc.: ORG (Organisation)
# 1976: DATE (Datum)
# Steve Jobs: PER (Person)
# Cupertino: LOC (Ort)
# Kalifornien: LOC (Ort)Implementing sentiment analysis
Sentiment analysis makes it possible to assess the emotional tone of texts - positive, negative or neutral.
from transformers import pipeline
# Load German sentiment model
sentiment_pipeline = pipeline(
"sentiment-analysis",
model="oliverguhr/german-sentiment-bert",
tokenizer="oliverguhr/german-sentiment-bert"
)
texts = [
"I love this product! It is fantastic.",
"That was a terrible experience.",
"The weather is okay today."
]
for text in texts:
result = sentiment_pipeline(text)[0]
print(f"Text: {text}")
print(f"Sentiment: {result['label']} (Konfidenz: {result['score']:.2f})")
print("-" * 50)
# Output:
# Text: I love this product! It is fantastic.
# Sentiment: POSITIVE (confidence: 0.99)
# --------------------------------------------------
# Text: That was a terrible experience.
# Sentiment: NEGATIVE (Konfidenz: 0.95)
# --------------------------------------------------Moderne Transformer-Modelle
BERT
Bidirectional Encoder Representations from Transformers - Understands context in both directions for better text comprehension.
GPT
Generative Pre-trained Transformer - Specialized in text generation and autoregressive language modeling.
use BERT for text classification
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline
# German BERT model for classification
model_name = "dbmdz/bert-base-german-cased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Create text classification pipeline
classifier = pipeline(
"text-classification",
model="deutsche-telekom/gbert-base-fine-tuned-toxic-text-classification",
tokenizer=tokenizer
)
# Classify example text
text = "This is a really interesting article about NLP!"
result = classifier(text)
print(f"Text: {text}")
print(f"Klassifikation: {result[0]['label']}")
print(f"Konfidenz: {result[0]['score']:.3f}")
# Custom tokenization
tokens = tokenizer.tokenize(text)
print(f"Tokens: {tokens}")
# ['This', 'is', 'a', 'really', 'interest', '##ing', 'article', ...]Erweiterte NLP-Techniken
Word Embeddings
Vector representation of words that captures semantic similarities.
from gensim.models import Word2Vec
from sklearn.feature_extraction.text import TfidfVectorizer
# Train Word2Vec model
sentences = [
["ich", "liebe", "machine", "learning"],
["deep", "learning", "is", "fantastic"],
["nlp", "is", "fun"],
["python", "is", "great", "for", "ai"]
]
model = Word2Vec(sentences, vector_size=100, window=5, min_count=1, workers=4)
# Find similar words
similar_words = model.wv.most_similar("learning", topn=3)
print("Similar words to'learning':", similar_words)
# Word-Vektor abrufen
vector = model.wv["learning"]
print(f"Vektor-Dimension: {len(vector)}")
# TF-IDF vectorization
documents = [
"Machine Learning is a subfield of AI",
"Deep Learning verwendet neuronale Netzwerke",
"NLP verarbeitet menschliche Sprache"
]
tfidf = TfidfVectorizer()
tfidf_matrix = tfidf.fit_transform(documents)
print(f"TF-IDF Matrix Shape: {tfidf_matrix.shape}")Topic Modeling
Automatic identification of topics in large text collections with LDA.
from gensim import corpora, models
from gensim.utils import simple_preprocess
# Beispieldokumente
documents = [
"Machine Learning algorithms learn from data",
"Deep Learning uses neural networks for complex tasks",
"NLP enables computers to understand human speech",
"Computer Vision analyzes and interprets images",
"Reinforcement Learning learns through reward and punishment"
]
# Preprocessing
texts = [simple_preprocess(doc) for doc in documents]
# Create dictionary and corpus
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
# Train LDA model
lda_model = models.LdaModel(
corpus=corpus,
id2word=dictionary,
num_topics=2,
random_state=42,
passes=10,
alpha='auto',
per_word_topics=True
)
# Display topics
for idx, topic in lda_model.print_topics(-1):
print(f'Topic {idx}: {topic}')NLP best practices
- ✓Data quality: Use clean, representative training data
- ✓Preprocessing: consistent text preprocessing (normalization, tokenization)
- ✓Evaluation: Use multiple metrics for comprehensive evaluation
- ✓Multilingual: Select language-specific models for better results
Next steps
Further topics
- →Question Answering Systeme
- →Chatbot-development
- →Neural machine translation
- →Information Extraction
Recommended tools
- •Hugging Face Transformers
- •spaCy for industrial NLP
- •NLTK for research and teaching
- •Gensim for topic modeling
