hkml
hkml

Reputation: 339

Build a dictionary from .txt files analysis

I have a basic program that can count the number of words in a given text file. I am trying to turn this into a program that can take in several different .txt files, with an arbitrary amount of keywords within those file analyzed, and output a dictionary within a list of the results (or similar object).

The output I am looking for is a list of dictionaries wherein the list keys are the names of the .txt files in the filenames list, and the dictionary keys-values are the arbitrary words within the first function and their words counts, respectively.

I have two function that I have created and cannot seem to get any out whatsoever - which means that somethin n.

Code:

def word_count(filename, *selected_words):
    """Count the approximate number of words in a file."""
    with open(filename,"r",encoding='utf-8') as f_obj:
            contents = f_obj.read()
    filename = {} 
    filename['document'] = filename 
    filename['total_words'] = len(contents.split()) 
       
    for word in selected_words:
        count =  contents.lower().count(word)
        filename[word] = count
    return filename

def analysis_output():
    for file in files:
        word_count(file, 'the', 'yes') #WORD_COUNT FUNCTION 

files = ['alice.txt', 'siddhartha.txt', 
'moby_dick.txt', 'little_women.txt']

analysis_output()

When I run this, I am not getting any output - no errors telling me the code has run (likely improperly). Any advice on how to turn this into a a list of dictionaries is helpful!

Upvotes: 0

Views: 96

Answers (2)

Adam Wroblewski
Adam Wroblewski

Reputation: 11

My solution below solves your problem in a slightly different way. I am using lists and strings only, no dictionaries. I've entered extra comments, if needed - I hope you will find it useful.

def get_words_string(file_name):
    """get a lower-case string of all words from a file"""
    try:
        with open(file_name,"r",encoding='utf-8') as file_object:
            contents = file_object.read().lower()
            return contents
    except FileNotFoundError:
        print(f'File not found')

def count_words(words_string, *words_to_count):
    '''counts a number of each *words_to_count in a words_string'''
    for word in words_to_count:
        print(f'{word} occurs {words_string.count(word)} times')

files = [
    'text files/alice.txt',
    'text files/moby_dick.txt',
    'text files/pride_and_pre.txt',
    ]

for file in files:
        print(file)
        #try block just in case if file is missing 
        #so the program can continue
        try:
            count_words(get_words_string(file), 'yes', 'the', 'cat', 'honour')
        except:
            pass

Upvotes: 1

Peeraponw
Peeraponw

Reputation: 101

You simply forgot to define a variable to receive the output from word_count. In fact, you can do it this way:

def word_count(filename, *selected_words):
    """Count the approximate number of words in a file."""
    with open(filename,"r",encoding='utf-8') as f_obj:
            contents = f_obj.read()
    results_dict = {} 
    results_dict['document'] = filename 
    results_dict['total_words'] = len(contents.split()) 
       
    for word in selected_words:
        count =  contents.lower().count(word)
        results_dict[word] = count
    return results_dict

def analysis_output():
    output = []
    for file in files:
        output.append(word_count(file, 'the', 'yes')) #WORD_COUNT FUNCTION 
    return output

files = ['alice.txt', 'siddhartha.txt', 
'moby_dick.txt', 'little_women.txt']

final_result = analysis_output()

Upvotes: 1

Related Questions