2021-02-24

Feb 24 In-Class Exercise.

Please post your solutions to the Feb 24 In-Class Exercise to this thread.
Best,
Chris
Please post your solutions to the Feb 24 In-Class Exercise to this thread. Best, Chris

-- Feb 24 In-Class Exercise
rankProximity(t = ["to", "be"], k = 2)

u = -infinity
[u,v] = nextCover(t, u) which evaluates to [2,3] ("to be") in d1
d = docid(u) which evaluates to 1
score = 0
j = 0
u < infinity is true, so we enter the while loop
	d < docid(u) is false (they are equal), so we skip the if block
	score = 0 + 1 / (3-2+1) = 1/2
	[u,v] = nextCover(t, u) which evaluates to [3,5] ("be able to") in d1
u < infinity is true, so we stay in the while loop
	d < docid(u) is false (they are still equal), so we skip the if block
	score = 1/2 +  1 / (5-3+1) = 1/2 + 1/3 = 5/6
	[u,v] = nextCover(t, u) which evaluates to [0,1] ("to be") in d2 (i.e. docid(u) = 2 now!)
u < infinity is true, so we stay in the while loop
	d < docid(u) is true, so we enter the if block
		j = 0 + 1 = 1
		Result[1].docid = d = 1
		Result[1].score = score = 5/6
		d = docid(u) = 2
		score = 0
	score = 0 +  1 / (1-0+1) = 1/2
	[u,v] = nextCover(t, u) which evaluates to [1,4] ("be or not to") in d2
u < infinity is true, so we stay in the while loop
	d < docid(u) is false (they are equal), so we skip the if block
	score = 1/2 + 1 / (4-1+1) = 3/4
	[u,v] = nextCover(t, u) which evaluates to [4,5] ("to be") in d2
u < infinity is true, so we stay in the while loop
	d < docid(u) is false (they are equal), so we skip the if block
	score = 3/4 + 1 / (5-4+1) = 5/4
	[u,v] = nextCover(t, u) which evaluates to [infinity,infinity] as there is no further cover in this corpus

u < infinity is false, so we break out of the while loop
d < infinity is true, so we enter the if block
	j = 1 + 1 = 2
	Result[2].docid = d = 2
	Result[2].score = score = 5/4

sort Result[1...j] by score, so now Result[1].docid = 2, Result[1].score = 5/4, Result[2].docid = 1, Result[2].score = 5/6
return Result[1...k] = Result[1...2] which is just both of our results.
(Edited: 2021-02-24)
rankProximity(t = ["to", "be"], k = 2) <br>u = -infinity <br>[u,v] = nextCover(t, u) which evaluates to [2,3] ("to be") in d1 <br>d = docid(u) which evaluates to 1 <br>score = 0 <br>j = 0 <br> u < infinity is true, so we enter the while loop d < docid(u) is false (they are equal), so we skip the if block score = 0 + 1 / (3-2+1) = 1/2 [u,v] = nextCover(t, u) which evaluates to [3,5] ("be able to") in d1 u < infinity is true, so we stay in the while loop d < docid(u) is false (they are still equal), so we skip the if block score = 1/2 + 1 / (5-3+1) = 1/2 + 1/3 = 5/6 [u,v] = nextCover(t, u) which evaluates to [0,1] ("to be") in d2 (i.e. docid(u) = 2 now!) u < infinity is true, so we stay in the while loop d < docid(u) is true, so we enter the if block j = 0 + 1 = 1 Result[1].docid = d = 1 Result[1].score = score = 5/6 d = docid(u) = 2 score = 0 score = 0 + 1 / (1-0+1) = 1/2 [u,v] = nextCover(t, u) which evaluates to [1,4] ("be or not to") in d2 u < infinity is true, so we stay in the while loop d < docid(u) is false (they are equal), so we skip the if block score = 1/2 + 1 / (4-1+1) = 3/4 [u,v] = nextCover(t, u) which evaluates to [4,5] ("to be") in d2 u < infinity is true, so we stay in the while loop d < docid(u) is false (they are equal), so we skip the if block score = 3/4 + 1 / (5-4+1) = 5/4 [u,v] = nextCover(t, u) which evaluates to [infinity,infinity] as there is no further cover in this corpus <br>u < infinity is false, so we break out of the while loop <br>d < infinity is true, so we enter the if block j = 1 + 1 = 2 Result[2].docid = d = 2 Result[2].score = score = 5/4 <br>sort Result[1...j] by score, so now Result[1].docid = 2, Result[1].score = 5/4, Result[2].docid = 1, Result[2].score = 5/6 <br>return Result[1...k] = Result[1...2] which is just both of our results.

-- Feb 24 In-Class Exercise
Given d1 = "I want to be able to go" and d2 = "to be or not to be"
u = -infty
[u,v] = [2,3]
d = 1
score = 0
j = 0
u < infty -> true
	d < docid(u) -> false
	score = 0 + 1/(3-2+1) = 0.5
	[u,v] = [3,5]
u < infty -> true
	d < docid(u) -> false
	score = 0.5 + 1/(5-3+1) = 0.83
	[u,v] = [0,1]
u < infty -> true
	d < docid(u) -> true
		j = 1
		Result[1].docid = 1
		Result[1].score = 0.83
		d = 2
		score = 0
	score = 0 + 1/(1-0+1) = 0.5
	[u,v] = [1,4]
u < infty -> true
	d < docid(u) -> false
	score = 0.5 + 1/(4-1+1) = 0.75
	[u,v] = [4,5]
u < infty -> true
	d < docid(u) -> false
	score = 0.75 + 1/(5-4+1) = 1.25
	[u,v] = [infty, infty]
u < infty -> false
d < infty -> true
	j = 2
	Result[2].docid = 2
	Result[2].score = 1.25
sort Result[1..j] by score -> [[1.25,2],[0.83,1]]
return Result[1..k] -> [[1.25,2],[0.83,1]]
(Edited: 2021-02-24)
Given d1 = "I want to be able to go" and d2 = "to be or not to be" u = -infty [u,v] = [2,3] d = 1 score = 0 j = 0 u < infty -> true d < docid(u) -> false score = 0 + 1/(3-2+1) = 0.5 [u,v] = [3,5] u < infty -> true d < docid(u) -> false score = 0.5 + 1/(5-3+1) = 0.83 [u,v] = [0,1] u < infty -> true d < docid(u) -> true j = 1 Result[1].docid = 1 Result[1].score = 0.83 d = 2 score = 0 score = 0 + 1/(1-0+1) = 0.5 [u,v] = [1,4] u < infty -> true d < docid(u) -> false score = 0.5 + 1/(4-1+1) = 0.75 [u,v] = [4,5] u < infty -> true d < docid(u) -> false score = 0.75 + 1/(5-4+1) = 1.25 [u,v] = [infty, infty] u < infty -> false d < infty -> true j = 2 Result[2].docid = 2 Result[2].score = 1.25 sort Result[1..j] by score -> [[1.25,2],[0.83,1]] return Result[1..k] -> [[1.25,2],[0.83,1]]

-- Feb 24 In-Class Exercise
We compute the scores for each cover in the document based on this score + 1/(v - u + 1). We add up all of the scores for each document and store it in an array.
covers for doc 1:
  • "to be" [2,3]: 1/(3-2 +1) = 1/2
  • "be able to" [3,5]: 1/(5-3+1) = 1/3
  • score = 1/2 + 1/3 = 5/6
  • d is not smaller than docid(u) so we store 5/6
  • Result[1].docid = 1
  • Result[1].score = 5/6
score is reset back to 0 and d is assigned to 2
covers for doc 1:
  • "to be" [0,1]: 1/(1-0+1) = 1/2
  • "be or not to" [1,4]: 1/(4-1+1) = 1/4
  • score = 1/2 + 1/4 = 3/4
  • "to be" [4,5]: 1/(5-4+1) = 1/2
  • score = 3/4 + 1/2 = 5/4
  • d is not smaller than docid(u) so we store 5/4
  • Result[1].docid = 2
  • Result[1].score = 5/4
Sort and return Result by it's second index which returns: [(2, 5/4), (1,5/6)]
(Edited: 2021-02-24)
We compute the scores for each cover in the document based on this score + 1/(v - u + 1). We add up all of the scores for each document and store it in an array. covers for doc 1: *"to be" [2,3]: 1/(3-2 +1) = 1/2 *"be able to" [3,5]: 1/(5-3+1) = 1/3 *score = 1/2 + 1/3 = 5/6 *d is not smaller than docid(u) so we store 5/6 *Result[1].docid = 1 *Result[1].score = 5/6 score is reset back to 0 and d is assigned to 2 covers for doc 1: *"to be" [0,1]: 1/(1-0+1) = 1/2 *"be or not to" [1,4]: 1/(4-1+1) = 1/4 *score = 1/2 + 1/4 = 3/4 *"to be" [4,5]: 1/(5-4+1) = 1/2 *score = 3/4 + 1/2 = 5/4 *d is not smaller than docid(u) so we store 5/4 *Result[1].docid = 2 *Result[1].score = 5/4 Sort and return Result by it's second index which returns: [(2, 5/4), (1,5/6)]

-- Feb 24 In-Class Exercise
 [u,v] = [(1,2),(1,3)] (from nextCover)
 d = 1
 Score = 0
 j = 0
 First loop
 Score = 0 + 1/(2-1+1)
 [u,v] = [(1,3),(1,5)] (from nextCover)
 Second loop
 Score = 1/2 + 1/(5-3+1) = 5/6
 [u,v] = [(2,0),(2,1)] (from nextCover)
 Third loop
 J = 1 
 Result [1] = (1, 5/6)
 D = 2
 Score = 0
 Score = 0 + 1/2
 [u,v] = [(2,1),(2,4)] (from nextCover)
 Fourth loop 
 Score = 1/2 + 1/(4-1+1) = 3/4
 [u,v] = [(2,4),(2,5)] (from nextCover)
 5th loop
 Score = 3/4 + 1/2 = 5/4
 [u,v] = infinity 
 Break out of loop because u == infinity
 J = 2
 Result [2] = (2, 5/4)
 Result sort => Result[] = [(2, 5/4), (1, 5/6)]
 Return [(2, 5/4), (1, 5/6)]
[u,v] = [(1,2),(1,3)] (from nextCover) d = 1 Score = 0 j = 0 First loop Score = 0 + 1/(2-1+1) [u,v] = [(1,3),(1,5)] (from nextCover) Second loop Score = 1/2 + 1/(5-3+1) = 5/6 [u,v] = [(2,0),(2,1)] (from nextCover) Third loop J = 1 Result [1] = (1, 5/6) D = 2 Score = 0 Score = 0 + 1/2 [u,v] = [(2,1),(2,4)] (from nextCover) Fourth loop Score = 1/2 + 1/(4-1+1) = 3/4 [u,v] = [(2,4),(2,5)] (from nextCover) 5th loop Score = 3/4 + 1/2 = 5/4 [u,v] = infinity Break out of loop because u == infinity J = 2 Result [2] = (2, 5/4) Result sort => Result[] = [(2, 5/4), (1, 5/6)] Return [(2, 5/4), (1, 5/6)]

-- Feb 24 In-Class Exercise
rankProximity("to","be",2) initialize u as -infinity. set [u,v] as the first position where a cover of "to..be" is found with nextCover("to","be",-infinity). now [u,v] = [3,4] use the new u value from nextCover to select the first document to search with d = docid(u). set d = 1. initialize score and j to 0. enter while(3 < infinity) loop. test if d=1 < docid(3), false. so, append the value 1/(4-3+1) to score. (score = 1/2) update [u,v] with nextCover("to","be",3). ([u,v] = [4,6]) reiterate while loop (4 < infinity). test if d=1 < docid(4), false. so, append the value 1/(6-4+1) to score. (score = 1/2 + 1/3 = 5/6) update [u,v] with nextCover("to","be",4). ([u,v] = [1,2]) reiterate while loop (1 < infinity). test if d=1 < docid(1), true. append 1 to j (j = 1). write Result[1].docid = 1. write Result[1].score = score = 5/6. set d = 2. set score = 0. then, append the value 1/(1-0+1) to score (score = 1/2). update [u,v] with nextCover("to","be",1). ([u,v] = [2,5]) reiterate while loop (2 < infinity). test if d=2 < docid(2), false. so, append the value 1/(5-2+1) to score (score = 1/2 + 1/4). update [u,v] with nextCover("to","be",2). ([u,v] = [5,6]) reiterate while loop. test if d=2 < docid(5), false. so, append the value 1/(6-5+1) to score (score = 1/2 + 1/4 + 1/2). update [u,v] with nextCover("to","be",5). [u,v] = [infinity,infinity]. exit while loop since (infinity < infinity) is false. test if d=2 < infinity, true. append 1 to j (j = 2). write Result[2].docid = 2. write Result[2].score = score = 5/4. sort Result[1..j] by score -> [(5/4,2), (5/6,1)]. return Result[1..j] = [(5/4,2), (5/6,1)].
(Edited: 2021-02-24)
<nowiki> rankProximity("to","be",2) initialize u as -infinity. set [u,v] as the first position where a cover of "to..be" is found with nextCover("to","be",-infinity). now [u,v] = [3,4] use the new u value from nextCover to select the first document to search with d = docid(u). set d = 1. initialize score and j to 0. enter while(3 < infinity) loop. test if d=1 < docid(3), false. so, append the value 1/(4-3+1) to score. (score = 1/2) update [u,v] with nextCover("to","be",3). ([u,v] = [4,6]) reiterate while loop (4 < infinity). test if d=1 < docid(4), false. so, append the value 1/(6-4+1) to score. (score = 1/2 + 1/3 = 5/6) update [u,v] with nextCover("to","be",4). ([u,v] = [1,2]) reiterate while loop (1 < infinity). test if d=1 < docid(1), true. append 1 to j (j = 1). write Result[1].docid = 1. write Result[1].score = score = 5/6. set d = 2. set score = 0. then, append the value 1/(1-0+1) to score (score = 1/2). update [u,v] with nextCover("to","be",1). ([u,v] = [2,5]) reiterate while loop (2 < infinity). test if d=2 < docid(2), false. so, append the value 1/(5-2+1) to score (score = 1/2 + 1/4). update [u,v] with nextCover("to","be",2). ([u,v] = [5,6]) reiterate while loop. test if d=2 < docid(5), false. so, append the value 1/(6-5+1) to score (score = 1/2 + 1/4 + 1/2). update [u,v] with nextCover("to","be",5). [u,v] = [infinity,infinity]. exit while loop since (infinity < infinity) is false. test if d=2 < infinity, true. append 1 to j (j = 2). write Result[2].docid = 2. write Result[2].score = score = 5/4. sort Result[1..j] by score -> [(5/4,2), (5/6,1)]. return Result[1..j] = [(5/4,2), (5/6,1)]. </nowiki>

-- Feb 24 In-Class Exercise
['to', 'be'] = [2, 3] d = 1 score = 0 j = 0 Loop:
	doesn't satisfy the if condition
	score = 1/(3 - 2 + 1) = 1/2
	nextCover = [3, 5] 
	score = 1/2 + 1/(5 - 3 + 1) = 5/6
	
	d is lesser than infty
		j = 1
		Result[1].docid = 1
		Result[1].score = 5/6
	
	Result[1] = (1, 5/6)
	
	Score is reset to 0
	
	Covers for doc 2
	Cover = [0, 1]
	doesn't satisfy the if condition
	score = 1/(1 - 0 + 1) = 1/2
	nextCover = [1, 4]
	score = 1/2 + 1/(4 - 1 + 1) = 3/4
	nextCover = [4, 5]
	score = 3/4 + 1/(5 - 4 + 1) = 5/4
	
	d is lesser than infty
		j = 2
		Result[2].docid = 2
		Result[2].score = 5/4
	
	Result[2] = (2, 5/4)	
		
	Final Answer = [(2, 5/4), (1, 5/6)]
(Edited: 2021-02-24)
['to', 'be'] = [2, 3] d = 1 score = 0 j = 0 Loop: doesn't satisfy the if condition score = 1/(3 - 2 + 1) = 1/2 nextCover = [3, 5] score = 1/2 + 1/(5 - 3 + 1) = 5/6 d is lesser than infty j = 1 Result[1].docid = 1 Result[1].score = 5/6 Result[1] = (1, 5/6) Score is reset to 0 Covers for doc 2 Cover = [0, 1] doesn't satisfy the if condition score = 1/(1 - 0 + 1) = 1/2 nextCover = [1, 4] score = 1/2 + 1/(4 - 1 + 1) = 3/4 nextCover = [4, 5] score = 3/4 + 1/(5 - 4 + 1) = 5/4 d is lesser than infty j = 2 Result[2].docid = 2 Result[2].score = 5/4 Result[2] = (2, 5/4) Final Answer = [(2, 5/4), (1, 5/6)]

-- Feb 24 In-Class Exercise
d1 = "I want to be able to go" and d2 = "to be or not to be"
rankProximity("to","be",2)
here k = 2 (number of results to return)
u = -infinity. set [u,v] with nextCover("to","be",-infinity). now [u,v] = [2,3] d = docid(u). set d = 1. score = 0 j = 0
while(u < infinity) loop.
  if d < docid(u) -> false.
  so, score = 0 + 1/(3-2+1) = 1/2
  [u,v] = [3,5]
while(u < infinity) -> true
  if d < docid(u) -> false.
  so, score = 1/2 + (1/(5-3+1)) = 5/6
  [u,v] = [0,1]
while(u < infinity) -> true
  if d < docid(u) -> true
       j = 1
       Result[1].docid = 1
       Result[1].score = 5/6
       d = 2
       score = 0
   score = 0 + (1/(1-0+1)) = 1/2
  [u,v] = [1,4]
while(u < infinity) -> true
  if d < docid(u) -> false.
  so, score = 1/2 + (1/(4-1+1)) = 3/4
  [u,v] = [4,5] 
while(u < infinity) -> true
  if d < docid(u) -> false.
  so, score = 3/4 + (1/(5-4+1)) = 5/4
  [u,v] = [infinity,infinity] 
while(u < infinity) -> false (break out of while loop)
if (d(2) < infinity) - > true.
    j = 1 + 1 = 2
    Result[2].docid = 2
    Result[2].score = 5/4
sort Result[1,2] by score -> Result[1] = 5/4 and Result[2] = 5/6 return Result[1,2] = Result[1] = 5/4 and Result[2] = 5/6
(Edited: 2021-02-24)
d1 = "I want to be able to go" and d2 = "to be or not to be" rankProximity("to","be",2) here k = 2 (number of results to return) u = -infinity. set [u,v] with nextCover("to","be",-infinity). now [u,v] = [2,3] d = docid(u). set d = 1. score = 0 j = 0 while(u < infinity) loop. if d < docid(u) -> false. so, score = 0 + 1/(3-2+1) = 1/2 [u,v] = [3,5] while(u < infinity) -> true if d < docid(u) -> false. so, score = 1/2 + (1/(5-3+1)) = 5/6 [u,v] = [0,1] while(u < infinity) -> true if d < docid(u) -> true j = 1 Result[1].docid = 1 Result[1].score = 5/6 d = 2 score = 0 score = 0 + (1/(1-0+1)) = 1/2 [u,v] = [1,4] while(u < infinity) -> true if d < docid(u) -> false. so, score = 1/2 + (1/(4-1+1)) = 3/4 [u,v] = [4,5] while(u < infinity) -> true if d < docid(u) -> false. so, score = 3/4 + (1/(5-4+1)) = 5/4 [u,v] = [infinity,infinity] while(u < infinity) -> false (break out of while loop) if (d(2) < infinity) - > true. j = 1 + 1 = 2 Result[2].docid = 2 Result[2].score = 5/4 sort Result[1,2] by score -> Result[1] = 5/4 and Result[2] = 5/6 return Result[1,2] = Result[1] = 5/4 and Result[2] = 5/6
2021-02-25

-- Feb 24 In-Class Exercise
rankProximity(t = ["to", "be"], k = 2)
u = -inf
[u,v] = nextCover(t, u) = [2,3] ---> ("to be") in d1
d = 1
score = 0
j = 0
u < inf -> true; entering while loop,
  • d < docid(u) -> false; don't enter if block
  • score = 0 + 1/(3-2+1) = 1/2
  • [u,v] = nextCover(t, u) = [3,5] ---> ("be able to") in d1
u < inf -> true; enter while loop
  • d < docid(u) -> false; don't enter if block
  • score = 1/2 + 1/(5-3+1) = 5/6
  • [u,v] = nextCover(t, u) = [0,1] ---> ("to be") in d2
u < inf -> true; enter while loop
  • d < docid(u) -> true; enter if block
    • j = 0 + 1 = 1
    • Result[1].docid = d = 1
    • Result[1].score = 5/6
    • d = 2
    • score = 0
  • if block ends
  • score = 0 + 1/(1-0+1) = 1/2
  • [u,v] = nextCover(t, u) = [1,4] ---> ("be or not to") in d2
u < inf -> true; in while loop
  • d < docid(u) -> false; don't enter if block
  • score = 1/2 + 1/(4-1+1) = 3/4
  • [u,v] = nextCover(t, u) = [4,5] ---> ("to be") in d2
u < inf -> true; in the while loop
  • d < docid(u) -> false; don't enter if block
  • score = 3/4 + 1/(5-4+1) = 5/4
  • [u,v] = nextCover(t, u) = [inf,inf] since the cover in this corpus are exhausted
u < inf -> false; exit the while loop
d < inf -> true; enter the if block
  • j = 1 + 1 = 2
  • Result[2].docid = 2
  • Result[2].score = 5/4
Sorting Result by score:
  • Result[1].docid = 2, Result[1].score = 5/4
  • Result[2].docid = 1, Result[2].score = 5/6 return Result[1...k] = Result[1...2]
(Edited: 2021-02-25)
rankProximity(t = ["to", "be"], k = 2) u = -inf [u,v] = nextCover(t, u) = [2,3] ---> ("to be") in d1 d = 1 score = 0 j = 0 u < inf -> true; entering while loop, * d < docid(u) -> false; don't enter if block * score = 0 + 1/(3-2+1) = 1/2 * [u,v] = nextCover(t, u) = [3,5] ---> ("be able to") in d1 u < inf -> true; enter while loop * d < docid(u) -> false; don't enter if block * score = 1/2 + 1/(5-3+1) = 5/6 * [u,v] = nextCover(t, u) = [0,1] ---> ("to be") in d2 u < inf -> true; enter while loop * d < docid(u) -> true; enter if block ** j = 0 + 1 = 1 ** Result[1].docid = d = 1 ** Result[1].score = 5/6 ** d = 2 ** score = 0 * if block ends * score = 0 + 1/(1-0+1) = 1/2 * [u,v] = nextCover(t, u) = [1,4] ---> ("be or not to") in d2 u < inf -> true; in while loop * d < docid(u) -> false; don't enter if block * score = 1/2 + 1/(4-1+1) = 3/4 * [u,v] = nextCover(t, u) = [4,5] ---> ("to be") in d2 u < inf -> true; in the while loop * d < docid(u) -> false; don't enter if block * score = 3/4 + 1/(5-4+1) = 5/4 * [u,v] = nextCover(t, u) = [inf,inf] since the cover in this corpus are exhausted u < inf -> false; exit the while loop d < inf -> true; enter the if block * j = 1 + 1 = 2 * Result[2].docid = 2 * Result[2].score = 5/4 Sorting Result by score: * Result[1].docid = 2, Result[1].score = 5/4 * Result[2].docid = 1, Result[2].score = 5/6 return Result[1...k] = Result[1...2]
2021-02-28

-- Feb 24 In-Class Exercise
For document 1: Cover 1 found at [2,3] in document 1
u=1 d=u=1
Since d=the currently processing doc, we will skip the if block and straight away compute the score for cover 1.
1/((2-1)+1)=1/2
Then we will search for next cover.
Cover 2 found at [3,5] for (to be able to)
d=the currently processing doc , we will skip the while loop and compute the score for the second cover and do summation, given below.
1/((5-3)+1)=1/3 ((1/2)+(1/3))=5/6.
Next cover was found in doc 2.
So now d=2 The previously set value for d is lesser than the current value. 1<2 , so we enter the if block:
And we create a field for doc 1 in the Result array with doc ID=1 and score as 5/6 for the score calculated for doc 1.We also reset the score to 0.
After coming out of the if block we calculate the score for the first cover from doc2 shown below:
[0,1] for (to be) 1/((1-0)+1)=1/2
Then we will search the next cover in doc 2 which is shown below:
[1,4] for (be or not to)
We will go to through the loop(skipping if block) and calculate the score of this cover and perform summation. 1/((4-1)+1)=1/4 (1/2)+(1/4)=3/4
Then we will come out of the loop and search the next cover in doc 2.
Next cover comes out to be [4,5] for (to be).After finding the cover we will again go through the while loop(Skipping if block).
We will perform score of cover and summation of the the score for the cover [4,5] shown below: 1/((5-4)+1)=1/2 (3/4)+(1/2)=5/4
Since there is no next cover for "to be" in the document, we will get infinity as the result. Which will make us come out of the while loop.
After coming out we will update the Result array at index 2 with [2,5/4].
Finally we sort the array in the descending order of score. The final array looks like below:
[[2,5/4],[1,5/6]]
Then we return the same array as output.
(Edited: 2021-02-28)
For document 1: Cover 1 found at [2,3] in document 1 u=1 d=u=1 Since d=the currently processing doc, we will skip the if block and straight away compute the score for cover 1. 1/((2-1)+1)=1/2 Then we will search for next cover. Cover 2 found at [3,5] for (to be able to) d=the currently processing doc , we will skip the while loop and compute the score for the second cover and do summation, given below. 1/((5-3)+1)=1/3 ((1/2)+(1/3))=5/6. Next cover was found in doc 2. So now d=2 The previously set value for d is lesser than the current value. 1<2 , so we enter the if block: And we create a field for doc 1 in the Result array with doc ID=1 and score as 5/6 for the score calculated for doc 1.We also reset the score to 0. After coming out of the if block we calculate the score for the first cover from doc2 shown below: [0,1] for (to be) 1/((1-0)+1)=1/2 Then we will search the next cover in doc 2 which is shown below: [1,4] for (be or not to) We will go to through the loop(skipping if block) and calculate the score of this cover and perform summation. 1/((4-1)+1)=1/4 (1/2)+(1/4)=3/4 Then we will come out of the loop and search the next cover in doc 2. Next cover comes out to be [4,5] for (to be).After finding the cover we will again go through the while loop(Skipping if block). We will perform score of cover and summation of the the score for the cover [4,5] shown below: 1/((5-4)+1)=1/2 (3/4)+(1/2)=5/4 Since there is no next cover for "to be" in the document, we will get infinity as the result. Which will make us come out of the while loop. After coming out we will update the Result array at index 2 with [2,5/4]. Finally we sort the array in the descending order of score. The final array looks like below: [[2,5/4],[1,5/6]] Then we return the same array as output.
[ Next ]
X