I am having trouble with a python homework question. The problem is the following:
You are to write a program that counts the frequencies of each word in a text file (text.in), and outputs each word with its count to a file (word.out). Here a word is defined as a contiguous sequence of non-blank space characters, and different capitalizations of the same character sequence should be considered same word (e.g., Text and text). The input file includes several lines with only blank spaces and alphabet characters. The output is formatted as the following: each line begins with a number indicating the frequency of the word, a white space, and then the word itself.
You need to define two functions in the program:
read_text(), which has a parameter to get the file name of the article, and returns a dictionary with the words as the keys, and their counts as the values;
save_to_file(), which should have a parameter to receive the dictionary of words, and a parameter to receive the file name to which the information will be stored.
I have been working on this for a few hours how so I'll post below what I have so far, but it's completely wrong. The first function is taking a section of the text and counting how many words are in it and the second function isn't writing into the output file at all. Can anyone help please?
def read_text():
word_dict = {}
text_file = open("line.in.txt","r")
for line in text_file:
line = line.replace('-', ' ')
for word in line.split():
word = line.strip("-")
word = word.lower()
word_dict[word] = word_dict.get(word, 0) + 1
text_file.close()
return word_dict
def save_to_file(word_dict):
text_file = open("output.txt","w")
text_file.write(word_dict)
text_file.close()
text_file = open("output.txt","r")
text_file.read()
text_file.close()