PYTHON PROGRAMMING (1BPLC105B) LAB COMPONENT
4. Develop a program to print 10 most frequently appearing words in a text file. [Hint: Use a dictionary
with distinct words and their frequency of occurrences. Sort the dictionary in the reverse order of
frequency and display the dictionary slice of the first 10 items.
Source Code:
from collections import OrderedDict
import numpy as np
import itertools
text = open("[Link]")
def sort_dict_by_value(d, reverse = False):
return dict(sorted([Link](), key = lambda x: x[1], reverse = reverse))
# Create an empty dictionary
d = dict()
# Loop through each line of the file
for line in text:
# Remove the leading spaces and newline character
line = [Link]()
# Convert the characters in line to
# lowercase to avoid case mismatch
line = [Link]()
# Split the line into words
words = [Link](" ")
# To eliminate delimiters.
for word in words:
word = [Link](".","")
word = [Link](",","")
word = [Link](":","")
word = [Link](";","")
word = [Link]("!","")
word = [Link]("*","")
# Iterate over each word in line
for word in words:
# Check if the word is already in dictionary
if word in d:
# Increment count of word by 1
d[word] = d[word] + 1
else:
# Add the word to dictionary with count 1
d[word] = 1
print("\n Sorted dictionary elements by frequency [Descending order]:\n")
d1 =sort_dict_by_value(d, True)
print(d1)
print("\n")
N= int(input("Enter the number of top frequency words to be displayed: \n"))
print("\nThe {0} most frequently appearing words are : \n".format(N))
out = dict([Link]([Link](),N))
for i in out:
print(i)
Input File:
ScreenShot of Code:
Sample Output:
Sorted dictionary elements by frequency (Descending order) : {'quantum': 6,
'and': 5, 'a': 4, 'computer': 4, 'of': 3, 'the': 3, 'is': 2, 'physical': 2, 'could': 2, 'in': 2,
'that': 1, 'exploits': 1, 'mechanical': 1, 'phenomena.': 1, 'at': 1, 'small': 1, 'scales,': 1,
'matter': 1, 'exhibits': 1, 'properties': 1, 'both': 1, 'particles': 1, 'waves,': 1,
'computing': 1, 'leverages': 1, 'this': 1, 'behavior': 1, 'using': 1, 'specialized': 1,
'hardware.': 1, 'classical': 1, 'physics': 1, 'cannot': 1, 'explain': 1, 'operation': 1,
'these': 1, 'devices,': 1, 'scalable': 1, 'perform': 1, 'some': 1, 'calculations': 1,
'exponentially': 1, 'faster': 1, 'than': 1, 'any': 1, 'modern': 1, '"classical"': 1,
'computer.': 1, 'particular,': 1, 'large-scale': 1, 'break': 1, 'widely-used': 1,
'encryption': 1, 'schemes': 1, 'aid': 1, 'physicists': 1, 'performing': 1, 'simulations;':
1, 'however,': 1, 'current': 1, 'state': 1, 'art': 1, 'still': 1, 'largely': 1, 'experimental':
1, 'impractical.': 1}
Enter the number of top frequency words to be displayed: 10
The 10 most frequently appearing words are :
quantum
and
a
computer
of
the
is
physical
could
in
7. Develop a function named DivExp which takes TWO parameters a, b, and returns a value c (c=a/b).
Write a suitable assertion for a>0 in the function DivExp and raise an exception for when b=0. Develop a
suitable program that reads two console values and calls the function DivExp.
SOURCE CODE:
def DivExp(a,b):
assert a>0,"a must be > 0"
if b == 0:
raise ZeroDivisionError;
else:
c = a/b
return c
a = int(input("Enter the value of a :"))
b = int(input("Enter the value of b :"))
r = DivExp(a,b)
print("The value of {0}/{1} = {2}".format(a,b,r))
Screenshot of Code:
Sample Output 1:
Output 2:
Output 3: