Answer:
import string
dic = {}
book=open("book.txt","r")
# Iterate over each line in the book
for line in book.readlines():
tex = line
tex = tex.lower()
tex=tex.translate(str.maketrans('', '', string.punctuation))
new = tex.split()
for word in new:
if len(word) > 2:
if word not in dic.keys():
dic[word] = 1
else:
dic[word] = dic[word] + 1
for word in sorted(dic):
print(word, dic[word], '\\')
book.close()
Step-by-step explanation:
The code above was written in python 3.
import string
Firstly, it is important to import all the modules that you will need. The string module was imported to allow us carry out special operations on strings.
dic = {}
book=open("book.txt","r")
# Iterate over each line in the book
for line in book.readlines():
tex = line
tex = tex.lower()
tex=tex.translate(str.maketrans('', '', string.punctuation))
new = tex.split()
An empty dictionary is then created, a dictionary is needed to store both the word and the occurrences, with the word being the key and the occurrences being the value in a word : occurrence format.
Next, the file you want to read from is opened and then the code iterates over each line, punctuation and special characters are removed from the line and it is converted into a list of words that can be iterated over.
for word in new:
if len(word) > 2:
if word not in dic.keys():
dic[word] = 1
else:
dic[word] = dic[word] + 1
For every word in the new list, if the length of the word is greater than 2 and the word is not already in the dictionary, add the word to the dictionary and give it a value 1.
If the word is already in the dictionary increase the value by 1.
for word in sorted(dic):
print(word, dic[word], '\\')
book.close()
The dictionary is arranged alphabetically and with the keys(words) and printed out. Finally, the file is closed.
check attachment to see code in action.