Exercise 18: TF-IDF Advanced level
I think I entered a correct solution, but my solution is surprisingly rejected as wrong.
According to the forum rules I’ll report just a summary of my code:
text = '''Humpty Dumpty sat on a wall
Humpty Dumpty had a great fall
all the king's horses and all the king's men
couldn't put Humpty together again'''
def distance(row1, row2):
#this function returns the sum of differences between the occurrences of each word in row1 and row2
def find_nearest_pair(data):
#This function returns an nXn array (called "dist"), were n is the number of rows in text, and a couple.
#The i,j element of dist is:
# inf if i=j;
# the distance between row i and row j in text otherwise (performed by a call to function "distance").
#The couple is made of the coordinates of the lowest element in "dist".
def main(text):
# As required, this function, calling the functions "distance" and "find_nearest_pair" defined above, should perform the following tasks:
# 1. split the text into words, and get a list of unique words that appear in it
# 2. go over each unique word and calculate its term frequency, and its document frequency
# 3. go over each line in the text and calculate its TF-IDF representation, which will be a vector
# 4. calculate the distances between each line to find which are the closest.
By this code I get the following output:
[[0.0, 0.020823122768049988, 0.05017166594399686, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.05017166594399686, 0.0, 0.0, 0.0, 0.0, 0.10034333188799371, 0.10034333188799371, 0.10034333188799371, 0.0], [0.0, 0.020823122768049988, 0.05017166594399686, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10034333188799371, 0.10034333188799371, 0.05017166594399686, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10034333188799371], [0.13379110918399162, 0.0, 0.0, 0.0, 0.0, 0.06689555459199581, 0.13379110918399162, 0.0, 0.0, 0.0, 0.0, 0.0, 0.13379110918399162, 0.06689555459199581, 0.06689555459199581, 0.0, 0.0, 0.0, 0.0], [0.0, 0.024987747321659988, 0.0, 0.12041199826559246, 0.12041199826559246, 0.0, 0.0, 0.12041199826559246, 0.0, 0.0, 0.0, 0.12041199826559246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
[[ inf 0.60205999 1.02425644 0.88718595]
[0.60205999 inf 1.02425644 0.88718595]
[1.02425644 1.02425644 inf 1.10869573]
[0.88718595 0.88718595 1.10869573 inf]]
(0, 1)
So it should work, but the reply is the following:
Test Failed: AssertionError: '[[0.0, 0.020823122768049988, 0.05017166594399686, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.05017166594399686, 0.0, 0.0, 0.0, 0.0, 0.10034333188799371, 0.10034333188799371, 0.10034333188799371, 0.0], [0.0, 0.020823122768049988, 0.05017166594399686, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10034333188799371, 0.10034333188799371, 0.05017166594399686, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10034333188799371], [0.13379110918399162, 0.0, 0.0, 0.0, 0.0, 0.06689555459199581, 0.13379110918399162, 0.0, 0.0, 0.0, 0.0, 0.0, 0.13379110918399162, 0.06689555459199581, 0.06689555459199581, 0.0, 0.0, 0.0, 0.0], [0.0, 0.024987747321659988, 0.0, 0.12041199826559246, 0.12041199826559246, 0.0, 0.0, 0.12041199826559246, 0.0, 0.0, 0.0, 0.12041199826559246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]\n[[ inf 0.60205999 1.02425644 0.88718595]\n [0.60205999 inf 1.02425644 0.88718595]\n [1.02425644 1.02425644 inf 1.10869573]\n [0.88718595 0.88718595 1.10869573 inf]]\n(0, 1)' not found in ['(0, 1)', '(1, 0)']
I tested it also with the following text:
There's a long, long trail a-winding
Into the land of my dreams,
Where the nightingales are singing
And the white moon beams.
There's a long, long night of waiting
Until my dreams all come true;
Till the day when I'll be going down
That long, long trail with you.
getting the following output:
[[0.10034333188799371, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10034333188799371, 0.0, 0.0, 0.10034333188799371, 0.0, 0.0, 0.0, 0.0, 0.15051499783199057, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.07099478871204684, 0.0, 0.07099478871204684, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.15051499783199057, 0.15051499783199057, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15051499783199057, 0.0, 0.0, 0.0, 0.05017166594399686, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10034333188799371, 0.0, 0.0, 0.10034333188799371, 0.0, 0.0, 0.0], [0.0, 0.18061799739838869, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.18061799739838869, 0.0, 0.0, 0.0, 0.0, 0.18061799739838869, 0.18061799739838869, 0.0, 0.0, 0.0, 0.0, 0.0, 0.06020599913279623, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.18061799739838869, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.18061799739838869, 0.0, 0.18061799739838869, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.06020599913279623, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.18061799739838869, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0860085701897089, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.12901285528456333, 0.0, 0.0, 0.0860085701897089, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.12901285528456333, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.060852676038897296, 0.0860085701897089, 0.060852676038897296, 0.0, 0.0], [0.0, 0.0, 0.0, 0.15051499783199057, 0.0, 0.0, 0.15051499783199057, 0.15051499783199057, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15051499783199057, 0.15051499783199057, 0.0, 0.0, 0.10034333188799371, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.11288624837399293, 0.0, 0.0, 0.0, 0.0, 0.11288624837399293, 0.0, 0.0, 0.0, 0.0, 0.0, 0.11288624837399293, 0.0, 0.11288624837399293, 0.03762874945799764, 0.11288624837399293, 0.0, 0.0, 0.0, 0.0, 0.0, 0.11288624837399293, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.11288624837399293], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15051499783199057, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10034333188799371, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15051499783199057, 0.0, 0.0, 0.0, 0.07099478871204684, 0.0, 0.07099478871204684, 0.15051499783199057, 0.0]]
[[ inf 1.29593789 1.37621256 1.37621256 0.64384636 1.44645289
1.42136706 0.80274666]
[1.29593789 inf 1.38473798 1.38473798 1.16814296 1.35463498
1.45497831 1.39628123]
[1.37621256 1.38473798 inf 1.44494398 1.42043476 1.63559631
1.53525298 1.47655589]
[1.37621256 1.38473798 1.44494398 inf 1.42043476 1.63559631
1.53525298 1.47655589]
[0.64384636 1.16814296 1.42043476 1.42043476 inf 1.49067509
1.46558926 1.08822397]
[1.44645289 1.35463498 1.63559631 1.63559631 1.49067509 inf
1.68075081 1.54679622]
[1.42136706 1.45497831 1.53525298 1.53525298 1.46558926 1.68075081
inf 1.52171039]
[0.80274666 1.39628123 1.47655589 1.47655589 1.08822397 1.54679622
1.52171039 inf]]
(0, 4)
Which should confirm the correctness of my solution.