In: Computer Science
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.
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
"""