def _score(self, freq, scores, row, col, memoization=None):
mem = memoization if memoization is not None else [False] * scores.shape[1]
# memoization hit
if mem[col]: return scores[row, col]
children = self.hierarchy.successors(self.feature_names[col] if self.feature_names else col)
if len(children) == 0:
# Base case for leaves
scores[row, col] = freq[row, col]
mem[col] = True
return scores[row, col]
# recursively compute children score
score = float(0)
for child in children:
child_idx = self.vocabulary[child] if self.vocabulary else child
score += self._score(freq, scores, row, child_idx, memoization=mem)
# scale them with some method specific factor
if self.method in ["bell", "belllog"]:
k = nx.shortest_path_length(self.hierarchy, self.root, self.feature_names[col] if self.feature_names else col)
print(k+1, self.levels[k+1])
print("Count of children:", len(children))
denom = self.levels[k+1]
if self.method == "belllog": denom = log(denom, 10) #TODO problem when zero
score *= 1.0 / denom
elif self.method == "children":
score *= 1.0 / len(children)
elif self.method == "basic":
score *= self.decay
# add the freq of the concept just now since it should not be scaled
score += freq[row, col]
scores[row, col] = score
mem[col] = True
return scores[row, col]
评论列表
文章目录