Word count python program

Python – Count Number of Words in Text File

You can count number of words in a text file in Python by following a sequence of steps which we shall discuss in this tutorial.

In this tutorial, we shall learn how to count number of words in text file, using Python example programs.

Steps to Count Number of Words in Text File

To count the number of words in a text file, follow these steps.

  1. Open the file in read mode and handle it in text mode.
  2. Read the text using read() function.
  3. Split the text using space separator. We assume that words in a sentence are separated by a space character.
  4. The length of the split list should equal the number of words in the text file.
  5. You can refine the count by cleaning the string prior to splitting or validating the words after splitting.
Читайте также:  Присоединение сценария CSS к странице HTML различными вариантами

Examples

1. Count number of words in given text file

In this Python Example, we will read a text file and count the number of words in it. Consider the following text file.

Welcome to pythonexamples.org. Here, you will find python programs for all general use cases.

Python Program

file = open("C:\data.txt", "rt") data = file.read() words = data.split() print('Number of words in text file :', len(words))
Number of words in text file : 14

2. Count number of words in Text File, where the file has multiple lines

In this Python Example, we will read a text file with multiple lines and count the number of words in it. Consider the following text file.

New line character separates lines in a text file. New line is a white space character and when we split the whole data in text file using split() method, all the words in all the sentences are split and returned as a single list.

Text File – data.txt

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases. This is another line with some words.

Python Program

file = open("C:\data.txt", "rt") data = file.read() words = data.split() print('Number of words in text file :', len(words))
Number of words in text file : 21

Summary

In this tutorial of Python Examples, we learned how to count number of words in a Text File, with the help of example programs.

Источник

Python: Count Words in a String or File

How to Use Python to Count Words and Generate Word Frequencies Cover Image

In this tutorial, you’ll learn how to use Python to count the number of words and word frequencies in both a string and a text file. Being able to count words and word frequencies is a useful skill. For example, knowing how to do this can be important in text classification machine learning algorithms.

By the end of this tutorial, you’ll have learned:

  • How to count the number of words in a string
  • How to count the number of words in a text file
  • How to calculate word frequencies using Python

Reading a Text File in Python

The processes to count words and calculate word frequencies shown below are the same for whether you’re considering a string or an entire text file. Because of this, this section will briefly describe how to read a text file in Python.

If you want a more in-depth guide on how to read a text file in Python, check out this tutorial here. Here is a quick piece of code that you can use to load the contents of a text file into a Python string:

# Reading a Text File in Python file_path = '/Users/datagy/Desktop/sample_text.txt' with open(file_path) as file: text = file.read()

I encourage you to check out the tutorial to learn why and how this approach works. However, if you’re in a hurry, just know that the process opens the file, reads its contents, and then closes the file again.

Count Number of Words In Python Using split()

One of the simplest ways to count the number of words in a Python string is by using the split() function. The split function looks like this:

# Understanding the split() function str.split( sep=None # The delimiter to split on maxsplit=-1 # The number of times to split )

By default, Python will consider runs of consecutive whitespace to be a single separator. This means that if our string had multiple spaces, they’d only be considered a single delimiter. Let’s see what this method returns:

# Splitting a string with .split() text = 'Welcome to datagy! Here you will learn Python and data science.' print(text.split()) # Returns: ['Welcome', 'to', 'datagy!', 'Here', 'you', 'will', 'learn', 'Python', 'and', 'data', 'science.']

We can see that the method now returns a list of items. Because we can use the len() function to count the number of items in a list, we’re able to generate a word count. Let’s see what this looks like:

# Counting words with .split() text = 'Welcome to datagy! Here you will learn Python and data science.' print(len(text.split())) # Returns: 11

Count Number of Words In Python Using Regex

Another simple way to count the number of words in a Python string is to use the regular expressions library, re . The library comes with a function, findall() , which lets you search for different patterns of strings.

Because we can use regular expression to search for patterns, we must first define our pattern. In this case, we want patterns of alphanumeric characters that are separated by whitespace.

For this, we can use the pattern \w+ , where \w represents any alphanumeric character and the + denotes one or more occurrences. Once the pattern encounters whitespace, such as a space, it will stop the pattern there.

Let’s see how we can use this method to generate a word count using the regular expressions library, re :

# Counting words with regular expressions import re text = 'Welcome to datagy! Here you will learn Python and data science.' print(len(re.findall(r'\w+', text))) # Returns: 11

Calculating Word Frequencies in Python

In order to calculate word frequencies, we can use either the defaultdict class or the Counter class. Word frequencies represent how often a given word appears in a piece of text.

Using defaultdict To Calculate Word Frequencies in Python

Let’s see how we can use defaultdict to calculate word frequencies in Python. The defaultdict extend on the regular Python dictionary by providing helpful functions to initialize missing keys.

Because of this, we can loop over a piece of text and count the occurrences of each word. Let’s see how we can use it to create word frequencies for a given string:

# Creating word frequencies with defaultdict from collections import defaultdict import re text = 'welcome to datagy! datagy will teach data. data is fun. data data data!' counts = defaultdict(int) for word in re.findall('\w+', text): counts[word] += 1 print(counts) # Returns: # defaultdict(, )

Let’s break down what we did here:

  1. We imported both the defaultdict function and the re library
  2. We loaded some text and instantiated a defaultdict using the int factory function
  3. We then looped over each word in the word list and added one for each time it occurred

Using Counter to Create Word Frequencies in Python

Another way to do this is to use the Counter class. The benefit of this approach is that we can even easily identify the most frequent word. Let’s see how we can use this approach:

# Creating word frequencies with Counter from collections import Counter import re text = 'welcome to datagy! datagy will teach data. data is fun. data data data!' counts = Counter(re.findall('\w+', text)) print(counts) # Returns: # Counter()

Let’s break down what we did here:

  1. We imported our required libraries and classes
  2. We passed the resulting list from the findall() function into the Counter class
  3. We printed the result of this class

One of the perks of this is that we can easily find the most common word by using the .most_common() function. The function returns a sorted list of tuples, ordering the items from most common to least common. Because of this, we can simply access the 0th index to find the most common word:

# Finding the Most Common Word from collections import Counter import re text = 'welcome to datagy! datagy will teach data. data is fun. data data data!' counts = Counter(re.findall('\w+', text)) print(counts.most_common()[0]) # Returns: # ('data', 5)

Conclusion

In this tutorial, you learned how to generate word counts and word frequencies using Python. You learned a number of different ways to count words including using the .split() method and the re library. Then, you learned different ways to generate word frequencies using defaultdict and Counter . Using the Counter method, you were able to find the most frequent word in a string.

Additional Resources

To learn more about related topics, check out the tutorials below:

Источник

Python – Count Number of Words in Text File

You can count number of words in a text file in Python by following a sequence of steps which we shall discuss in this tutorial.

In this tutorial, we shall learn how to count number of words in text file, using Python example programs.

Steps to Count Number of Words in Text File

To count the number of words in a text file, follow these steps.

  1. Open the file in read mode and handle it in text mode.
  2. Read the text using read() function.
  3. Split the text using space separator. We assume that words in a sentence are separated by a space character.
  4. The length of the split list should equal the number of words in the text file.
  5. You can refine the count by cleaning the string prior to splitting or validating the words after splitting.

Examples

1. Count number of words in given text file

In this Python Example, we will read a text file and count the number of words in it. Consider the following text file.

Welcome to pythonexamples.org. Here, you will find python programs for all general use cases.

Python Program

file = open("C:\data.txt", "rt") data = file.read() words = data.split() print('Number of words in text file :', len(words))
Number of words in text file : 14

2. Count number of words in Text File, where the file has multiple lines

In this Python Example, we will read a text file with multiple lines and count the number of words in it. Consider the following text file.

New line character separates lines in a text file. New line is a white space character and when we split the whole data in text file using split() method, all the words in all the sentences are split and returned as a single list.

Text File – data.txt

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases. This is another line with some words.

Python Program

file = open("C:\data.txt", "rt") data = file.read() words = data.split() print('Number of words in text file :', len(words))
Number of words in text file : 21

Summary

In this tutorial of Python Examples, we learned how to count number of words in a Text File, with the help of example programs.

Источник

Оцените статью