I want to find a way in which we can score each tree by summing the scores of its spans and choose the highest scoring binary tree that maximizes the sum by backtracking.
Assume, I have the following sentence:
string = "Revenue was 19.9 million"
The spans array represents the scores of span at the corresponding (i, j) position. Here: j=i+1
For example,
score(0, 1) = -100.0 corresponds to the span "Revenue";
score(0, 4) = 100.0 corresponds to the span "Revenue was 19.9 million";
score(1, 4) = 100.0 corresponds to the span "was 19.9 million";
And so on ...
# 1 2 3 4
spans = [[-100.0, -100.0, -100.0, 100.0], # 0
[ 0, -100.0, -100.0, 100.0], # 1
[ 0, 0 , -100.0, 100.0], # 2
[ 0, 0, 0, -100.0]] # 3
The expected tree is - (Revenue (was (19.9 million))) corresponding to the spans (0, 4), (1, 4), (2, 4).
Although, I am getting - (Revenue was 19.9 million) with the following code.
I tried to do:
class MaxSpanCalculator:
def __init__(self, s, spans):
self.sentence = s.split(" ")
self.spans = spans
self.splits = []
def get_max_span_sentence(self):
n = len(self.sentence)
# Initialize the splits array
for i in range(n):
s = []
for j in range(n):
s.append(-1)
self.splits.append(s)
# Here Using spans array itself as dp array
for l in range(1, n+1):
i = 0
j = i + l - 1
while j<n:
if i!=j:
for p in range(i,j):
if self.spans[i][j] < self.spans[i][p] + self.spans[p+1][j]:
self.splits[i][j] = p
self.spans[i][j] = self.spans[i][p] + self.spans[p+1][j]
i = i + 1
j = j + 1
print("Max span value: " + str(self.spans[0][n-1]))
print("Max span sentence is : " + self.get_sentence(0, n-1))
def get_sentence(self, start, end):
# print(str(start) + " " + str(end))
if start==end: return self.sentence[start]
if self.splits[start][end] == -1:
return " ".join(x for x in self.sentence[start:end+1])
return "(" + self.get_sentence(start, self.splits[start][end]) + ")" + "(" + self.get_sentence(self.splits[start][end]+1, end) + ")"