Question

In: Computer Science

Assume a file containing a series of words is named words.txt and exists on the computer’s...

Assume a file containing a series of words is named words.txt and exists on the computer’s disk. The program should generate the set of unique words in the document and display the number of times each word in the list appears in the document.

Solutions

Expert Solution

Python code :

import re
with open("words.txt") as file:
words = re.findall(r"([a-zA-Z\-]+)", file.read())
#Get unique word from the list
seen = set()
list_of_unique_words = [x for x in words if x not in seen and not seen.add(x)]
print("List of Unique Words:")
print(list_of_unique_words)

#making the a dict of unique word with their number of appearence
words_with_number_of_appearence = {}
dupes = []
for x in words:
if x not in words_with_number_of_appearence:
words_with_number_of_appearence[x] = 1
else:
if words_with_number_of_appearence[x] == 1:
dupes.append(x)
words_with_number_of_appearence[x] += 1
print("Display all unique words with their number of appearence :")
for w in words_with_number_of_appearence.keys():
print(str(w) + "=" + str(words_with_number_of_appearence[w]))



"""
File Nmae: words.txt
File Content:
apple, banana, orange, dates, apple, dates, apple, orange, dates
coco,banana, water
"""





"""
OutPut:

Desktop>python test.py
List of Unique Words:
['apple', 'banana', 'orange', 'dates', 'coco', 'water']
Display all unique words with their number of appearence :
dates=3
apple=3
water=1
coco=1
orange=2
banana=2

"""


Related Solutions

In Python: Assume a file containing a series of integers is named numbers.txt and exists on...
In Python: Assume a file containing a series of integers is named numbers.txt and exists on the computer's Disk. Write a program that reads al the numbers stored in the file and calculates their total. - create your own text file called numbers.txt and put in a set of 20 numbers (each number should be between 1 and 100). - Each number should be on its own line. - do not assume that the file will always have 20 numbers...
Using Python. A file exists on the disk named students.txt. The file contains several records, and...
Using Python. A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student’s name, and (2) the student’s score for the final exam. Write code that deletes the record containing “John Perz”as the student name. This the code i have so far but it's not working: import os def main(): found=False search='John Perz' student_file = open('student.txt','r') temp_file = open('temp_students.txt','w') name=student_file.readline() score='' while name !='': score=student_file.readline() name=name.rstrip('/n') score=score.rstrip('/n') if name...
In this assignment you will be given an input file containing a series of universities along...
In this assignment you will be given an input file containing a series of universities along with that universities' location and rating. You must write a program that uses user defined functions to output a list of all school's above a certain rating to the terminal. Your program should do the following: - Prompt the user for the name of the input file to open - Prompt the user for the rating threshold ( Note: we print if the rating...
You should assume that there will be an object file named “foreignsub.o” which implements a function...
You should assume that there will be an object file named “foreignsub.o” which implements a function whose prototype is given in the header file “foreignsub.h”. The header file “foreignsub.h” consists of the following line. int sub(int n, int *A, int *B, int *C); However, you do not know what this function does exactly. For your testing purpose, you may produce such a function, e.g., returning the maximum value among the 3n integers in the three arrays. You are also given...
Assume you have a WAV file containing an audio with noise. The audio was sampled with...
Assume you have a WAV file containing an audio with noise. The audio was sampled with a sampling frequency of 8ksamples/second. If you play the audio file, you will be able to notice that there is a small tune along with a strong noise. Design a digital filter using Matlab or Octave to remove the noise. Write the code showing all steps of designing a digital filter? Need help please
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
In the Linux file system, the majority of the file system code exists in the kernel....
In the Linux file system, the majority of the file system code exists in the kernel. research the anatomy of the Linux file system, then discuss the effectiveness or ineffectiveness of this type of architecture. In addition, explain why you feel the way you do and provide support
Part A Operating systems generally maintain a computer’s file system. Which of the following items of...
Part A Operating systems generally maintain a computer’s file system. Which of the following items of information about a file would you expect an operating system to maintain? Note: This question gives a small amout of negative marks for wrong answers. Select one or more: a. File password b. Modification date c. Ownership d. Font size e. Access permissions f. Last access date g. File checksum h. Location (directory) i. Colour j. Expiry date k. File name l. File size...
Write R commands for below queries, assume the data is in file named input.csv. Also explain...
Write R commands for below queries, assume the data is in file named input.csv. Also explain your answer id name salary start_date dept 1 Rick 623.3 1/01/2012 IT 2 Dan 515.2 23/09/2013 Operations 3 Michelle 611 15/11/2014 IT 4 Ryan 729 11/05/2014 HR 5 Gary 843.25 27/03/2015 Finance 6 Nina 578 21/05/2013 IT 7 Simon 632.8 30/07/2013 Operations 8 Guru 722.5 17/06/2014 Finance Open the file and Get the max salary from data frame Get the detail of person having...
Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT