subject

The following function open_file() opens a file called 'example. txt' and returns the file pointer. This function is called within main and the file pointer is used to read lines in the file. Create a dictionary called 'dict_of_words' that have words as 'keys' and (integer) counters as values. The counter values keep a count of the number of times a word has appeared in the text file. In the end, store the dictionary key, values in a list, sort and print the list on the screen.
Note that the counts are not case-sensitive, that is, 'Word' is the same as 'word' or 'wORd'.
Also, note that your program should account for if a ',' (comma) separates two words, e. g. 'food, water, electricity'
Example:
Contents of input text file:
I do not think there is any thrill that can go through the human heart like that felt by the inventor as he sees some creation of the brain unfolding to success such emotions make a man forget food sleep friends love everything.
Nikola Tesla

Output:
[('a', 1), ('any', 1), ('as', 1), ('brain', 1), ('by', 1), ('can', 1), ('creation', 1), ('do', 1), ('emotions', 1), ('everything', 1), ('felt', 1), ('food', 1), ('forget', 1), ('friends', 1), ('go', 1), ('he', 1), ('heart', 1), ('human', 1), ('i', 1), ('inventor', 1), ('is', 1), ('like', 1), ('love', 1), ('make', 1), ('man', 1), ('nikola', 1), ('not', 1), ('of', 1), ('sees', 1), ('sleep', 1), ('some', 1), ('success', 1), ('such', 1), ('tesla', 1), ('that', 2), ('the', 3), ('there', 1), ('think', 1), ('thrill', 1), ('through', 1), ('to', 1), ('unfolding', 1)]

Use the code below:

def open_file():
fpointer = open('example. txt')
return fpointer

def main():
dictlist = []
fp = open_file()
#loop to iterate over lines in file

for key, value in dict_of_words. items():
temp = (key, value)
dictlist. append(temp)
print(sorted(dictlist))

main()

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 19:30
When creating a presentation in libre office impress, where does the editing of slides take place? a. the slides panel b. the center panel c. the tasks panel, under the masters pages tab d. the tasks panel, under the layouts tab
Answers: 1
question
Computers and Technology, 23.06.2019 11:30
The most accurate readings that you can take on an analog vom are when the meter's pointer is at the a. center scale. b. extreme right. c. near right. d. extreme left.
Answers: 1
question
Computers and Technology, 23.06.2019 16:30
If i wanted to include a built-in calendar in a document, what option could i select? draw table insert table insert chart quick tables
Answers: 1
question
Computers and Technology, 24.06.2019 07:30
Consider the folloeing website url: what does the "http: //" represent? a. protocal identifier. b. ftp. c. domain name d. resource name
Answers: 2
You know the right answer?
The following function open_file() opens a file called 'example. txt' and returns the file pointer....
Questions
question
Biology, 23.09.2020 19:01
question
Mathematics, 23.09.2020 19:01
question
Mathematics, 23.09.2020 19:01
Questions on the website: 13722362