Categories We Write About

Create a word frequency analyzer

Here is a simple Word Frequency Analyzer written in Python. It reads a block of text, cleans it, splits it into words, and then counts the frequency of each word.

python
import re from collections import Counter def word_frequency_analyzer(text): # Clean text: remove punctuation and convert to lowercase cleaned_text = re.sub(r'[^ws]', '', text.lower()) # Split text into words words = cleaned_text.split() # Count word frequencies word_counts = Counter(words) # Display results for word, count in word_counts.most_common(): print(f"{word}: {count}") # Example usage if __name__ == "__main__": sample_text = """ This is a simple word frequency analyzer. This analyzer will count how many times each word appears in a given text. Word frequency analysis is a common task in natural language processing. """ word_frequency_analyzer(sample_text)

How It Works:

  • re.sub(r'[^ws]', '', text.lower()): Removes punctuation and converts text to lowercase.

  • split(): Splits the cleaned text into words.

  • Counter(words): Tallies how often each word appears.

  • most_common(): Lists words in descending order of frequency.

You can adapt this for reading text from files or analyzing large documents. Let me know if you want a version with a GUI or web interface.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About