Question: Python Program Assume that the following dictionary is given to you: {'Nancy Pollock': {'Lawrence of Arabia': 2.5, 'Gravity': 3.5, 'The Godfather': 3.0, 'Prometheus': 3.5, 'For
Python Program
Assume that the following dictionary is given to you:
{'Nancy Pollock': {'Lawrence of Arabia': 2.5, 'Gravity': 3.5,
'The Godfather': 3.0, 'Prometheus': 3.5, 'For a Few Dollars More': 2.5,
'The Guns of Navarone': 3.0},'Jack Holmes': {'Lawrence of Arabia': 3.0, 'Gravity': 3.5,
'The Godfather': 1.5, 'Prometheus': 5.0, 'The Guns of Navarone': 3.0,
'For a Few Dollars More': 3.5}, 'Mary Doyle': {'Lawrence of Arabia': 2.5, 'Gravity': 3.0,
'Prometheus': 3.5, 'The Guns of Navarone': 4.0},'Doug Redpath': {'Gravity': 3.5, 'The Godfather': 3.0,
'The Guns of Navarone': 4.5, 'Prometheus': 4.0, 'For a Few Dollars More': 2.5},'Jill Brown': {'Lawrence of Arabia': 3.0, 'Gravity': 4.0,
'The Godfather': 2.0, 'Prometheus': 3.0, 'The Guns of Navarone': 3.0, 'For a Few Dollars More': 2.0}, 'Trevor Chappell': {'Lawrence of Arabia': 3.0, 'Gravity': 4.0,
'The Guns of Navarone': 3.0, 'Prometheus': 5.0, 'For a Few Dollars More': 3.5},'Peter': {'Gravity':4.5,'For a Few Dollars More':1.0,'Prometheus':4.0}}
The dictionary contains names of reviewers and their reviews of different movies. You are required to write a Python program that computes a similarity score between any two reviewers. A simple way to calculate a similarity score is to use Euclidean distances. An example is given below:
Trevor Chappells reviews: {'Lawrence of Arabia': 3.0, 'Gravity': 4.0,
'The Guns of Navarone': 3.0, 'Prometheus': 5.0, 'For a Few Dollars More': 3.5}
Peters reviews: {'Gravity':4.5,'For a Few Dollars More':1.0,'Prometheus':4.0}
The Euclidean distance between Peter and Trevor Chappell is computed as follows:
Only consider the movies that both have reviewed.
Take the difference between corresponding reviews.
Sum the square of the differences
The square root of the sum of differences is the Euclidean score. The shorter the distance the closer the two reviewers.
For our example, this would be: (4.0 4.5)2 + (3.5 1.0)2 + (5.0 4.0)2 = 0.25 + 6.25 + 1 = 7.5
Euclidean distance is Square Root of 7.5 = 2.7386
Your program should provide two methods. The first will compute the similarity between two reviewers. The second will get the similarities between a given reviewer and all other reviewer. For example, the results for the following calls are shown below:
print(getSimilarity("Peter", "Trevor Chappell"))
2.7386127875258306
getSimilarities("Peter")
Peter Nancy Pollock 1.87
Peter Jill Brown 1.50
Peter Jack Holmes 2.87
Peter Trevor Chappell 2.74
Peter Mary Doyle 1.58
Peter Doug Redpath 1.80
Note that these methods will use the dictionary of reviews to compute similarities.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
