Build Python Project : A Text Analyzer Tool

Create a text analyzer tool that performs various operations on a given text input.

Ahmad Mizan Nur Haq
Data And Beyond
3 min readDec 1, 2023

--

Have you ever stopped to analyze the words themselves? How often are they used? Do they follow patterns? with string manipulation we can create this simple text analyzer demonstrates how to manipulate strings using various techniques, such as splitting, counting, and comparing.

What we gonna build in here is

A Text analyzer that can do ;

  1. Count the number of words in the text.
  2. Count the number of occurrences of each unique word.
  3. Identify the most frequently used words.
  4. Determine the average word length.

Lets Build the project

  1. Import Libraries
import string

2. Define Functions

def count_words(text):
words = text.split()
return len(words)

def count_unique_words(text):
words = text.split()
unique_words = set(words)
return len(unique_words)

def most_frequent_words(text):
words = text.split()
word_counts = {}
for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1

most_frequent_words = []
for word, count in word_counts.items():
if count == max(word_counts.values()):
most_frequent_words.append(word)

return most_frequent_words

def average_word_length(text):
words = text.split()
total_length = 0
for word in words:
total_length += len(word)

average_length = total_length / len(words)
return average_length

- count_words(text):

  • Splits the text into individual words using text.split().
  • Counts the number of words using len(words).
  • Returns the number of words.

- count_unique_words(text):

  • Splits the text into individual words.
  • Creates a set of unique words using set(words).
  • Counts the number of unique words using len(unique_words).
  • Returns the number of unique words.

- most_frequent_words(text):

  • Splits the text into individual words.
  • Initializes an empty dictionary word_counts.
  • Iterates through each word and increments its count in the dictionary.
  • Finds the word with the highest count using max(word_counts.values()).
  • Creates a list of words with the maximum count and returns it.

- average_word_length(text):

  • Splits the text into individual words.
  • Initializes two variables: total_length and word_count.
  • Iterates through each word, adding its length to total_length and incrementing word_count.
  • Calculates the average word length by dividing total_length by word_count.
  • Returns the average word length.

3. The Interface

text_input = input("Enter the text to analyze: ")

word_count = count_words(text_input)
unique_word_count = count_unique_words(text_input)
most_frequent_words = most_frequent_words(text_input)
average_word_length = average_word_length(text_input)

print("\nWord Count:", word_count)
print("Unique Word Count:", unique_word_count)
print("Most Frequent Words:", most_frequent_words)
print("Average Word Length:", average_word_length)

Result

This simple text analyzer demonstrates how to manipulate strings using various techniques, such as splitting, counting, and comparing. It provides a basic framework for analyzing text data and extracting meaningful information.

Hey 👋 Enjoying the content? If you find it valuable, why not subscribe and follow Me on Medium for more content?

🔔 Subscribe & Follow

☕️Buymeacoffee |📚Substack | GitHub | LinkedIn

By subscribing, you’ll get the latest updates, tips, and content delivered right to your inbox.

Thank you for your support! 🙌

In this post, I kindly want to see the effectiveness of Call to Action (CTA).

--

--