import re
def remove_extra_whitespaces(infile, outfile):
file1 = open(infile)
text = re.sub("\n\n\n+", "\n\n", file1.read())
file1.close()
file2 = open(outfile, "w")
formatted = ""
for line in text.split("\n"):
formatted += " ".join(line.split()) + "\n"
file2.write(formatted.strip())
file2.close()
#Function to adjust the line length
def adjust_linelength(infile, outfile):
file1 = open(infile, encoding='utf-8')
text = file1.readlines()
file1.close()
file2 = open(outfile, 'w')
line_len = 0
formatted = ""
for line in text:
if line == "\n":
formatted += "\n\n"
line_len = 0
continue
for word in line.split():
next_word_len = len(word) + 1
if line_len + next_word_len <= 60:
formatted += word + " "
line_len += next_word_len
else:
formatted = formatted.strip() + "\n" + word + " "
line_len = 0
file2.write(formatted.strip())
file2.close()
# Function to write stats for essay
def essay_statistics(infile, outfile):
file = open(infile)
file2 = open(outfile, 'w')
non_blank_lines = 0
num_words = 0
sum_word_length = 0
avg_word_length = 0
for line in file:
if line != '\n':
non_blank_lines = non_blank_lines + 1
num_words = num_words + len(line.split())
for word in line.split():
sum_word_length = sum_word_length + len(word)
avg_word_length = sum_word_length // (1.0 * num_words)
file2.write('In the file essay.txt' + '\n\n')
file2.write('Number of (non-blank) lines : ' + str(non_blank_lines) + '\n')
file2.write('Number of words : ' + str(num_words) + '\n')
file2.write('Average word length : ' + str(avg_word_length) + '\n')
# Function to call all functions in order
def format_essay():
file_name = input('Enter name (*.txt) of the file containing the essay: ')
remove_extra_whitespaces(file_name, file_name[0:-4] + '_neb.txt')
print()
print(file_name[0:-4] + '_neb.txt' + ' Created')
print()
adjust_linelength(file_name[0:-4] + '_neb.txt', file_name[0:-4] + '_final.txt')
print()
print('The formatted essay is in the file ' + file_name[0:-4] + '_final.txt')
print()
essay_statistics(file_name[0:-4] + '_final.txt', file_name[0:-4] + '_stats.txt')
print()
print('The essay statistics are in the file ' + file_name[0:-4] + '_stats.txt')
if __name__ == '__main__': # give call to all functions
format_essay()
Albuquerque is my turkey and he's feathered and he's fine, And he
wobbles and he gobbles and he's
absolutely mine
He's the best pet you can get yet better than
a dog or cat, He's my albuquerque turkey and i'm awfully proud of that
Albuquerque is my turkey and he's happy in his bed, 'Cause for
our thanksgiving dinner we'll have sphagetti instead
Albuquerque is my turkey and he's feathered and he's fine,
And he wobbles and he gobbles and he's absolutely mine
He's the best pet you can get yet better than a dog or cat,
He's my albuquerque turkey and i'm awfully proud of that
Albuquerque is my turkey and he's happy in his bed, 'Cause
for our thanksgiving dinner we'll have sphagetti instead
Albuquerque is my turkey and he's feathered and he's fine, And he
wobbles and he gobbles and he's
absolutely mine
He's the best pet you can get yet better than
a dog or cat, He's my albuquerque turkey and i'm awfully proud of that
Albuquerque is my turkey and he's happy in his bed, 'Cause for
our thanksgiving dinner we'll have sphagetti instead
In the file essay.txt
Number of (non-blank) lines : 6
Number of words : 63
Average word length : 4.0