def APSyn(x_row, y_row, N):
"""
APSyn(x, y) = (\sum_{f\epsilon N(f_{x})\bigcap N(f_{y})))} \frac{1}{(rank(f_{x})+rank(f_{y})/2)})
:param x_row:
:param y_row:
:return:
"""
# Sort y's contexts
y_contexts_cols = sort_by_value_get_col(scipy.sparse.coo_matrix(y_row.mat)) # tuples of (row, col, value)
y_contexts_cols = y_contexts_cols[:N]
y_context_rank = { c : i + 1 for i, c in enumerate(y_contexts_cols) }
# Sort x's contexts
x_contexts_cols = sort_by_value_get_col(scipy.sparse.coo_matrix(x_row.mat))
x_contexts_cols = x_contexts_cols[:N]
x_context_rank = { c : i + 1 for i, c in enumerate(x_contexts_cols) }
# Average of 1/(rank(w1)+rank(w2)/2) for every intersected feature among the top N contexts
intersected_context = set(y_contexts_cols).intersection(set(x_contexts_cols))
score = sum([1.0 / ((x_context_rank[c] + y_context_rank[c]) / 2.0) for c in intersected_context])
#score *= (1.0 / N)
return score
评论列表
文章目录