I have the following code:
import re
def get_filename():
"""gets the file"""
filename = input("Please enter filename: ")
return filename
def get_words_from_file(filename):
"""getting the data and printing it word by word"""
infile = open(filename, 'r', encoding='utf-8')
outfile = infile.read().splitlines()
words = []
reading = False
for let in outfile:
if let.startswith("*** START OF")and reading == False:
reading = True
elif let.startswith("*** END OF SYNTHETIC TEST CASE ***") or let.startswith("*** END"):
return words
elif reading:
let = let.lower()
words.extend(re.findall("[a-z] [-'][a-z] |[a-z] [']?|[a-z] ", let))
return words
def calculate(words):
"""gjhwjghwg2"""
all_times = []
max_word_length = 0
number_of_words = len(words)
average = sum(len(word) for word in words) / number_of_words
for word in words:
if len(word)>max_word_length:
max_word_length=len(word)
frequency = {word: 0 for word in words}
for word in words:
frequency[word] = 1
max_frequency = max(frequency.values())
result = (number_of_words, average, max_word_length, max_frequency)
return result
def get_frequency(words):
"""ghjhgwejhgwjgw"""
def print_results(stats_tuple):
"""calculate the goods"""
(number_of_words, average, max_word_length, max_frequency) = stats_tuple
print("")
print("Word summary (all words):")
print(" Number of words = {0}".format(number_of_words))
print(" Average word length = {:.2f}".format(average))
print(" Maximum word length = {0}".format(max_word_length))
print(" Maximum frequency = {0}".format(max_frequency))
print("")
print(" Len Freq")
def main():
"""ghkghwgjkwhgw"""
filename = get_filename()
data = get_words_from_file(filename)
stats = calculate(data)
print_results(stats)
main()
Without importing anything else, how would I make a table which prints the length and then the frequency.
For example: In a file with the text of "a blah ba ba" it would print:
Len Freq
1 1
2 2
3 0
4 1
What confuses me about this is how to add all the length of the words together, should I be making a new list with all the same length of words and then counting the length of that list, or is there a better way to do it.
CodePudding user response:
You can use a Counter
from the collections
modules. Then loop over the lengths from 1 to the maximum count. If the length is not present in the counts dictionary then write 0
, otherwise write the corresponding value.
from collections import Counter
filename = 'test.txt'
with open(filename) as fin:
words = fin.read().split()
counts = Counter(len(word) for word in words)
print('Len Freq')
for length in range(1, max(counts.keys()) 1):
print(f'{length:4d} {counts.get(length, 0):5d}')
If the input file consists of:
a blah ba ba
then this gives:
Len Freq
1 1
2 2
3 0
4 1
Note that the max(counts.keys())
is written that way for clarity, but max(counts)
could be used instead (because if you iterate over a dictionary, as max
will do here - and a Counter
object is essentially a dictionary - then you iterate over its keys).
This example assumes that you have a relatively small file that can be stored in memory. If you have a larger file then you could do something like the following, to avoid doing so:
from collections import Counter
filename = 'test.txt'
def iter_lengths(filename):
with open(filename) as fin:
for line in fin:
for word in line.split():
yield len(word)
counts = Counter(iter_lengths(filename))
print('Len Freq')
for length in range(1, max(counts) 1):
print(f'{length:4d} {counts.get(length, 0):5d}')
CodePudding user response:
len_count = {}
with open(filename, "r") as file:
for line in file:
for word in line.split():
word_len = len(word)
if not word_len in len_count:
len_count[word_len] = 1
else:
len_count[word_len] = 1
Then you can print the two columns:
print("Len\tFreq")
for word_len in range(1, max(len_count) 1):
print(f'{word_len}\t{len_count.get(word_len, 0)}')