The Natural Language Toolkit (NLTK) is a powerful Python library for working with human language data (text). It provides easy-to-use interfaces to over 50 corpora and lexical resources such as WordNet, along with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning.
Designed initially for teaching and research, NLTK is also used in commercial and industrial NLP applications. It serves as a foundational toolkit for natural language processing and computational linguistics.
pip install nltk
import nltk
nltk.download('all')
Downloading 'all' ensures you have access to all corpora, tokenizers, taggers, and other tools required by the examples and real-world projects.
Tokenization is the process of breaking a text into words or sentences.
from nltk.tokenize import word_tokenize, sent_tokenize
text = "NLTK is a leading platform for building Python programs to work with human language data."
word_tokens = word_tokenize(text)
sent_tokens = sent_tokenize(text)
print("Word Tokens:", word_tokens)
print("Sentence Tokens:", sent_tokens)
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
stop_words = set(stopwords.words('english'))
text = "This is an example showing off stop word filtration."
words = word_tokenize(text)
filtered = [w for w in words if w.lower() not in stop_words]
print("Filtered Words:", filtered)
Stemming reduces words to their root form.
from nltk.stem import PorterStemmer
ps = PorterStemmer()
words = ["python", "pythoner", "pythoning", "pythonly"]
for w in words:
print(w, "->", ps.stem(w))
Lemmatization considers the context and converts a word to its base form using vocabulary and morphological analysis.
from nltk.stem import WordNetLemmatizer
import nltk
nltk.download('wordnet')
lemmatizer = WordNetLemmatizer()
words = ["running", "flies", "better"]
for w in words:
print(w, "->", lemmatizer.lemmatize(w, pos='v'))
import nltk
nltk.download('averaged_perceptron_tagger')
sentence = "The quick brown fox jumps over the lazy dog"
tokens = nltk.word_tokenize(sentence)
tags = nltk.pos_tag(tokens)
print(tags)
import nltk
nltk.download('maxent_ne_chunker')
nltk.download('words')
sentence = "Barack Obama was born in Hawaii."
tokens = nltk.word_tokenize(sentence)
tags = nltk.pos_tag(tokens)
tree = nltk.ne_chunk(tags)
print(tree)
tree.draw()
import nltk
sentence = [("the", "DT"), ("little", "JJ"), ("yellow", "JJ"), ("dog", "NN")]
grammar = "NP: {?*}"
cp = nltk.RegexpParser(grammar)
result = cp.parse(sentence)
print(result)
result.draw()
from nltk.corpus import gutenberg
nltk.download('gutenberg')
print(gutenberg.fileids())
text = gutenberg.raw('austen-emma.txt')
print(text[:1000])
from nltk.corpus import wordnet
syns = wordnet.synsets("computer")
print("Name:", syns[0].name())
print("Definition:", syns[0].definition())
print("Examples:", syns[0].examples())
synonyms = []
antonyms = []
for syn in wordnet.synsets("good"):
for lemma in syn.lemmas():
synonyms.append(lemma.name())
if lemma.antonyms():
antonyms.append(lemma.antonyms()[0].name())
print("Synonyms:", set(synonyms))
print("Antonyms:", set(antonyms))
from nltk import ngrams
text = "I love natural language processing".split()
bigrams = list(ngrams(text, 2))
print("Bigrams:", bigrams)
from nltk import FreqDist
from nltk.corpus import brown
nltk.download('brown')
words = brown.words(categories='news')
fdist = FreqDist(words)
print(fdist.most_common(10))
fdist.plot(10)
from nltk.classify import NaiveBayesClassifier
def word_features(word):
return {'last_letter': word[-1]}
labeled_words = [('happiness', 'positive'), ('anger', 'negative'),
('joy', 'positive'), ('fear', 'negative')]
featuresets = [(word_features(n), label) for (n, label) in labeled_words]
train_set = featuresets
classifier = NaiveBayesClassifier.train(train_set)
print(classifier.classify(word_features('excitement')))
print(classifier.show_most_informative_features(5))
from nltk.corpus import movie_reviews
import random
nltk.download('movie_reviews')
documents = [(list(movie_reviews.words(fileid)), category)
for category in movie_reviews.categories()
for fileid in movie_reviews.fileids(category)]
random.shuffle(documents)
all_words = nltk.FreqDist(w.lower() for w in movie_reviews.words())
word_features = list(all_words)[:2000]
def document_features(document):
words = set(document)
features = {}
for word in word_features:
features[word] = (word in words)
return features
featuresets = [(document_features(d), c) for (d, c) in documents]
train_set = featuresets[:1900]
test_set = featuresets[1900:]
classifier = nltk.NaiveBayesClassifier.train(train_set)
print("Accuracy:", nltk.classify.accuracy(classifier, test_set))
classifier.show_most_informative_features(10)
import nltk
grammar = nltk.CFG.fromstring("""
S -> NP VP
NP -> Det N
VP -> V NP
Det -> 'the'
N -> 'dog' | 'cat'
V -> 'chased' | 'sat'
""")
parser = nltk.ChartParser(grammar)
sentence = ['the', 'dog', 'chased', 'the', 'cat']
for tree in parser.parse(sentence):
print(tree)
tree.draw()
from nltk.corpus import udhr
nltk.download('udhr')
languages = ['English-Latin1', 'French_Francais-Latin1', 'German_Deutsch-Latin1']
for lang in languages:
print(lang, "Sample:\n", udhr.raw(lang)[:100])
from gensim import corpora, models
import gensim
from nltk.tokenize import word_tokenize
documents = ["Human machine interface for computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement"]
texts = [[word for word in word_tokenize(doc.lower()) if word.isalpha()]
for doc in documents]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
lda_model = gensim.models.ldamodel.LdaModel(corpus, num_topics=2, id2word=dictionary, passes=15)
topics = lda_model.print_topics(num_words=4)
for topic in topics:
print(topic)
NLTK is a foundational library in Python's NLP ecosystem. It offers a broad collection of tools, corpora, and resources for language processing, making it ideal for education, prototyping, and research. Though libraries like SpaCy have emerged for industrial-scale processing, NLTK still holds significant value due to its vast dataset access, flexibility, and educational utility.
From tokenization and stemming to full-blown classification and parsing, NLTK allows you to build end-to-end NLP pipelines. With continued exploration and integration, it remains a key tool for anyone working with natural language data in Python.
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is βΉ5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from βΉ3,000 - βΉ1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved