- Python Text Processing - Home
- Python Text Processing - Introduction
- Python Text Processing - Environment
- Python Text Processing - String Immutability
- Python Text Processing - Sorting Lines
- Python Text Processing - Counting Token in Paragraphs
- Python Text Processing - Binary ASCII Conversion
- Python Text Processing - Strings as Files
- Python Text Processing - Backward File Reading
- Python Text Processing - Filter Duplicate Words
- Python Text Processing - Extract Emails from Text
- Python Text Processing - Extract URL from Text
- Python Text Processing - Pretty Print
- Python Text Processing - State Machine
- Python Text Processing - Capitalize and Translate
- Python Text Processing - Tokenization
- Python Text Processing - Remove Stopwords
- Python Text Processing - Synonyms and Antonyms
- Python Text Processing - Translation
- Python Text Processing - Word Replacement
- Python Text Processing - Spelling Check
- Python Text Processing - WordNet Interface
- Python Text Processing - Corpora Access
- Python Text Processing - Tagging Words
- Python Text Processing - Chunks and Chinks
- Python Text Processing - Chunk Classification
- Python Text Processing - Classification
- Python Text Processing - Bigrams
- Python Text Processing - Process PDF
- Python Text Processing - Process Word Document
- Python Text Processing - Reading RSS feed
- Python Text Processing - Sentiment Analysis
- Python Text Processing - Search and Match
- Python Text Processing - Text Munging
- Python Text Processing - Text wrapping
- Python Text Processing - Frequency Distribution
- Python Text Processing - Summarization
- Python Text Processing - Stemming Algorithms
- Python Text Processing - Constrained Search
Python Text Processing Useful Resources
Python Text Processing - Counting Tokens in Paragraphs
While reading the text from a source, sometimes we also need to find out some statistics about the type of words used. That makes it necessary to count the number of words as well as lines with a specific type of words in a given text. In the below example we show programs to count the words in a paragraph using two different approaches. We consider a text file for this purpose which contains the summary of a Hollywood movie.
Reading the File
main.py
fileName = "GodFather.txt"
with open(fileName, 'r') as file:
lines_in_file = file.read()
print(lines_in_file)
Output
When we run the above program we get the following output −
Vito Corleone is the aging don (head) of the Corleone Mafia Family. His youngest son Michael has returned from WWII just in time to see the wedding of Connie Corleone (Michael's sister) to Carlo Rizzi. ...
Counting Words Using nltk
Next we use the nltk module to count the words in the text. Please note the word '(head)' is counted as 3 words and not one.
main.py
import nltk
fileName = "GodFather.txt"
with open(fileName, 'r') as file:
lines_in_file = file.read()
nltk_tokens = nltk.word_tokenize(lines_in_file)
print(nltk_tokens)
print("\n")
print("Number of Words: " , len(nltk_tokens))
Output
When we run the above program we get the following output −
['Vito', 'Corleone', 'is', 'the', 'aging', 'don', ... ] Number of Words: 167
Counting Words Using Split
Next we count the words using Split function and here the word '(head)' is counted as a single word and not 3 words as in case of using nltk.
fileName = "GodFather.txt"
with open(fileName, 'r') as file:
lines_in_file = file.read()
print(lines_in_file.split())
print("\n")
print("Number of Words: ", len(lines_in_file.split()))
Output
When we run the above program we get the following output −
['Vito', 'Corleone', 'is', 'the', 'aging', 'don', ... ] Number of Words: 146