Skip to content

Update exercise_1_poem.py #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions Basics/Exercise/13_read_write_files/exercise_1_poem.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
word_stats={}
# Road Not Taken.txt contains famous poem "Road not taken" by poet Robert Frost.
# You have to read this file in your python program and find out words with maximum occurrence.

with open("poem.txt","r") as f:
keywords ={}
with open("Road Not Taken Poem.txt", "r") as f:
for line in f:
words=line.split(' ')
for word in words:
if word in word_stats:
word_stats[word]+=1
else:
word_stats[word] = 1
# Filtration of line removing special characters
new_line = ''
for character in line:
if character.isalnum() or character == ' ' or character == '\'':
new_line += character

print(word_stats)
# splitting new line into words
items = new_line.split(" ")
for item in items:
# confirming that space should not be counted
if item != '':
# converting each word into lower case so that case can be matched later
key = item.lower()
if key in keywords:
keywords[key]+=1
else:
keywords[key] = 1

word_occurances = list(word_stats.values())
max_count = max(word_occurances)
print("Max occurances of any word is:",max_count)

print("Words with max occurances are: ")
for word, count in word_stats.items():
if count==max_count:
print(word)
word = ''
frequency = 0
for keyword, value in keywords.items():
if value > frequency:
frequency = value
word = keyword

print(f"The most frequent used word in the poem is '{word}'. It was repeated {frequency} times")