Search code examples
pythonstringlistdictionaryword-count

Python: Creating a function counting specific words in a textfile


I want to create a function that returns the value of word count of a specific word in a text file.

Here's what I currently have:

def Word_Counter(Text_File, Word):

    Data = open(Text_File, 'r').read().lower()

    count = Data.count(Word)


    print(Word, "; ", count)

Word_Counter('Example.txt', "the")

Which returns: "the ; 35"

That is pretty much what I want it to do. But what if I want to test a text for a range of words. I want the words (key) and values in say a list or dictionary. What's a way of doing that without using modules?

Say if I tested the function with this list of words: [time, when, left, I, do, an, who, what, sometimes].

The results I would like would be something like:

Word Counts = {'time': 1, 'when': 4, 'left': 0, 'I': 5, 'do': 2, 'an': 0, 'who': 1, 'what': 3, 'sometimes': 1}

I have been able to create a dictionary which does a word count for every word, like example below.

wordfreq = {}
for word in words.replace(',', ' ').split():
   wordfreq[word] = wordfreq.setdefault(word, 0) + 1

I'd like to do a similar style but only targeting specific words, any suggestions?


Solution

  • From your given code, I did not test this.

    def Word_Counter(Text_File, word_list):
    
            Data = open(Text_File, 'r').read().lower()
            output = {}
            for word in word_list:
               output[word] = Data.count(Word)
    

    Or you can do this

    text = open("sample.txt", "r") 
    
    # Create an empty dictionary 
    d = dict() 
    
    # Loop through each line of the file 
    for line in text: 
        # Remove the leading spaces and newline character 
        line = line.strip() 
    
        # Convert the characters in line to  
        # lowercase to avoid case mismatch 
        line = line.lower() 
    
        # Split the line into words 
        words = line.split(" ") 
    
        # Iterate over each word in line 
        for word in words: 
            # Check if the word is already in dictionary 
            if word in d: 
                # Increment count of word by 1 
                d[word] = d[word] + 1
            else: 
                # Add the word to dictionary with count 1 
                d[word] = 1