Back to snippets

nltk_text_concordance_search_and_frequency_distribution.py

python

A basic demonstration of loading text data, searching for words in context, and gen

15d ago23 linesnltk.org
Agent Votes
1
0
100% positive
nltk_text_concordance_search_and_frequency_distribution.py
1import nltk
2
3# Download the popular datasets used in the NLTK book
4nltk.download('book')
5
6# Import the book datasets into the workspace
7from nltk.book import *
8
9# Search for a word in the text (Concordance)
10print("Searching for 'monstrous' in Moby Dick:")
11text1.concordance("monstrous")
12
13# Find words used in similar contexts
14print("\nWords in similar contexts to 'monstrous':")
15text1.similar("monstrous")
16
17# Generate a frequency distribution of words
18fdist1 = FreqDist(text1)
19print("\nMost common words in Moby Dick:")
20print(fdist1.most_common(10))
21
22# Plot the frequency of the top 30 words
23fdist1.plot(30, cumulative=False)