Question

In: Computer Science

I have a Python code that reads the text file, creates word list then calculates word...

I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below:

#Open file

f = open('example.txt', 'r')

#list created with all words

data=f.read().lower()

list1=data.split()

#empty dictionary

d={}

# Adding all elements of the list to a dictionary and assigning it's value as zero

for i in set(list1):

    d[i]=0

# checking and counting the values

for i in list1:

    for j in d.keys():

       if i==j:

          d[i]=d[i]+1

#Return all non-overlapping matches of pattern return pattern

print(d)

Question: How do I have my code only calculate specific list of words not the every single word in the file. for example: I only wanna know how many times (Apple, Banana, Orange, Watermelon, Blueberry) occurred throughout the text. Also apple/Apple/Apple! should count as same word. I appreciate your help. Please don't comment if you don't want to work on this question.

Here is the example text file: named example.txt

I Love apple, I don't like banana, but blueberry for me too. Apple, banana, orange, watermelon are my fav.
Banana can keep you full. Watermelon is good for summer. Banana!

Solutions

Expert Solution

Python code with comments pasted below.

#Open file in read mode
fr = open('example.txt', 'r')
#list of favourite fruits
fav_fruits=["apple", "banana", "orange", "watermelon", "blueberry"]
#list created with all fruits/words in lowercase
data=fr.read().lower()
words=data.split()
#Initialize an empty dictionary for storing the the count of favourite fruits only
fruits={}
#Traversing through the list named words
for word in words:
#Checking whether word is one of our favourite fruits
#word[0:len(word)-1] is to check whether the word is followed by !, etc.
if word in fav_fruits or word[0:len(word)-1] in fav_fruits:
#if word is followed by ! or , then we need to remove it
#Only then banana! and banana will be treated as the same.
if word[0:len(word)-1] in fav_fruits:
word=word[0:len(word)-1]
#If word is not in the dictionary fruits, then
#Add word as the key of the dictionary and set its count to 1
if word not in fruits:
fruits[word]=1
#If word is already in the dictionary fruits, then
#Increment the value of the dictionary to 1 with the key word
else:
fruits[word]+=1
#printing the favourite fruits and count
for k,v in fruits.items():
print(k,"=",v)
Python code in IDLE pasted for better understanding of the indent.

Output Screen

Input File - Example.txt


Related Solutions

This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Python 3 Fix the code so the program reads the file and see if the bar...
Python 3 Fix the code so the program reads the file and see if the bar code was already inputted 3 times if so, it ishows a warning indicating that the item was already tested 3 times Code: import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook from tkinter import messagebox from datetime import datetime window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window,...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
a python function that reads two text files and merges in to one Linked List, be...
a python function that reads two text files and merges in to one Linked List, be able to print each Item in the new single Linked List class Node(object): item = -1 next = None def __init__(self, item, next): self.item = item self.next = next ================================ textfile! 979 2744 5409 1364 4948 4994 5089 703 1994 4637 2228 4004 1088 2812 170 5179 2614 238 4523 4849 3592 3258 1951 3440 3977 1247 4076 1824 4759 4855 5430 347 974...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt)...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT